class Solution {
static int findPosition(int N) {
if((N&(N-1)) != 0) return -1;
int pos = 1;
while(N!=0){
if((N&1) == 1){
return pos;
} else {
pos++;
N>>=1;
}
}
return -1; }
};
In this code, if the N is 5, then in while loop, (5&1) == 1 which satisfy the condition and it suppose to return 1, but the code is returning -1. Please explain where exactly I'm missing the concept
Here it checks wether the value is power of 2 or not and 5 is not a power of 2 thats why it returns -1