Skip to main content

Two Pointers

The two pointers technique is a powerful approach for solving array and string problems efficiently, often reducing time complexity from O(n²) to O(n).

Two sum variations

  • Two Sum: Finding two numbers that add up to target
  • Three Sum: Finding three numbers that add up to target
  • Four Sum: Extending to four numbers
  • K Sum: Generalizing to k numbers

Container with most water

  • Problem: Finding two lines that form container with maximum area
  • Approach: Two pointers from both ends, move pointer with smaller height
  • Optimization: Greedy approach with proof of correctness

Remove duplicates

  • Sorted Array: Remove duplicates from sorted array in-place
  • Unsorted Array: Remove duplicates using hash set or sorting
  • Multiple Duplicates: Handle cases with more than 2 duplicates
  • Return New Array: Creating new array without duplicates

Valid palindrome

  • String Palindrome: Check if string reads same forwards and backwards
  • Alphanumeric Only: Ignore non-alphanumeric characters
  • Case Insensitive: Handle different cases
  • Palindrome Substring: Find longest palindromic substring

Merge sorted arrays

  • Two Sorted Arrays: Merge two sorted arrays into one
  • In-place Merge: Merge without extra space
  • K Sorted Arrays: Merge multiple sorted arrays
  • Merge Sort: Using merge operation in sorting algorithm