By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You have a sorted array and need to find pairs of elements that meet some condition—like summing to a target. A naive approach uses nested loops and runs in O(n²). Two pointers lets you solve this in O(n) by starting at opposite ends and moving inward based on what you learn from each comparison.
Place one pointer at the start of the array and one at the end. Compare the values at both positions. If the comparison satisfies your condition, you've found a candidate. If not, move one pointer inward—which one depends on your problem logic.
The key insight is that the array is sorted. This means moving a pointer has predictable consequences. If your sum is too small, moving the left pointer right gives you a larger sum. If it's too large, moving the right pointer left gives you a smaller sum. You never need to backtrack.
Here's the template for finding two numbers that add up to a target:
def two_sum_sorted(nums, target):
left, right = 0, len(nums) - 1
while left < right:
s = nums[left] + nums[right]
if s == target:
return [left, right]
elif s < target:
left += 1 # need larger sum
else:
right -= 1 # need smaller sum
return []Start with left at index 0 and right at the last index. Calculate the sum. If it equals the target, return the indices. If the sum is too small, increment left to try a larger value. If it's too large, decrement right to try a smaller value. Continue until the pointers meet or you find your answer.
Two pointers also works for checking whether a string is a palindrome. The approach is similar: compare characters at opposite ends and move inward.
def is_palindrome(s):
# Filter to alphanumeric, lowercase
cleaned = [c.lower() for c in s if c.isalnum()]
left, right = 0, len(cleaned) - 1
while left < right:
if cleaned[left] != cleaned[right]:
return False
left += 1
right -= 1
return True
print(is_palindrome("A man, a plan, a canal: Panama")) # TrueNotice the preprocessing step. The code filters to only alphanumeric characters and converts to lowercase. This handles real-world strings with punctuation and mixed case. Once cleaned, the pointer logic is straightforward: if characters at left and right don't match, it's not a palindrome. Otherwise, move both pointers inward until they cross.
Two pointers transforms problems that seem to require O(n²) time into O(n) solutions. You avoid nested loops entirely. The space cost is minimal—just two integer indices. This pattern appears in many interview problems and real systems that need to process sorted data efficiently.
When to use two pointers: the array is sorted (or can be), and you're looking for pairs/triplets that satisfy some condition.
Beyond pairs, you can extend this to three-sum problems by nesting two pointers inside a loop. You can also use two pointers on linked lists to find cycles or the middle element. The core principle remains: leverage order to eliminate redundant comparisons.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.