Move pointers inward from both ends of a sorted sequence.
Every pair that uses a value outside left..right has already been ruled out, so if an answer exists, it lies inside left..right.
Use it when the values are sorted, or can be sorted, and the question is about a pair. Signal words: sorted, two values, add up to, closest to, within a budget, from both ends, palindrome, reverse.
Start: left on the first index, right on the last.
Step: compare the two values with the target. Too small, move left in. Too large, move right in. Each move rules out one value for good.
Stop: when left meets right. Use left < right, so a value is never paired with itself.
Counting: when a total fits within a limit, the value at left fits with every value from left + 1 to right. Add right − left in one step.
It needs sorted values. On unsorted values, sort them first, or use a hash map when you must keep the original positions.
Cost: at most n − 1 steps, so O(n) time and O(1) extra space.
def pair_with_sum(values, target):
# [1] Start with one pointer at each end.
left = 0
right = len(values) - 1
# [2] While two different values remain, compare their total with the target.
while left < right:
total = values[left] + values[right]
# [3] An exact match: return the two indexes.
if total == target:
return [left, right]
# [4] Too small: even the largest value still in play cannot lift values[left] to the target, so rule it out.
if total < target:
left += 1
# [5] Too large: even the smallest value still in play is too much for values[right], so rule it out.
else:
right -= 1
# [6] The pointers met: no pair adds up to the target.
return [-1, -1]public int[] pairWithSum(int[] values, long target) {
// [1] Start with one pointer at each end.
int left = 0;
int right = values.length - 1;
// [2] While two different values remain, compare their total with the target.
while (left < right) {
long total = (long) values[left] + values[right]; // 64-bit, so two large values cannot overflow
// [3] An exact match: return the two indexes.
if (total == target) {
return new int[] {left, right};
}
// [4] Too small: even the largest value still in play cannot lift values[left] to the target, so rule it out.
if (total < target) {
left++;
}
// [5] Too large: even the smallest value still in play is too much for values[right], so rule it out.
else {
right--;
}
}
// [6] The pointers met: no pair adds up to the target.
return new int[] {-1, -1};
}vector<int> pairWithSum(const vector<int>& values, long long target) {
// [1] Start with one pointer at each end.
int left = 0;
int right = (int) values.size() - 1;
// [2] While two different values remain, compare their total with the target.
while (left < right) {
long long total = (long long) values[left] + values[right]; // 64-bit, so two large values cannot overflow
// [3] An exact match: return the two indexes.
if (total == target) {
return {left, right};
}
// [4] Too small: even the largest value still in play cannot lift values[left] to the target, so rule it out.
if (total < target) {
left++;
}
// [5] Too large: even the smallest value still in play is too much for values[right], so rule it out.
else {
right--;
}
}
// [6] The pointers met: no pair adds up to the target.
return {-1, -1};
}function pairWithSum(values, target) {
// [1] Start with one pointer at each end.
let left = 0;
let right = values.length - 1;
// [2] While two different values remain, compare their total with the target.
while (left < right) {
const total = values[left] + values[right];
// [3] An exact match: return the two indexes.
if (total === target) {
return [left, right];
}
// [4] Too small: even the largest value still in play cannot lift values[left] to the target, so rule it out.
if (total < target) {
left++;
}
// [5] Too large: even the smallest value still in play is too much for values[right], so rule it out.
else {
right--;
}
}
// [6] The pointers met: no pair adds up to the target.
return [-1, -1];
}def count_pairs_within(values, limit):
# [1] Start with one pointer at each end.
left = 0
right = len(values) - 1
count = 0
# [2] While two different values remain, compare their total with the limit.
while left < right:
# [3] It fits: values[left] fits with every value from left + 1 to right, so count them all.
if values[left] + values[right] <= limit:
count += right - left
left += 1
# [4] Too large: values[right] fits with no value still in play, so rule it out.
else:
right -= 1
return countpublic long countPairsWithin(int[] values, long limit) {
// [1] Start with one pointer at each end.
int left = 0;
int right = values.length - 1;
long count = 0; // 64-bit: 100,000 values have about 5 billion pairs
// [2] While two different values remain, compare their total with the limit.
while (left < right) {
// [3] It fits: values[left] fits with every value from left + 1 to right, so count them all.
if ((long) values[left] + values[right] <= limit) {
count += right - left;
left++;
}
// [4] Too large: values[right] fits with no value still in play, so rule it out.
else {
right--;
}
}
return count;
}long long countPairsWithin(const vector<int>& values, long long limit) {
// [1] Start with one pointer at each end.
int left = 0;
int right = (int) values.size() - 1;
long long count = 0; // 64-bit: 100,000 values have about 5 billion pairs
// [2] While two different values remain, compare their total with the limit.
while (left < right) {
// [3] It fits: values[left] fits with every value from left + 1 to right, so count them all.
if ((long long) values[left] + values[right] <= limit) {
count += right - left;
left++;
}
// [4] Too large: values[right] fits with no value still in play, so rule it out.
else {
right--;
}
}
return count;
}function countPairsWithin(values, limit) {
// [1] Start with one pointer at each end.
let left = 0;
let right = values.length - 1;
let count = 0; // exact up to 2^53, far above the 5 billion pairs of 100,000 values
// [2] While two different values remain, compare their total with the limit.
while (left < right) {
// [3] It fits: values[left] fits with every value from left + 1 to right, so count them all.
if (values[left] + values[right] <= limit) {
count += right - left;
left++;
}
// [4] Too large: values[right] fits with no value still in play, so rule it out.
else {
right--;
}
}
return count;
}if total < target:
right -= 1 # bug: a total that is too small rules out left, not right
else:
left += 1This version loops while left <= right.
A new hub sends its parcels in arrival order: [15, 4, 26, 31, 11, 7, 20], with 37 kg of room.
Your team lead brings a request from the loading team. Their scanner lists parcels in arrival order, [15, 4, 26, 31, 11, 7, 20], and loaders need the scanner positions of two parcels that fill 37 kg.
systemdesignmastery.dev/dsa/cheatsheets/opposite-ends