Triplet Sum in Array
Sort the array. Fix arr[i] as the smallest element of the triplet. In the part to its right, put left at the start and right at the end. If arr[left] + arr[right] is smaller than need, move left right to get a bigger sum. If it is bigger, move right left. If it equals need, we found a triplet.
For a fixed i, a pair (left, right) is the answer when arr[left] + arr[right] == need. Because the range is sorted, a sum that is too small means arr[left] is useless, and a sum that is too big means arr[right] is useless.
Problem
You are given an array of integers arr and a number target.
Can you pick three elements at three different positions whose sum is exactly target? Return true if such a triplet exists, otherwise false.
The same position can't be used twice, but two positions may hold the same value.
An integer array arr of length n, and an integer target.
true if some arr[i] + arr[j] + arr[k] == target with i, j, k all different, else false. (Function: bool hasTripletSum(vector<int>& arr, int target).)
1 + 4 + 8 = 13.
Several triplets work, for example 1 + 3 + 6 and 1 + 2 + 7. We only need to say that one exists.
No three of these numbers add up to 24.
- • 3 ≤ n ≤ 10^3
- • 1 ≤ arr[i] ≤ 10^5
- • 1 ≤ target ≤ 3·10^5
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Does the order of the elements matter for the answer, or only which three I pick?
- 2.
If I already decided what the first number is, what is left to solve?
- 3.
Have I solved a "find two numbers with a given sum" problem before? What did it need?
- 4.
Would sorting the array change the answer? Would it make anything easier?
- 5.
Can I return as soon as I find one triplet, or do I need all of them?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Three nested loops over i < j < k | O(N³) | O(1) |
| Better | Fix arr[i]; find a pair summing to target − arr[i] using a set | O(N²) | O(N) |
| Optimal | Sort; fix arr[i]; move left/right inwards to find the pair | O(N²) | O(1) |
Why does this belong to this pattern?
- “three elements”Three unknowns → fix one of them in a loop, leaving a pair problem.
- “sum equal to target”A pair with a known sum → Two Sum. In a sorted array that is opposite-ends two pointers.
- “return true / false”Order and positions don't matter for the answer → we are free to sort.
This is the anchor of the k-sum family. The one idea to take away: fix one element, and the rest is Two Sum II on the part to its right. Here we only need yes/no, so there is nothing to deduplicate. The next questions keep this exact loop and add one thing each: collecting every unique triplet (3Sum), tracking the closest sum (3Sum Closest), counting instead of finding (Valid Triangle Number), fixing two elements (4Sum), and counting with repeated values (3Sum With Multiplicity).
Don't make this mistake
Two pointers works here because we only return true/false, so sorting is harmless. Plain Two Sum (LC 1) asks for the indices of the pair in an unsorted array. Sorting would scramble those indices, so Two Sum is a hash-map problem, not a two-pointer one. Before sorting, always ask: does the answer depend on the original positions?
If left starts at 0, it can land on i itself, so arr[i] gets counted twice. For arr = [2, 5, 9], target = 9, fixing 2 needs a pair summing to 7, and left = 0 finds 2 + 5 — so it wrongly answers true using the same 2 twice. Searching only to the right of i also avoids checking the same triplet in a different order.
The pointer moves are only safe in a sorted array. Without sorting, sum < need tells you nothing about which element to drop, and the search silently misses triplets.
When left == right both pointers sit on one element. Adding it to itself uses one position twice, which the problem does not allow.
Want to implement this?
Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.