Question:Given 2 sorted arrays, find the intersection elements between them.

Array 1:2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26

Array 2:3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33

Output:6, 12, 18, 24

Letâ€™s called Array1 as **“A”** and Array2 as **“B”**, each with size m and n.

The obvious brute-force solution is to scan through each element in A, and for each element in A, scan if that element exist in B. The running time complexity is O(m*n). *Not good! Can we do better? Absolutely!
*

First, we know that both arrays are sorted. Can we somehow use this information to our advantage?

We can apply binary search to search if an element of A exist in B. So, the only modification from the brute-force approach is modifying linear search to binary search. This seems like a good improvement, **we manage to reduce the complexity to O(m*lg(n)).**

Of course, you know you can trade space for running time by using a hash table. Is it really useful? We can definitely hash each element in B to an array index (takes O(n) time). Therefore, to find if an element of A exist in B, it would require just O(1) time. The complexity improves to O(m+n).

But there is a problem, **what if n is very big?** (i.e, n is One Billion!). We have a problem here. The hash table will either requires a large amount of memory space, or there will be lots of collision in the table, which makes access time no longer O(1) time. Therefore, using a hash table is not a good general solution to this problem. Besides, using hash table DO NOT require that the array being sorted in the first place.

**Here is the most important observation in order to solve this problem. Both arrays ARE sorted. This provides a very important clue. We must make full use of this information that they ARE in fact sorted.**

### The best solution:

We can have two index, which both starts at zero. Compare the two first elements of A and B.

- If A[0] is greater than B[0], we increase index of B by one.
- If B[0] is greater than A[0], we increase index of A by one.
- If they are equal, we know an intersection has occurred, so add it to the list and increment index of A and B by one.
- Once either index reaches the end of A or B, we have found all the intersections of A and B.

**The complexity of this approach is still O(m+n), but it does not requires any extra space that a hash table requires**. The complexity is O(m+n) because in the worse case, there would be no intersection between the two arrays, and we need to increment first index a total of m times and increment second index a total of n times, which is a total of m+n times.

Here is an implementation of the above algorithm-

void findIntersection(int arr1[], int m, int arr2[], int n) { // An array to store the intersection elements int intersection_array[50]; int counter = 0; int i = 0, j = 0; // Loop through the elements while (i < m && j < n) { if (arr1[i] > arr2[j]) { // Increase iterator for array 2 j++; } else if (arr2[j] > arr1[i]) { // Increase iterator for array 1 i++; } else { intersection_array[counter] = arr1[i]; counter++; // Increment counter for both arrays i++; j++; } } for(i = 0; i<counter; i++) printf("%d ",intersection_array[i]); }

Here is the working code:- http://ideone.com/65YzKi

## 7 comments

Why it’s no O(min(m, n)). Since any array end is reached we don’t need to continue

The complexity big-O is always given as an upper bound. The maximum time the algorithm can take.

But what if it is not sorted? Then sortimg will take extra time..

Yes. But it will still be faster than the briteforce method.

Nice but is this worken on c++

Looks obvious only after seeing the Solution. Nice one .

A newer solution is always welcome

Comments are closed.