Power of Four - Problem

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four if there exists an integer x such that n == 4x.

Input & Output

Example 1 — Power of Four
$ Input: n = 16
Output: true
💡 Note: 16 = 4², so it is a power of four
Example 2 — Not a Power of Four
$ Input: n = 5
Output: false
💡 Note: 5 cannot be expressed as 4^x for any integer x
Example 3 — Edge Case
$ Input: n = 1
Output: true
💡 Note: 1 = 4⁰, so it is a power of four

Constraints

  • -2³¹ ≤ n ≤ 2³¹ - 1

Visualization

Tap to expand
Power of Four: Pattern RecognitionPowers of 4: 1, 4, 16, 64, 256, ...Input Examplesn = 16n = 5n = 1Check Methods1. Divide by 4 repeatedly2. Use logarithms3. Bit manipulationResults16 → true5 → false1 → truePowers of 4 in binary: 1(1), 4(100), 16(10000), 64(1000000)Notice: Each has exactly one bit set at positions 0, 2, 4, 6...
Understanding the Visualization
1
Input
Given integer n
2
Check Pattern
Verify if n = 4^x for some integer x
3
Output
Return true if it's a power of four, false otherwise
Key Takeaway
🎯 Key Insight: Powers of 4 have exactly one bit set at even positions in binary
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
180.0K Views
Medium Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen