Fundamentals/Patterns/Two Pointers — Opposite Ends18 problems· Two Pointers

Two pointers starting at both ends, converging toward the middle

When to useSorted array, palindrome check, reverse in place, pair sum on sorted input

iterative
String Methods
string_methods
Is Palindrome
is_palindrome
Reverse String
reversing_strings
Two Pointer Templates
two_pointers_bonus
Two Pointers Introduction
two_pointers_intro
Movies on Flight
movies_on_flight
Optimal Utilization
optimal_utilization
Optimize Memory Usage
optimize_memory_usage
3Sum
three_sum
Two Sum Sorted
two_sum_sorted
Valid Palindrome
valid_palindrome
Deduplication
deduplication
Citadel Online Assessment | Triplets | Citadel OA 2022
triplets
Minimum Adjacent Swaps to Make Palindrome
min_swaps_to_make_palindrome
Amazon OA — Valid Coupons
amazon_oa_valid_coupons
Palindrome Counting
palindrome_counting
Trapping Rain Water
trapping_rain_water
Container With Most Watermedium
container_with_most_water · no slots
INITIALIZE
left=0, right=last index
let left = 0;
let right = s.length - 1;
let left = 0;
let right = s.length - 1;
const chars = Array.from(s);
let left = 0;
let right = chars.length - 1;
let left = 0;
let right = arr.length - 1;
let left = 0;
let right = arr.length - 1;
const target = d - 30;
const sorted = [...movieDuration].sort((a, b) => a - b);
let left = 0;
let right = sorted.length - 1;
sortedA ascending by value;
sortedB descending by value;
let left = 0; let right = 0;
const fg = foregroundTasks.map((m, i) => [m, i]).sort((a, b) => a[0] - b[0]);
const bg = backgroundTasks.map((m, i) => [m, i]).sort((a, b) => a[0] - b[0]);
let bestSum = -1;
const result = [];
let left = 0;
let right = bg.length - 1;
nums.sort((a, b) => a - b);
const result = [];
let left = 0;
let right = arr.length - 1;
let left = 0;
let right = s.length - 1;
nums.sort((a, b) => a - b);
let left = i + 1;
let right = nums.length - 1;
arr.sort((a, b) => a - b);
let count = 0;
const arr = s.split('');
// frequency check → if oddCount > 1 return -1
let left = 0, right = arr.length - 1;
let swaps = 0;
let left = 0;
let right = coupon.length - 1;
const n = s.length;
let count = 0;
let left = 0, right = heights.length - 1;
let leftMax = 0, rightMax = 0, total = 0;
TRAVERSE
while (left < right)
while (left <= right && s[left] === ' ') left++;
while (right >= left && s[right] === ' ') right--;
while (left < right) {
while (left < right) {
while (left < right) {
while (left < right) {
while (left < right) {
while (left < sortedA.length && right < sortedB.length) {
while (left < fg.length && right >= 0) {
for (let i = 0; i < nums.length - 2; i++) {
while (left < right) {
while (left < right) {
while (left < right) {
for (let i = 0; i < arr.length - 2; i++) {
let left = i + 1;
let right = arr.length - 1;
while (left < right) {
while (left < right) {
while (left < right) {
for (let i = 0; i < n; i++) {
while (left < right) {
[COMPARE]
compare or process arr[left] and arr[right]
for (let i = left; i <= right; i++) {
const code = s.charCodeAt(i);
result += (code >= 65 && code <= 90) ? String.fromCharCode(code + 32) : s[i];
}
if (s[left] !== s[right]) return false;
[chars[left], chars[right]] = [chars[right], chars[left]];
const sum = arr[left] + arr[right];
if (sum === target) return [left, right];
if (sum < target) left += 1;
else right -= 1;
const temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
const sum = sorted[left] + sorted[right];
if (sum > target) { right--; }
else { /* update bestPair if better */; left++; }
const sum = sortedA[left][1] + sortedB[right][1];
if (sum > target) { right++; }
else { /* collect pairs at bestSum */; left++; }
const sum = fg[left][0] + bg[right][0];
if (sum > k) { right--; }
else { if (sum > bestSum) { bestSum = sum; result.length = 0; result.push([fg[left][1], bg[right][1]]); } else if (sum === bestSum) { result.push([fg[left][1], bg[right][1]]); } left++; }
let left = i + 1; let right = nums.length - 1;
while (left < right) {
const sum = nums[i] + nums[left] + nums[right];
if (sum === 0) { result.push([nums[i], nums[left], nums[right]]); while (left < right && nums[left] === nums[left+1]) left++; while (left < right && nums[right] === nums[right-1]) right--; left++; right--; }
else if (sum < 0) { left++; } else { right--; }
}
const sum = arr[left] + arr[right];
if (sum === target) return [left, right];
if (sum < target) left += 1;
else right -= 1;
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
const sum = nums[i] + nums[left] + nums[right];
if (sum === target) { result.push([...]); left++; right--; }
else if (sum < target) { left++; }
else { right--; }
if (arr[i] + arr[left] + arr[right] <= t) {
count += right - left;
left += 1;
} else {
right -= 1;
}
if (arr[left] === arr[right]) { left++; right--; continue; }
let k = right;
while (k > left && arr[k] !== arr[left]) k--;
if (k === left) { swap(left, left+1); swaps++; }
else { bubble k to right; left++; right--; }
if (coupon[left] !== coupon[right]) { isPalin = false; break; }
expand(i, i); // odd-length palindromes
expand(i, i + 1); // even-length palindromes
if (heights[left] < heights[right]) {
leftMax = Math.max(leftMax, heights[left]);
total += leftMax - heights[left];
left += 1;
} else {
rightMax = Math.max(rightMax, heights[right]);
total += rightMax - heights[right];
right -= 1;
}
advance
move left++ or right-- based on logic
left += 1;
right -= 1;
left++;
right--;
left += 1;
right -= 1;
// left++ or right-- handled inside [COMPARE]
// left++ or right++ handled inside [COMPARE]
left += 1;
right -= 1;
// left++ or right-- handled inside [COMPARE] branch
left += 1;
right -= 1;
RETURN
final answer
return result;
return true;
return chars.join("");
return [];
return arr;
return bestPair;
return result;
return result;
return result;
return [];
return true;
return result;
return count;
return swaps;
return count;
return count;
return total;