A control panel holds n relay banks. The state of one bank is encoded as a non-negative integer whose binary representation lists which switches are closed (on): a 1 bit means that switch is on, a 0 bit means it is off.
Count the total number of on-switches across all n banks (that is, the total number of 1 bits over all the given integers).
Line 1: an integer n.
Line 2: n space-separated non-negative integers, the encoded bank states.
A single integer: the total number of set (1) bits across all n values.
Example 1
Input
3 5 3 8
Expected
5
Explanation
5 is 101 (2 on-switches), 3 is 11 (2 on-switches), 8 is 1000 (1 on-switch): 2 + 2 + 1 = 5.
Example 2
Input
1 0
Expected
0
Explanation
A single bank with no switches on contributes 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →