-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHard_Prob217.cpp
More file actions
74 lines (58 loc) · 1.6 KB
/
Hard_Prob217.cpp
File metadata and controls
74 lines (58 loc) · 1.6 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* We say a number is sparse if there are no adjacent ones in its binary representation. For example, 21 (10101) is sparse, but 22 (10110) is not. For a given input N, find the smallest sparse number greater than or equal to N.
Do this in faster than O(N log N) time.*/
#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;
void intToBits(int n, vector<bool>& bits) {
bits.clear();
int k = 0;
while (n >> (k) != 0)
{
k ++;
}
k --;
for (int i=k; i>= 0; i--) {
int r = (n>>i)%2;
bits.push_back(r);
n = n - (r<<i);
}
}
int bitsToInt(vector<bool>& bits) {
int n = 0;
for (int k=0; k<bits.size(); k++) {
n += bits[k]<<(bits.size()-k-1);
}
return n;
}
void printBits(vector<bool> bits) {
for (int k=0; k<bits.size(); k++){
cout << bits[k] << " " ;
}
cout << "\n";
}
int findNextSparse(int n) {
vector<bool> bits ;
intToBits(n, bits);
int result = n;
for (int deg=bits.size()-1; deg>=0; deg--) {
if (bits[deg] == 1 & bits[deg+1] == 1 & bits[deg-1] != 1) {
for (int k=bits.size(); k>=deg-1; k--) {
bits[k] = 0;
}
if (deg-1 < 0) {
bits.insert(bits.begin(),1);
} else {
bits[deg-1] = 1;
}
}
}
result = bitsToInt(bits);
return result;
}
int main(int argc, char *argv[]) {
cout << findNextSparse(6) << endl;
cout << findNextSparse(4) << endl;
cout << findNextSparse(38) << endl;
cout << findNextSparse(44) << endl;
}