Algorithm


Problem Name: 693. Binary Number with Alternating Bits

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

 

Example 1:

Input: n = 5
Output: true
Explanation: The binary representation of 5 is: 101

Example 2:

Input: n = 7
Output: false
Explanation: The binary representation of 7 is: 111.

Example 3:

Input: n = 11
Output: false
Explanation: The binary representation of 11 is: 1011.

 

Constraints:

  • 1 <= n <= 231 - 1

Code Examples

#1 Code Example with C Programming

Code - C Programming


unsigned int first_bit_on(unsigned int k, unsigned int sz) {
    unsigned int m = sz / 2;
    if (k == 1) return 1;
    
    if ((k >> m) == 0) {
        return first_bit_on(k, m);
    }
    
    return m + first_bit_on(k >> m, m);
}

bool hasAlternatingBits(int n){
    unsigned int k = n;
    unsigned int b = first_bit_on(n, 32);
    //printf("%d\n", b);
    return (k ^ (k >> 1)) == ((unsigned int)1 << b) - 1 ? true : false;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 5

Output

x
+
cmd
true

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    bool hasAlternatingBits(int n) {
        while(n)
            if(!((n & 1) ^ ((n >> 1) & 1))) return false;
            else n >>= 1;
        return true;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 5

Output

x
+
cmd
true

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean hasAlternatingBits(int n) {
    Integer prev = null;
    while (n > 0) {
      int rem = n % 2;
      n /= 2;
      if (prev == null || prev != rem) {
        prev = rem;
      } else {
        return false;
      }
    }
    return true;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 7

Output

x
+
cmd
false

#4 Code Example with Javascript Programming

Code - Javascript Programming


const hasAlternatingBits = function(n) {
  const str = bin(n);
  for (let i = 1, prev = str.charAt(0); i  <  str.length; i++) {
    if (str.charAt(i) === prev) {
      return false;
    }
    prev = str.charAt(i);
  }
  return true;
};

function bin(num) {
  return (num >>> 0).toString(2);
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 7

Output

x
+
cmd
false

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        return all(a != b for a, b in zip(bin(n)[2:], bin(n)[3:]))
            
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 11

Output

x
+
cmd
false

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0693_BinaryNumberWithAlternatingBits
    {
        public bool HasAlternatingBits(int n)
        {
            var prev = n & 1;
            n >>= 1;
            while (n > 0)
            {
                var current = n & 1;
                if (current == prev) return false;
                prev = current;
                n >>= 1;
            }
            return true;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 11

Output

x
+
cmd
false
Advertisements

Demonstration


Previous
#692 Leetcode Top K Frequent Words Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#695 Leetcode Max Area of Island Solution in C, C++, Java, JavaScript, Python, C# Leetcode