ORIGINAL CODING QUESTION
Power-of-two batch sizes
A build system prefers batch sizes that are exact powers of two (1, 2, 4, 8 and so on). Count how many of the n proposed batch sizes are powers of two. Zero and negative sizes never qualify.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
First integer n, then n integers. Print one integer.
Constraints
1 ≤ n ≤ 200; -1000 ≤ size ≤ 1000.
Example 1
Input
6 1 2 3 4 6 8
Output
4
The sizes 1, 2, 4 and 8 are powers of two.
Example 2
Input
3 0 -2 -4
Output
0
Zero and negative sizes never count.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
A positive power of two has exactly one bit set in binary.
Hint 2
Subtracting 1 from such a number flips that bit and sets all lower bits.
Hint 3
A positive x is a power of two exactly when (x & (x - 1)) equals 0.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →