The Two Pointer technique is one of the most intuitive yet powerful problem-solving strategies used in competitive programming and system design questions involving arrays, strings, and linked lists.

In this blog, we’ll break it down step by step with examples, Java code, and interview patterns.


🧠 What is the Two Pointer Technique?

The idea is simple:

Use two pointers (indices) to iterate through a data structure — usually starting from either end or both ends — and move them based on conditions to optimize the solution.


🔥 When to Use It?

  • When the problem asks for pairs or subarrays meeting a condition
  • To reduce nested loops (O(n²) → O(n))
  • Works best on sorted arrays or strings

✨ Common Two Pointer Patterns

Pattern Use Case
Opposite Ends Sorted arrays, pairs with sum
Sliding Window Longest substring, subarrays
Same Direction Merge sorted arrays, duplicates

🧪 Java Examples

📘 1. Two Sum – Sorted Array

🔧 Find two numbers that sum up to a target.

public int[] twoSumSorted(int[] nums, int target) {
    int left = 0, right = nums.length - 1;

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

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

⏱ Time: O(n)


📘 2. Reverse a String

public void reverseString(char[] s) {
    int left = 0, right = s.length - 1;

    while (left < right) {
        char temp = s[left];
        s[left++] = s[right];
        s[right--] = temp;
    }
}

📘 3. Remove Duplicates from Sorted Array

public int removeDuplicates(int[] nums) {
    int i = 0;

    for (int j = 1; j < nums.length; j++) {
        if (nums[i] != nums[j]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}

🧠 Here i is slow pointer and j is fast pointer.


📘 4. Move Zeroes to End

public void moveZeroes(int[] nums) {
    int insertPos = 0;

    for (int num : nums) {
        if (num != 0) {
            nums[insertPos++] = num;
        }
    }

    while (insertPos < nums.length) {
        nums[insertPos++] = 0;
    }
}

📘 5. Container With Most Water

🔧 Find the maximum area of water that can be contained between two lines.

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

    while (left < right) {
        int h = Math.min(height[left], height[right]);
        int area = h * (right - left);
        max = Math.max(max, area);

        if (height[left] < height[right]) left++;
        else right--;
    }
    return max;
}

📘 6. Is Palindrome?

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

    while (left < right) {
        if (s.charAt(left) != s.charAt(right))
            return false;
        left++;
        right--;
    }
    return true;
}

💡 Trickier Problems You Can Solve

Problem Idea
3Sum Sort + fix one + two-pointer
Longest Substring Without Repeating Characters Sliding window (variant)
Merge Intervals Same direction pointers
Dutch National Flag Three pointers

🧮 Comparison Table

Approach Time Complexity Space Complexity
Brute-force O(n²) O(1)
Two Pointers O(n) O(1)

✅ Key Takeaways

  • Two pointers help optimize brute-force O(n²) approaches to O(n).
  • Works great on sorted arrays, strings, and linked lists.
  • Variants include sliding window, fast-slow pointers, and three pointers.

🎯 Final Words

Mastering the Two Pointer technique will level up your coding game — especially in interviews. Pair it with other concepts like sorting, hash maps, and prefix sums for explosive problem-solving combos.