๐Ÿ” Two Pointer Problems


Welcome to the Two Pointer section of JDCodebase!

The two-pointer technique is one of the most powerful tools to reduce time complexity in array and string problems. It helps avoid nested loops and makes your solution more efficient.

๐Ÿ› ๏ธ Basic Approach

- Start with two pointers, usually at the beginning and end of the array (or both at the beginning).
- Move the pointers based on conditions, narrowing the problem space.
- Continue until the pointers meet or cross.

๐Ÿ“š Common Patterns

  • Opposite Direction: Start one pointer from the beginning and the other from the end. Useful for problems like reversing, finding pairs, etc.
  • Same Direction: Both pointers start from the beginning and one lags behind the other. This is common in sliding window problems.
  • Nested Pointer Logic: One pointer iterates and another explores ahead. Used in problems like merging arrays, removing duplicates, etc.

๐Ÿ“ Problems List

  1. Squares of a Sorted Array (#977)
  2. Two Sum II - Input array is sorted (#167)
  3. Merge Sorted Array (#88)
  4. Move Zeroes (#283)
  5. Remove Duplicates from Sorted Array (#26)
  6. Remove Element (#27)
  7. Sort Colors (#75)
  8. Reverse String (#344)
  9. Valid Palindrome (#125)
  10. String Compression (#443)
  11. Is Subsequence (#392)
  12. Merge Strings Alternately (#1768)
  13. Find the Index of the First Occurrence in a String (#28)
  14. Reverse Vowels of a String (#345)
  15. Reverse String II (#541)
  16. Reverse Words in a String III (#557)
  17. Count Binary Substrings (#696)
  18. Shortest Distance to a Character (#821)
  19. Sort Array by Parity (#905)
  20. Reverse Only Letters (#917)
  21. Longest Harmonious Subsequence (#594)