#C6362. Triplets Summing to Zero

    ID: 50114 Type: Default 1000ms 256MiB

Triplets Summing to Zero

Triplets Summing to Zero

You are given an array of integers. Your task is to find the number of unique triplets \((i, j, k)\) that satisfy the conditions:

\(0 \le i < j < k < n\) and \(nums[i] + nums[j] + nums[k] = 0\).

Note that a triplet is considered unique based on the values, not the indices, and duplicate triplets should not be counted more than once.

Example:

Input: nums = [-1, 0, 1, 2, -1, -4]
Output: 2
Explanation: The two unique triplets are [-1, -1, 2] and [-1, 0, 1].

Solve this problem using an efficient algorithm (for example by sorting the array and using the two pointers technique).

inputFormat

The first line of input contains a single integer \(n\) representing the number of elements in the array.

The second line contains \(n\) space-separated integers representing the array \(nums\).

If \(n = 0\), the second line may be empty.

outputFormat

Output a single integer representing the number of unique triplets \((i, j, k)\) such that \(nums[i] + nums[j] + nums[k] = 0\).

## sample
6
-1 0 1 2 -1 -4
2

</p>