Four Sum
Practice this coding question on Coding Gurukul.
Problem statement
Find all distinct quadruplets of values whose sum equals a target. Each quadruplet must use four distinct array positions. Repeated input values are allowed, but the same value quadruplet must appear only once. Sort each quadruplet in nondecreasing order, then list quadruplets in lexicographic order: compare first values, then second, third, and fourth.
Input format
First line: n target. Second line: n integers.
Output format
Print the number of distinct quadruplets. Then print each sorted quadruplet on its own line in lexicographic order. If none exist, print only 0.
Constraints
1 <= n <= 200; -1000000000 <= values, target <= 1000000000. Use 64-bit arithmetic for sums.
Sample input
6 0 1 0 -1 0 -2 2
Sample output
3 -2 -1 1 2 -2 0 0 2 -1 0 0 1
Sign in to run or submit a solution in C++, Java or Python.