Special Elements (Codeforces 1352E)
Tags: Implement, two pointers

Pay attention to the non-standard memory limit in this problem.In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.The array $$$a=[a_1, a_2, \ldots, a_n]$$$ ($$$1 \le a_i \le n$$$) is given. Its element $$$a_i$$$ is called special if there exists a pair of indices $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) such that $$$a_i = a_l + a_{l+1} + \ldots + a_r$$$. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).Print the number of special elements of the given array $$$a$$$.For example, if $$$n=9$$$ and $$$a=[3,1,4,1,5,9,2,6,5]$$$, then the answer is $$$5$$$:  $$$a_3=4$$$ is a special element, since $$$a_3=4=a_1+a_2=3+1$$$;  $$$a_5=5$$$ is a special element, since $$$a_5=5=a_2+a_3=1+4$$$;  $$$a_6=9$$$ is a special element, since $$$a_6=9=a_1+a_2+a_3+a_4=3+1+4+1$$$;  $$$a_8=6$$$ is a special element, since $$$a_8=6=a_2+a_3+a_4=1+4+1$$$;  $$$a_9=5$$$ is a special element, since $$$a_9=5=a_2+a_3=1+4$$$. Please note that some of the elements of the array $$$a$$$ may be equal — if several elements are equal and special, then all of them should be counted in the answer.

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow.Each test case is given in two lines. The first line contains an integer $$$n$$$ ($$$1 \le n \le 8000$$$) — the length of the array $$$a$$$. The second line contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of the values of $$$n$$$ for all test cases in the input does not exceed $$$8000$$$.

Output: Print $$$t$$$ numbers — the number of special elements for each of the given arrays.


Sample Inputs:
5
9
3 1 4 1 5 9 2 6 5
3
1 1 2
5
1 1 1 1 1
8
8 7 6 5 4 3 2 1
1
1

Sample Outputs:
5
1
0
4
0

Solution (Python):
import sys
I = lambda : [*map(int, sys.stdin.readline().split())]
(t,) = I()
for _ in range(t):
    (n,) = I()
    a = I()
    isspecial = [0] * (n + 1)
    for i in range(n - 1):
        curr = a[i] + a[i + 1]
        j = i + 1
        while j < n and curr <= n:
            isspecial[curr] = 1
            if j < n - 1:
                curr += a[j + 1]
            j += 1
    tot = 0
    for guy in a:
        if isspecial[guy]:
            tot += 1
    print(tot)

Solution (Python):
import sys
import array as arr
read = sys.stdin.readline

def run_case():
    n = int(read())
    ar = tuple(map(int, read().split()))
    mx = max(ar)
    ok = [False] * (mx + 1)
    for (i, a) in enumerate(ar):
        tmp = a
        for b in ar[i + 1:]:
            tmp += b
            if tmp <= mx:
                ok[tmp] = True
            else:
                break
    print(sum((ok[i] for i in ar)))
if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        run_case()

Solution (Python):
t = int(input())
for tidx in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    s = [0] * (n + 1)
    for i in range(n):
        s[i + 1] = s[i] + a[i]
    b = [0] * (n + 1)
    imin = 0
    for j in range(2, n + 1):
        while s[j] - s[imin] > n:
            imin += 1
        k = imin
        while k < j - 1:
            b[s[j] - s[k]] = 1
            k += 1
    ans = 0
    for i in a:
        ans += b[i]
    print(ans)