#K71442. Unique Quadruple Sum Count

    ID: 33532 Type: Default 1000ms 256MiB

Unique Quadruple Sum Count

Unique Quadruple Sum Count

You are given an array of integers and a target value \(T\). Your task is to count the number of unique quadruples \((a, b, c, d)\) from the array such that:

[ a + b + c + d = T ]

Two quadruples are considered the same if they consist of the same four numbers (regardless of their order). The answer should be the number of distinct quadruples which satisfy this condition.

Example: For the array [1, 0, -1, 0, -2, 2] with target \(T = 0\), there are 3 unique quadruples that sum to 0:

  • [-2, -1, 1, 2]
  • [-2, 0, 0, 2]
  • [-1, 0, 0, 1]

Design an efficient algorithm to solve the problem.

inputFormat

The input is given via standard input (stdin) with the following format:

  • The first line contains a single integer \(n\), the number of elements in the array.
  • The second line contains \(n\) space-separated integers representing the elements of the array.
  • The third line contains an integer \(T\), the target sum value.

outputFormat

Print a single integer to standard output (stdout) representing the count of unique quadruples that sum up to the target (T).## sample

6
1 0 -1 0 -2 2
0
3