A diagnostic panel has a row of switches that a signal passes through, one after another, in order. Each switch is described by an integer reading: a positive reading passes the signal through unchanged, a negative reading inverts the signal's polarity, and a reading of exactly zero means that switch is broken -- it kills the signal completely, so nothing reaches the end of the panel regardless of any other switch.
Given the readings of every switch on the panel, determine the net polarity of the signal after it passes through all of them in order.
Line 1: an integer n -- the number of switches on the panel.
Line 2: n space-separated integers a[0], a[1], ..., a[n-1] -- the reading of each switch, in the order the signal passes through them.
Print a single integer:
1 if the signal exits with positive net polarity (no broken switch, and an even number of inverting switches),-1 if the signal exits with negative net polarity (no broken switch, and an odd number of inverting switches),0 if at least one switch is broken (reading exactly zero).Example 1
Input
4 -3 5 -2 7
Expected
1
Explanation
There are two negative (inverting) switches and no broken switch. Two inversions cancel out, so the signal exits positive: output 1.
Example 2
Input
3 4 0 -9
Expected
0
Explanation
The second switch has reading 0, meaning it is broken. A broken switch kills the signal regardless of the other readings, so the output is 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 →