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_methodsIs Palindrome
is_palindromeReverse String
reversing_stringsTwo Pointer Templates
two_pointers_bonusTwo Pointers Introduction
two_pointers_introMovies on Flight
movies_on_flightOptimal Utilization
optimal_utilizationOptimize Memory Usage
optimize_memory_usage3Sum
three_sumTwo Sum Sorted
two_sum_sortedValid Palindrome
valid_palindromeDeduplication
deduplicationCitadel Online Assessment | Triplets | Citadel OA 2022
tripletsMinimum Adjacent Swaps to Make Palindrome
min_swaps_to_make_palindromeAmazon OA — Valid Coupons
amazon_oa_valid_couponsPalindrome Counting
palindrome_countingTrapping Rain Water
trapping_rain_waterContainer With Most Watermedium
container_with_most_water · no slotsINITIALIZE
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 -1let 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 palindromesexpand(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;
—