3Sum

Practice this coding question on Coding Gurukul.

Problem statement

A ledger contains signed adjustments. Find every distinct group of three values drawn from three different positions that balances to zero. Different selections of positions producing the same three values count once. Sort each triple and list the triples in lexicographic order.

Input format

First line: n. Second line: n space-separated integers.

Output format

Print the number of distinct triples, followed by one sorted triple per line in lexicographic order. Print only 0 if none exist.

Constraints

3 <= n <= 3000; -100000 <= value <= 100000.

Sample input

6
-3 0 1 2 -1 -2

Sample output

3
-3 1 2
-2 0 2
-1 0 1

Sign in to run or submit a solution in C++, Java or Python.