-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two_bits.cpp
84 lines (64 loc) · 1.05 KB
/
Two_bits.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
4star:
standard input/output: 2s/128000 KB
Given an integer X find an integer Y such that Y has exactly two set bits in its binary representaion and abs(X- Y) is minimum.
Input
Input contains a single integer X.
Constraints
1 <= X <= 10^15
Output
Print a single integer, the minimum value of abs(X- Y).
Sample input 1
5
Sample output 1
0
Explanation: Y=5.
Sample input 2
1
Sample output 2
2
Explanation: Y=3
*/
#include<bits/stdc++.h>
using namespace std;
#define vi vector<ll>
typedef long long int ll;
void solv(){
ll n ;
cin >> n ;
ll y = 0 ;
if(n == 0){
cout << 3 << endl;
return;
}
ll k = 0;
for(ll i = 31 ; i>=0 ; --i ){
bool bit = (n) &(1 << i);
if(bit == 1){
y |= (1 << i);
k++;
}
if(k == 2){
cout << abs(n - y) << endl;
return;
}
}
ll left = 1;
ll x = 0;
for(ll i = k ; i <= 1 ; ++i){
// bool bit = ( n ) & (i << i);
y = (y) | ( 1 | (x << left));
// cout << bit << endl;
left++;
}
cout << abs(n - y) << endl;
}
int main () {
ll tt;
// cin >> tt;
tt =1;
while(tt--){
solv();
}
return 0;
}