Skip to content

ORIGINAL CODING QUESTION

Three-member panel totals

Each interviewer has an integer score weight. Count the ways to pick three different positions i < j < k whose weights add up exactly to the target. Equal values at different positions are different picks.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n weights, then the target. Print the number of triples.

Constraints

3 ≤ n ≤ 200; -1000 ≤ weight ≤ 1000; -3000 ≤ target ≤ 3000.

Example 1

Input

5
1 2 3 4 5
9

Output

2

The triples 1+3+5 and 2+3+4 reach 9.

Example 2

Input

4
0 0 0 0
0

Output

4

Any three of the four zeros work: 4 ways.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Checking all triples is cubic; aim for quadratic time.

Hint 2

Fix the middle position j and count the earlier positions that complete each pair (j, k).

Hint 3

For each j, loop k > j and add how many earlier values equal target - a[j] - a[k] using a frequency map; then add a[j] to the map.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →