add codeforces solutions folder

This commit is contained in:
2026-03-05 19:55:52 +05:30
parent 77ff815f38
commit e698c35654
20 changed files with 778 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
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
+10
View File
@@ -0,0 +1,10 @@
class Solution{
public :
bool hasAlternatingBits(int n){
int m = n^(n>>1);
return (m&(m+1)==0);
}
}
// TC & SC : o(1)
// n>>1 inverts all bits and XOR makes m have all set bits if n had alternating bits. (m+1) makes all bits of m as zero and m&(m+1) will be zero