Count Triplets Summing to Zero

VERY HARD · C · 5000ms

For a deeper audit, the committee wants to know how many GROUPS of three marks sum to exactly zero after marks were normalized around the class average (so some values are negative). Read N integers. Count the number of index triplets (i<j<k) such that a[i]+a[j]+a[k] = 0. Aim for O(N^2) using sorting + two pointers, not O(N^3).

Input

Line1: N. Line2: N integers.

Output

Count of triplets summing to zero.

Constraints

1 ≤ N ≤ 3000, |a_i| ≤ 10^4

Sample Input

6
-1 0 1 2 -1 -4

Sample Output

2
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Count Triplets Summing to Zero · DIU ContestHub