Skip to main content

Practice · 3 of 5

Small-N Triple

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** Count triples (i, j, k), i < j < k, whose sum is exactly S. Constraints are small enough for O(n^3) — but O(n^2) with a hash set also works. **Input:** Line 1: n S (1 <= n <= 300, |S| <= 10^15). Line 2: n integers (|a[i]| <= 10^9). **Output:** One integer — the number of triples. **Example:** 5 10 / 1 2 3 4 5 -> 2 (1+4+5, 2+3+5).

Back to lesson: Practice: Contest Technique Problem Set