


INPUT : N = 4
OUTPUT : 3
In the above example, N = 4, whose Binary representation is “0100”. It is clearly visible that the binary number contains a single set bit, at a position 3 from the LSB end. Hence the output is 3
INPUT : N = 8
OUTPUT: 4
In the above example, N = 8, whose Binary representation is “1000”. It is clearly visible that the binary number contains a single set bit, at a position 4 from the LSB end. Hence the output is 4
INPUT : N = 9
OUTPUT : -1
In the above example, N = 9, whose Binary representation is “1001”. Now, the binary number contains 2 set bits, at a position 4 and 1 from LSB end. Hence the output is -1.
INPUT : N = 0
OUTPUT : -1
In the above example, N = 0, whose Binary representation is “0000”. Now, the binary number contains no set bits at all. Hence the output will be -1.
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
The first and the only line of each test case contains a single integer ‘N’.
For every test case, print a single integer, which is the position of the lone set bit in the binary representation of the input integer ‘N’.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
0 <= N <= 10^5
Time limit: 1 second
If we observe a little, we can clearly see that only those integers, which can be represented as a power of 2 have a single set bit in their binary equivalent, and rest either have 0 or more than 1 set bits. Now, we only have to work on the integers which are a power of 2. We will start from the LSB end or the rightmost bit, and check every bit one by one.
We know that the only set bit will always be the leftmost bit or the MSB (Most Significant Bit). So in this approach, we will perform the right shift operation on the only set bit of ‘N’ by 1, until it becomes ‘0’ also keeping the count of each shift. The final value of the count will be the position of the set bit.
If the integer is the power of 2, then finding the Log of the base 2, of the input integer N, and incrementing it by 1 will give the position of its set bit. If the integer is a power of two, then its log to the base 2 will give the count of 0s in its binary representation. Hence incrementing it by 1 will be the position of the set bit.
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers