#K45852. Equalizing Array Elements Under Parity Constraint

    ID: 27845 Type: Default 1000ms 256MiB

Equalizing Array Elements Under Parity Constraint

Equalizing Array Elements Under Parity Constraint

You are given an integer ( n ) and an array of ( n ) integers. In one operation, you can combine two adjacent elements of the array, reducing the array size by 1. However, the operation is allowed only when all elements have the same parity (i.e., all even or all odd). If the array does not have uniform parity, then it is impossible to perform the operations and you should output ( -1 ). Otherwise, if all elements have the same parity, you need exactly ( n-1 ) operations to reduce the array to a single element. Your task is to compute and output this minimum number of operations, or ( -1 ) if it is not possible.

inputFormat

The input is read from standard input (stdin). The first line contains a single integer ( n ) ( (1 \leq n \leq 10^5) ) representing the number of elements. The second line contains ( n ) space-separated integers representing the elements of the array.

outputFormat

Output a single integer on standard output (stdout) representing the minimum number of operations required. If it is not possible to perform the operations (i.e., the array does not have uniform parity), output ( -1 ).## sample

3
2 2 2
2

</p>