Hard · Sorted K-Sum Pointers
Understand → Visualise → Practice.
Return all unique sorted quadruplets summing to target.
Sort the array. Fix the first two values with nested loops, then find the remaining pair with two pointers, skipping equal values at every level to avoid duplicates.
Sorting enables two pointers and duplicate skipping.
Loop i and j, skipping a value equal to the previous one at the same level.
Move the pointers by comparing the sum with target; after a hit, skip equal values on both sides.
O(n³) time and O(1) extra space apart from the output.
Skipping duplicates only for i misses repeats created by j or by the pointers.
Read the full guide first: Two Pointers and Sliding Windows.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.