Make Nonzero Sum (easy version) (Codeforces 1754C1)
Tags: Others, constructive algorithms

This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved.You are given an array $$$[a_1, a_2, \ldots a_n]$$$ consisting of integers $$$-1$$$ and $$$1$$$. You have to build a partition of this array into the set of segments $$$[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$$$ with the following property:  Denote the alternating sum of all elements of the $$$i$$$-th segment as $$$s_i$$$: $$$s_i$$$ = $$$a_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + \ldots \pm a_{r_i}$$$. For example, the alternating sum of elements of segment $$$[2, 4]$$$ in array $$$[1, 0, -1, 1, 1]$$$ equals to $$$0 - (-1) + 1 = 2$$$.  The sum of $$$s_i$$$ over all segments of partition should be equal to zero. Note that each $$$s_i$$$ does not have to be equal to zero, this property is about sum of $$$s_i$$$ over all segments of partition.The set of segments $$$[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$$$ is called a partition of the array $$$a$$$ of length $$$n$$$ if $$$1 = l_1 \le r_1, l_2 \le r_2, \ldots, l_k \le r_k = n$$$ and $$$r_i + 1 = l_{i+1}$$$ for all $$$i = 1, 2, \ldots k-1$$$. In other words, each element of the array must belong to exactly one segment.You have to build a partition of the given array with properties described above or determine that such partition does not exist.Note that it is not required to minimize the number of segments in the partition.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows.The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the length of the array $$$a$$$.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$-1$$$ or $$$1$$$) — the elements of the given array.It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.

Output: For each test case, if required partition does not exist, print $$$-1$$$. Otherwise, print an integer $$$k$$$ — the number of segments in the partition. Then in the $$$i$$$-th of the following $$$k$$$ lines print two integers $$$l_i$$$ and $$$r_i$$$ — description of the $$$i$$$-th segment. The following conditions should be satisfied:  $$$l_i \le r_i$$$ for each $$$i$$$ from $$$1$$$ to $$$k$$$.  $$$l_{i + 1} = r_i + 1$$$ for each $$$i$$$ from $$$1$$$ to $$$(k - 1)$$$.  $$$l_1 = 1, r_k = n$$$. If there are multiple correct partitions of the array, print any of them.

Note: In the first test case we can build a partition of one segment of length $$$4$$$. The sum of this segment will be equal to $$$1 - 1 + 1 - 1 = 0$$$.In the second test case we can build a partition of two segments of length $$$3$$$. The sum of the first segment will be equal to $$$-1 -1 + 1 = -1$$$, and the sum of the second segment: $$$1 - 1 + 1 = 1$$$. So, the total sum will be equal to $$$-1 + 1 = 0$$$.In the third and in the fourth test cases it can be proved that there are no required partition.

Sample Inputs:
4
4
1 1 1 1
6
-1 1 1 1 1 1
3
1 -1 1
1
1

Sample Outputs:
1
1 4
2
1 3
4 6
-1
-1

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def div(As, l, r, ans: list):
    la = As[l]
    ra = As[r]
    m = (la + ra) // 2
    for i in range(l, r):
        if As[i] == m:
            idx = i
            break
    if m % 2 == 1:
        ans.append((l + 1, idx))
        ans.append((idx + 1, r))
    else:
        div(As, l, idx, ans)
        div(As, idx, r, ans)
T = int(input())
for t in range(T):
    N = int(input())
    A = list(map(int, input().split()))
    if N % 2 == 1:
        print(-1)
        continue
    s = 0
    As = [0]
    si = 1
    for a in A:
        s += a * si
        si *= -1
        As.append(s)
    if As[-1] == 0:
        print(1)
        print(1, N)
        continue
    tgt = As[-1] // 2
    ans = []
    div(As, 0, N, ans)
    print(len(ans))
    for (l, r) in ans:
        print(l, r)

Solution (Python):
a = int(input())
for i in range(a):
    q = int(input())
    w = list(map(int, input().split()))
    if q % 2 == 1:
        print('-1')
    else:
        t = 1
        s = 0
        p = []
        for j in range(q):
            s += t * w[j]
            t = -t
        t = -1
        s1 = 0
        err = True
        while s != 0:
            t += 1
            s1 += w[t]
            if s1 * abs(s) == s:
                p.append(t + 2)
                if err:
                    s = -int(s / abs(s)) * (abs(s) - 2)
                else:
                    s = -s
                err = not err
                s1 = 0
            else:
                s1 = s1 - w[t + 1]
                t += 1
        p.append(len(w) + 1)
        print(len(p))
        for i in range(len(p)):
            if i == 0:
                print('1', p[i] - 1)
            else:
                print(p[i - 1], p[i] - 1)

Solution (Python):
def solve():
    n = int(input())
    arr = [int(e) for e in input().split()]
    if n % 2 == 1:
        print('-1')
        return
    k = 0
    res = []
    for i in range(0, n, 2):
        if arr[i] != arr[i + 1]:
            res.append((i, i))
            res.append((i + 1, i + 1))
            k += 2
        else:
            res.append((i, i + 1))
            k += 1
    print(k)
    for i in res:
        print(f'{i[0] + 1} {i[1] + 1}')
t = int(input())
[solve() for _ in range(t)]