Common Elements in Sorted Arrays
Given two sorted arrays, find the common elements between them in O(n+m) time with constant space.
The Idea: Maintain two iterators for two arrays. If the number at one of the iterators exceeds the other number at the second iterator, then increment the iterator in the other number. In the next step, this iterator will be >= the previous number and potentially match next the number that was greater than it in the previous step. The same applies in reverse. Once any of iterators exceed there current list, there are no longer any elements in common so the algorithm terminates.
Last updated