Two Pointers Fundamentals

Loading concept...

๐ŸŽฏ Two Pointers: The Magic of Walking Together

The Story of Two Friends Exploring a Maze

Imagine you and your best friend are standing at different ends of a long hallway. You want to find something special in the middle. Instead of one person walking the ENTIRE hallway alone, you BOTH walk toward each other! You meet in the middle much faster!

Thatโ€™s the Two Pointers technique! Two helpers exploring a list from different positions, working together to solve problems FAST.


๐Ÿšถโ€โ™‚๏ธ๐Ÿšถโ€โ™€๏ธ What is Two Pointers?

Think of it like this: You have a line of toys. Instead of checking one toy at a time from the start, you put one finger at the FIRST toy and another finger at the LAST toy. Then you move your fingers based on what you find!

// Two fingers on a list
int left = 0;           // First position
int right = arr.length - 1;  // Last position

while (left < right) {
    // Move fingers toward each other
    // based on what we find!
}

Why is this magical?

Old Way (One Pointer) New Way (Two Pointers)
Check everything twice Check things once
Slow like a snail ๐ŸŒ Fast like a cheetah ๐Ÿ†
O(nยฒ) time O(n) time

โœ… Valid Palindrome: The Mirror Word Game

The Story

A palindrome is like looking in a mirror! The word reads the same forwards AND backwards.

๐Ÿชž โ€œracecarโ€ โ†’ YES! Same both ways! ๐Ÿชž โ€œhelloโ€ โ†’ NO! Different backwards!

How Two Pointers Help

Put one finger at the START, one at the END. If both letters match, move BOTH fingers toward the middle!

public boolean isPalindrome(String s) {
    int left = 0;
    int right = s.length() - 1;

    while (left < right) {
        char leftChar = s.charAt(left);
        char rightChar = s.charAt(right);

        // Skip non-letters/numbers
        if (!Character.isLetterOrDigit(leftChar)) {
            left++;
            continue;
        }
        if (!Character.isLetterOrDigit(rightChar)) {
            right--;
            continue;
        }

        // Compare (ignore uppercase/lowercase)
        if (Character.toLowerCase(leftChar)
            != Character.toLowerCase(rightChar)) {
            return false;
        }

        left++;
        right--;
    }
    return true;
}

Visual Journey

graph TD A["๐Ÿ”ค r a c e c a r"] --> B["๐Ÿ‘†left=r, right=r๐Ÿ‘†"] B --> C["โœ… Match! Move both inward"] C --> D["๐Ÿ‘†left=a, right=a๐Ÿ‘†"] D --> E["โœ… Match! Keep going..."] E --> F["๐ŸŽ‰ All matched = PALINDROME!"]

๐ŸŽฏ Three Sum Pattern: Finding the Perfect Trio

The Story

Imagine you have a bag of coins with different values (some positive, some negative). Can you pick EXACTLY 3 coins that add up to ZERO?

Example: Coins: [-1, 0, 1, 2, -1, -4]

  • Pick: -1 + 0 + 1 = 0 โœ…
  • Pick: -1 + 2 + -1 = 0 โœ…

The Magic Trick

  1. Sort the coins first (put them in order)
  2. Pick one coin (call it the โ€œanchorโ€)
  3. Use Two Pointers to find two more coins that balance it out!
public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> result = new ArrayList<>();
    Arrays.sort(nums);  // Sort first!

    for (int i = 0; i < nums.length - 2; i++) {
        // Skip duplicates
        if (i > 0 && nums[i] == nums[i-1]) {
            continue;
        }

        int left = i + 1;
        int right = nums.length - 1;
        int target = -nums[i];  // What we need

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

            if (sum == target) {
                result.add(Arrays.asList(
                    nums[i], nums[left], nums[right]
                ));
                left++;
                right--;
                // Skip duplicates
                while (left < right
                    && nums[left] == nums[left-1]) {
                    left++;
                }
            } else if (sum < target) {
                left++;   // Need bigger sum
            } else {
                right--;  // Need smaller sum
            }
        }
    }
    return result;
}

Why Sort First?

graph TD A["Unsorted: 4, -1, 2, -1, 0"] --> B["Sorted: -1, -1, 0, 2, 4"] B --> C["Pick anchor: -1"] C --> D["Two pointers find: 0 + 1 = โŒ"] D --> E["Adjust pointers smartly!"] E --> F["Find: -1 + 0 + 1 = 0 โœ…"]

๐ŸŠ Container With Most Water: The Swimming Pool Problem

The Story

You have vertical walls of different heights. You want to fill water between TWO walls. Which two walls hold the MOST water?

