Dsa
Bit Manipulation
Easy
The Power-of-Two Population
A 'Balanced Number' is defined as a non-negative integer whose Hamming Weight (the number of set bits in its binary representation) is itself a power of 2 (e.g., 1, 2, 4, 8, 16, 32).
Input: A line containing an integer N, followed by N integers on the next line.
Output: The sum of all 'Balanced Numbers' in the list.
Example:
Input:
3
3 7 8
Explanation:
3 in binary is '11' (popcount 2, power of 2) -> Balanced
7 in binary is '111' (popcount 3, not power of 2) -> Not Balanced
8 in binary is '1000' (popcount 1, power of 2) -> Balanced
Sum: 3 + 8 = 11.
Output: 11
Key concepts
bit_manipulationpopcount
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now