![]() |
VOOZH | about |
Given a number n, the task is to find the maximum 0's between two immediate 1's in binary representation of given n. Return -1 if binary representation contains less than two 1's.
Examples :
Input : n = 47 Output: 1 // binary of n = 47 is 101111 Input : n = 549 Output: 3 // binary of n = 549 is 1000100101 Input : n = 1030 Output: 7 // binary of n = 1030 is 10000000110 Input : n = 8 Output: -1 // There is only one 1 in binary representation // of 8.
The idea to solve this problem is to use shift operator. We just need to find the position of two immediate 1's in binary representation of n and maximize the difference of these position.
Below is the implementation of the above idea :
Output:
3
Time Complexity: O(1), because it is taking constant time irrespective of the input n.
Auxiliary Space: O(1)