Power of Two Easy 0 attempts
LeetCode ↗

Power of Two

Easy MatrixBFSDFS LeetCode

You get an integer n. Decide whether n is a power of two: there must exist some integer k >= 0 with n = 2^k. Return true or false.

Powers of two are 1, 2, 4, 8, .... Zero and negative integers are not powers of two in the usual sense for this problem.

Example 1

  • Input: n = 1
  • Output: true (since 2^0 = 1)

Example 2

  • Input: n = 16
  • Output: true (since 2^4 = 16)

Example 3

  • Input: n = 3
  • Output: false

Constraints

  • -2^31 <= n <= 2^31 - 1
Sample Input
Sample Output
Constraints
  • -2^31 <= n <= 2^31 - 1
Test Cases
Case 1
Args: [1] Expected: true
Case 2
Args: [16] Expected: true
Case 3
Args: [3] Expected: false
Topics

Bit Manipulation

function isPowerOfTwo(n) {
  return n > 0 && (n & (n - 1)) === 0;
}

Time: O(1) | Space: O(1)

Saved in this browser only. Private to you.

JavaScript