Think of it like this: Tall walls far apart = LOTS of water! ๐ŸŒŠ

The Rules

  • Water height = shorter wall (water spills over short ones!)
  • Water width = distance between walls
  • Area = height ร— width

Two Pointer Magic

Start with walls at the VERY ENDS (maximum width). Then move the SHORTER wall inward, hoping to find a taller one!

public int maxArea(int[] height) {
    int left = 0;
    int right = height.length - 1;
    int maxWater = 0;

    while (left < right) {
        // Calculate current water
        int h = Math.min(height[left],
                         height[right]);
        int width = right - left;
        int water = h * width;

        maxWater = Math.max(maxWater, water);

        // Move the shorter wall
        if (height[left] < height[right]) {
            left++;
        } else {
            right--;
        }
    }

    return maxWater;
}

Visual Example

Heights: [1, 8, 6, 2, 5, 4, 8, 3, 7]

    8       8
    |   6   |       7
    | 5 | 5 | 4     |
    | | | | | | 3   |
  1 | | | 2 | | | | |
  -------------------------
  0 1 2 3 4 5 6 7 8

Best: walls at index 1 and 8
Height = min(8,7) = 7
Width = 8-1 = 7
Water = 7 ร— 7 = 49 ๐Ÿ†

๐ŸŒง๏ธ Trapping Rain Water: Catching Every Drop!

The Story

After rain, water gets TRAPPED between tall buildings. How much water stays on the rooftops?

This is the HARDEST pattern - but youโ€™re ready! ๐Ÿ’ช

The Secret

For each position, water trapped = min(tallest left, tallest right) - current height

Two Pointer Approach

Track the maximum height seen from both sides as you go!

public int trap(int[] height) {
    if (height.length == 0) return 0;

    int left = 0;
    int right = height.length - 1;
    int leftMax = 0;
    int rightMax = 0;
    int water = 0;

    while (left < right) {
        if (height[left] < height[right]) {
            // Left side is limiting
            if (height[left] >= leftMax) {
                leftMax = height[left];
            } else {
                water += leftMax - height[left];
            }
            left++;
        } else {
            // Right side is limiting
            if (height[right] >= rightMax) {
                rightMax = height[right];
            } else {
                water += rightMax - height[right];
            }
            right--;
        }
    }

    return water;
}

Visual Example

Heights: [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]

        3
    2   | 2   2
  1 | 1 | | | | 1 1
  | | | | | | | | |
  0 1 2 3 4 5 6 7 8 9 10 11

Water trapped (shown as ~):
        3
    2 ~ | 2 ~ 2
  1 | 1 | | ~ | 1 1

Total water = 6 units! ๐ŸŒŠ

๐Ÿง  When to Use Two Pointers?

Ask yourself these questions:

Question If YESโ€ฆ
Is the array/string sorted? Perfect for Two Pointers!
Looking for pairs/triplets? Try Two Pointers!
Need to compare start & end? Two Pointers is your friend!
Want to avoid nested loops? Two Pointers saves the day!

๐ŸŽฎ Pattern Recognition Cheat

graph TD A["Problem Type?"] --> B{"Sorted Array?"} B -->|Yes| C["Two Sum โ†’ Two Pointers"] B -->|No| D{"Need to sort first?"} D -->|Yes| E["Three Sum โ†’ Sort + Two Pointers"] D -->|No| F{"Compare ends?"} F -->|Yes| G["Palindrome/Container"] F -->|No| H{"Track max both sides?"} H -->|Yes| I["Trapping Rain Water"]

๐Ÿ† Key Takeaways

  1. Two Pointers = Two friends exploring together - faster than one!

  2. Start positions matter:

    • Same direction: Slow & Fast pointer
    • Opposite ends: Shrinking window
  3. Movement rules:

    • Move based on comparison
    • Move the โ€œweakerโ€ pointer to find better options
  4. Time complexity: Usually O(n) - visit each element once!

  5. Space complexity: Usually O(1) - just two pointers!


๐Ÿš€ Youโ€™re Now a Two Pointer Pro!

You learned:

  • โœ… Valid Palindrome - Mirror check with two ends
  • ๐ŸŽฏ Three Sum - Anchor + two pointer search
  • ๐ŸŠ Container With Most Water - Move the shorter wall
  • ๐ŸŒง๏ธ Trapping Rain Water - Track maximums from both sides

Remember: When you see arrays and need to avoid O(nยฒ), think TWO POINTERS! Your code will run like a cheetah instead of crawling like a snail! ๐Ÿ†๐Ÿ’จ

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.