Tutorials Logic, IN info@tutorialslogic.com

Two Pointers Technique Two Sum, Palindrome

Move Two Indices With A Purpose

State what each pointer means, why it moves, and when the loop stops; without that invariant, a compact implementation is difficult to verify.

The two pointers technique uses two indices to scan a sequence without checking every pair. It works when movement rules can safely discard possibilities. Common patterns include one pointer at each end of a sorted array, slow and fast pointers in a linked list, and left/right boundaries for a sliding window.

For a sorted two-sum problem, place left at the start and right at the end. If the sum is too small, move left to increase it. If the sum is too large, move right to decrease it. The sorted property justifies discarding all pairs involving the old pointer position.

For palindrome checks, compare characters from both ends and move inward. For removing duplicates, one pointer tracks the write position while another scans. The important skill is explaining why each move is safe; without that reasoning, two pointers becomes guesswork.

  • Use two pointers when movement can discard many candidates.
  • Identify whether the input must be sorted.
  • Define what each pointer represents.
  • Explain why each movement is safe.
  • Stop when the pointers cross or the invariant is complete.

Invariants, Windows, Duplicates, and Edge Cases

Every two-pointer solution needs an invariant. In sorted two-sum, all pairs outside the current left/right window have already been proven impossible. In a sliding window, the window often represents the longest or shortest valid segment seen so far. State the invariant before coding.

Duplicates and boundary conditions change implementation details. For triplet problems, skip duplicate anchor and pointer values carefully. For strings, decide whether to ignore case, spaces, punctuation, or Unicode normalization. For arrays, handle empty input, one element, all duplicates, and negative numbers.

Two pointers often turns O(n²) brute force into O(n), but only when prerequisites hold. If the array is unsorted and sorting changes required output order, the solution may need indices preserved or a different approach such as hashing. Choose the technique from the proof, not from the topic name.

  • Write the invariant before loops.
  • Handle duplicates deliberately.
  • Preserve original indices when sorting would lose needed information.
  • Test empty, tiny, duplicate, and all-negative cases.
  • Use hashing when no safe pointer movement exists.

Two Pointers Technique Two Sum, Palindrome Example

Two Pointers Technique Two Sum, Palindrome Example
// Practice Two Pointers Technique Two Sum, Palindrome
const topic = 'Two Pointers Technique Two Sum, Palindrome';
console.log(topic);

Sorted two-sum with two pointers

The sorted order explains every pointer movement.

Sorted two-sum with two pointers
function twoSumSorted(values, target) {
  let left = 0;
  let right = values.length - 1;

  while (left < right) {
    const sum = values[left] + values[right];
    if (sum === target) return [left, right];
    if (sum < target) left++;
    else right--;
  }

  return null;
}
  • This returns positions in the sorted array.
  • If original indices matter, store them before sorting.
  • The movement rule depends on ascending order.

Palindrome check with cleanup

Two pointers compare meaningful characters from both ends.

Palindrome check with cleanup
function isLoosePalindrome(text) {
  const clean = text.toLowerCase().replace(/[^a-z0-9]/g, "");
  let left = 0;
  let right = clean.length - 1;

  while (left < right) {
    if (clean[left] !== clean[right]) return false;
    left++;
    right--;
  }

  return true;
}
  • This simple cleanup is ASCII-oriented.
  • For international text, Unicode normalization rules matter.
  • The loop stops once every mirrored pair has been checked.
Before you move on

Two Pointers Technique Two Sum, Palindrome Mastery Check

4 checks
  • I can state the invariant maintained by both pointers before writing the loop.
  • I can justify why each pointer movement discards only impossible candidates.
  • I can identify whether sorting is a prerequisite and whether index positions must be preserved.
  • I can test crossing pointers, duplicate values, no-match input, and one-element input.

Two Pointers Technique Two Sum, Palindrome Questions Learners Ask

Yes. The pointer movement depends on sorted order; for unsorted input, use a hash map or sort a copy first.

Compare the two ends, then move both inward. Stop at the first mismatch or when the pointers meet.

Test empty input, one item, duplicate values, adjacent pointers, and cases with no match.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.