Continue learning
Practice related DSA questions in the same topic.
Return 1 if target is two raised to a nonnegative integer power and 0 otherwise. The values list is unused.
Return 1 if target is two raised to a nonnegative integer power and 0 otherwise. The values list is unused.
def solve(values, target): if (target <= 0): return 0 remaining = (target & (target - 1)) if (remaining == 0): return 1 return 0Empty array
Reject a nonpositive target before inspecting bits.
Reject a nonpositive target before inspecting bits.
A binary shelf label for a power of two lights exactly one switch.
A positive power of two has exactly one set bit. Clearing that bit produces zero.
O(1) for fixed-width inputs time · O(1) auxiliary space
Worked example · {"values":[],"target":16} ⇒ 1
Edge case 1 · {"values":[],"target":0} ⇒ 0
Edge case 2 · {"values":[],"target":12} ⇒ 0
Edge case 3 · {"values":[],"target":-2} ⇒ 0
Zero also satisfies the mask equality and needs a separate positivity test.
How would you recognize a power of four?