Skip to content

342. Power of Four #2056

Answered by mah-shamim
mah-shamim asked this question in Q&A
Aug 15, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to determine if a given integer n is a power of four. An integer n is a power of four if there exists an integer x such that n = 4x. The solution should efficiently check this condition without using loops or recursion.

Approach

  1. Check for Positive and Non-zero: Since powers of four are always positive, any non-positive n (i.e., n <= 0) immediately returns false.
  2. Check Power of Two: A number that is a power of four must also be a power of two. This can be verified using the bitwise operation (n & (n - 1)) == 0. If this condition fails, n is not a power of two, hence not a power of four.
  3. Check Even Bit Position: For a number to be a power of four, its single set bit in binary repre…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Aug 15, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Aug 15, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants