πŸ—“οΈ Day 8: Two Pointers Pattern

🧠 Concept Deep Dive

The Two Pointers technique is great when dealing with sorted arrays, linked lists, or problems involving pairs, subarrays, or palindromes. It uses two pointers that move at different speeds or from different directions.

πŸ”Ή Typical Use Cases:

  • Removing duplicates from a sorted array
  • Checking for a pair that sums to a target
  • Reversing parts of arrays or strings
  • Merging sorted arrays

πŸ”Ή Strategies:

  1. Start + End:

    • One pointer at the beginning, one at the end
    • Used for target sum, palindrome checks
  2. Fast + Slow:

    • Fast pointer moves faster than slow
    • Used to detect cycles, remove duplicates
  3. Sliding Synchronization:

    • Both pointers move to track ranges or match conditions

πŸ“– Example 1: Pair With Target Sum (Sorted Array)

Problem:

Given a sorted array, find two numbers that add up to a target.

Approach:

  • Use left and right pointers
  • If sum is too big, move right leftward
  • If sum is too small, move left rightward
function twoSumSorted(nums: number[], target: number): number[] {
  let left = 0;
  let right = nums.length - 1;

  while (left < right) {
    const sum = nums[left] + nums[right];

    if (sum === target) return [left, right];
    else if (sum < target) left++;
    else right--;
  }

  return [-1, -1];
}

// Test
console.log(twoSumSorted([1, 2, 4, 6, 10], 8)); // Output: [1, 3]

Visualization:

[1, 2, 4, 6, 10]
 ^           ^
left       right => sum = 11, too big
 ^        ^
left     right => sum = 8 βœ…

πŸ“– Example 2: Remove Duplicates (In-place)

Problem:

Remove duplicates from sorted array in-place.

Approach:

  • Slow pointer marks position for unique elements
  • Fast pointer scans ahead
function removeDuplicates(nums: number[]): number {
  if (nums.length === 0) return 0;

  let slow = 1;

  for (let fast = 1; fast < nums.length; fast++) {
    if (nums[fast] !== nums[fast - 1]) {
      nums[slow] = nums[fast];
      slow++;
    }
  }

  return slow;
}

// Test
const arr = [1, 1, 2, 2, 3];
console.log(removeDuplicates(arr)); // Output: 3
console.log(arr.slice(0, 3)); // [1, 2, 3]

Visualization:

[1, 1, 2, 2, 3]
     ^  ^
   slow fast

🚨 Real-world Analogy

Imagine two friends searching for a meeting spot:

  • One starts from the entrance (left)
  • The other from the back door (right) They keep walking until they meet at the right location β€” perfect for balanced conditions or symmetric data.

πŸ“ Big O Complexity

Operation Time Space
Pair sum (sorted) O(n) O(1)
Remove duplicates O(n) O(1)

πŸ§ͺ LeetCode-style Homework

Problem: Squares of a Sorted Array

Function Signature:

function sortedSquares(nums: number[]): number[];

Input:

[-4, -1, 0, 3, 10]

Output:

[0, 1, 9, 16, 100]

Hint: Start with two pointers on both ends and fill result from the back.


πŸ“† Summary

  • Two pointers = elegant way to scan arrays
  • Best for sorted data or symmetric conditions
  • Reduces nested loops into linear scans

πŸ”¬ Up Next

▢️ Day 8: Fast & Slow Pointers (Tortoise and Hare)

This page was last edited on 2025-07-31 20:50

Powered by Wiki|Docs

This page was last edited on 2025-07-31 20:50

Tri Nguyen
No Copyright

Powered by Wiki|Docs