Find position of set bit | Practice | GeeksforGeeks

Given a number N having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of  set bit '1' should be counted starting with 1 from LSB side in binary representation of the number.
Example 1:
Input:
N = 2
Output:
2
Explanation:
2 is represented as "10" in Binary.
As we see there's only one set bit
and it's in Position 2 and thus the
Output 2.
Example 2:
Input:
N = 5
Output:
-1
Explanation:
5 is represented as "101" in Binary.
As we see there's two set bits
and thus the Output -1.

Your Task:
You don't need to read input or print anything. Your task is to complete the function findPosition() which takes an integer N as input and returns the answer.
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(1)
Constraints:
0 <= N <= 108
0
aamchicoder2 days ago
int findPosition(int N) {       // code here       if(N && !(N & N-1)){           return log2(N)+1;       }       return -1;   }
+1
shreepadbhuvad7914 days ago
Easy java solution
 
class Solution {   static int findPosition(int N) {      if(N==0)return -1;       int b= N & (N-1);       if(b==0){       int ans=(int)(Math.log(N) / Math.log(2));       return ans+1;       }       return -1;   }};
0
amarrajsmart1976 days ago
  int setbitcount(int n) {     int count=0;     while(n!=0)     {         n=n&(n-1);         count++;     }     return count; }   int findPosition(int N) {       // code here       if(setbitcount(N)>1||setbitcount(N)<1)       {           return -1;       }       int i=0;       while(N)       {           i++;           N=N>>1;       }       return i;   }
0
rohanmeher1641 week ago
class Solution {   static int findPosition(int N) {       int curr=1;       int p=-1;       int bit=0;       while(N!=0)       {            if((N&1)==1)           {p=curr;            bit++;           }                      if(bit>1)            {               p=-1;               break;           }                      N=N>>1;           curr++;       }       return p;   }}
+1
ayushrkacholia1 week ago
class Solution { public:   int findPosition(int N) {       if(N==0){           return -1;       }       int k = N;       int pos =0;       int c =0;       while(k){           k=k&(k-1);           c++;           if(c>1){               return -1;           }       }              while(N){           pos++;           N=N>>1;       }              return pos;   }};
 
Time taken = 0.01/1.12
+1
himanshukug19cs2 weeks ago
  if(N==0)           return -1;       int n=1;       while(N>0&&(N&1)==0){           n++;           N=N>>1;       }       N=N>>1;       if(N!=0)           return -1;       else           return n;
0
garvitchawla9122 weeks ago
class Solution {   static int findPosition(int N) {       // code here      if((N&(N-1))!=0)return -1;      int res=1;            for(int i=N;i>=0;i/=2){          if(i%2==0)res++;          else break;      }      return res;          }};
+1
negisachinsingh4 weeks ago
int findPosition(int N) {       // code here       int k;       k=log(N)/log(2);      if(N==0)   return -1;      if((N & N-1) == 0) //while using this take care of N=0 case;   return k+1;      else   return -1;
                 }
0
sakesai301 month ago
static int findPosition(int N) {       if(N==1) return 1;       if(N%2 != 0) return -1;       int k = 0;       int max =0;       for(int i =N;i>0;i/=2){           if(i%2!=0) max++;           k++;           if(max>1) return -1;       }       if(k==0) return -1;       return k;   }
0
milindprajapatmst191 month ago
class Solution {
  public:
    int findPosition(int N) {
        if (N == 0 || N & (N - 1))
            return -1;
        int result, pos = 1;
        while (N) {
            if (N & 1) result = pos;
            pos++;
            N >>= 1;
        }
        return result;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.