A diagnostic rig recorded a drift value for each of n sensors mounted in a fixed line along a calibration rail, given as an array nums of length n. An engineer wants to split the line at some point into a front group (the sensors at indices 0..i) and a back group (the sensors at indices i+1..n-1}), with both groups non-empty, so 0 <= i <= n-2. A split index i is called balanced if the front group's total drift minus the back group's total drift is an even integer. Count how many of the n-1 possible split indices are balanced.
The first line contains a single integer n, the number of sensors.
The second line contains n space-separated integers nums[0], nums[1], ..., nums[n-1], the drift value of each sensor.
A single integer: the number of balanced split indices.
Example 1
Input
4 10 4 8 3
Expected
0
Explanation
The 3 possible split indices give: i=0 -> front=10, back=15, diff=-5 (odd); i=1 -> front=14, back=11, diff=3 (odd); i=2 -> front=22, back=3, diff=19 (odd). None are balanced, so the answer is 0.
Example 2
Input
4 2 4 6 8
Expected
3
Explanation
The 3 possible split indices give: i=0 -> front=2, back=18, diff=-16 (even); i=1 -> front=6, back=14, diff=-8 (even); i=2 -> front=12, back=8, diff=4 (even). All 3 are balanced, so the answer is 3.
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 →