← Two PointersDSA 1.1.C

Same Direction

Slow (write) and fast (read) pointers moving the same way. Remove Duplicates, Move Zeroes, Is Subsequence, Longest Mountain in Array.

StatusCollected
Notes4 solved

Both pointers move left to right here instead of toward each other — one reads ahead (fast), one marks where the next "kept" value should go (slow).

int removeDuplicates(int *nums, int n) {
      if (n == 0) return 0;
      int slow = 0;
      for (int fast = 1; fast < n; fast++) {
          if (nums[fast] != nums[slow]) {
              slow++;
              nums[slow] = nums[fast];
          }
      }
      return slow + 1;   /* new length */
  }

slow only advances when something worth keeping shows up — it's a write cursor, not just a second reader. Move Zeroes and Is Subsequence are the same shape with a different keep-condition; Longest Mountain in Array needed while instead of if for the expansion steps, since a mountain's slopes can run for more than one element at a time.

Notes from readers

Comments — via GitHub