120. Triplets Summing to Zero
Challenge1000 ms256 MBSolved by 0%
Print how many triples of positions (i, j, l) with i < j < l satisfy
a[i] + a[j] + a[l] = 0.
Triples are counted by position, so repeated values produce separate triples.
Constraints - `1 ≤ n ≤ 3000` - `-1000000 ≤ a[i] ≤ 1000000`
Input
The first line contains an integer n.
The second line contains n space-separated integers.
Output
Print one integer — how many triples of positions i < j < l sum to zero.
Input5
-1 0 1 2 -1
Output3
Notehas three qualifying triples, two of which use a repeated value
Input4
1 2 3 4
Output0
Noteis entirely positive, so no triple can reach zero
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution