Topics
(2 Sum) Find two indices in a sorted array where elements sum to target
- left starts at
0, right atn-1.- If
arr[left] + arr[right] > target, decrementright(sum is too large).- If
sum < target, incrementleft.- Time: vs. brute-force .
Deduplicate a sorted array in-place
- Sort the array .
- Uses the concept of slow and fast pointers
slowpointer tracks the last unique element.fastscans ahead. Whenarr[fast] != arr[slow], incrementslowand copyarr[fast].- Time:
3 Sum Find all triplets
[a, b, c]such thata + b + c = 0
- Sort the array .
- Fix
a = arr[i], then use two pointers on the subarrayi+1ton-1to find pairs(b, c)such thatb + c = -a.- Time: vs. brute-force .