-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_FindPosOfSetBit.cpp
65 lines (51 loc) · 1.23 KB
/
GFG_FindPosOfSetBit.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
/*
https://practice.geeksforgeeks.org/problems/find-position-of-set-bit3706/1
Find position of set bit
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int findPosition1(int N) {
if(N==0) return -1;
int pos=0;
bool flag=false; //for second time 1.
while(N>0)
{
pos++; // increase position count
if(N&1 == 1 )
{
if(flag) return -1; //second one
flag = true;
}
N>>=1; //left shift by 1 bit
}
return pos;
}
int findPosition(int N) {
if( (N==0) || ((N&(N-1))!=0) )
return -1; // not power of two
return log2(N)+1; // power of two
// int pos=0;
// while(N>0)
// {
// pos++; // increase position count
// N>>=1; //left shift by 1 bit
// }
// return pos;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin>>N;
Solution ob;
cout << ob.findPosition(N) << endl;
}
return 0;
} // } Driver Code Ends