A chain of n signal relays is wired in a fixed sequence. Each relay holds a single bit, either 0 or 1, and the chain transmits its bits starting from the first relay (the most significant bit) down to the last relay (the least significant bit), exactly like reading a binary number from left to right. Given the bits held by the relays in that order, compute the decimal integer the chain represents.
The first line contains one integer n, the number of relays in the chain. The second line contains n space-separated bits (each 0 or 1), listed from the first relay to the last relay.
A single integer: the decimal value represented by the bit chain.
Example 1
Input
3 1 0 1
Expected
5
Explanation
Reading the bits from the first relay to the last gives binary 101, which equals 1*4 + 0*2 + 1*1 = 5.
Example 2
Input
1 0
Expected
0
Explanation
A single relay holding 0 represents the binary number 0, which is decimal 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 →