-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path11.04.2024.cpp
50 lines (44 loc) · 1016 Bytes
/
11.04.2024.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution{
public:
// function to convert a given Gray equivalent n to Binary equivalent.
int grayToBinary(int n)
{
// Your code here
string b="";
for(int i=0;i<=31;i++){
if(n & (1<<i)){
b='1'+b;
}
else{
b='0'+b;
}
}
int idx=0;
for(int i=0;i<=31;i++){
if(b[i]=='1'){
idx=i;
break;
}
}
b=b.substr(idx, 32-idx);
string g="";
g+=b[0];
int a=b[0]-'0';
for(int i=1;i<b.length();i++){
int x=b[i]-'0';
if(a^x){
g=g+'1';
a=1;
}
else{
g=g+'0';
a=0;
}
}
int ans=0;
for(int i=0;i<g.length();i++){
ans=ans*2 + (g[i]-'0');
}
return ans;
}
};