Files
dsa-competitive-programming/leetcode/lc693/AlternatingCompare.cpp
T

16 lines
486 B
C++

class Solution{
public :
bool hasAlternatingBits(int n){
bool cur = n%2;
n/=2;
while(n){
if(n%2 == cur) return false;
cur = n%2;
n/=2;
}
return true;
}
};
// n%2 extracts the last bit and n/=2 extracts the rest of the bits, now if last bit of the rest of the bits is equal to the current last bit, then two adjacent bits are same and hence we return false