Given two sorted arrays or lists, get their union and intersection.

For example, if the input arrays are:

int[] list1 = {1, 3, 4, 5, 6, 7}

int[] list2 = {2, 3, 5, 6}

Then the union is {1, 2, 3, 4, 5, 6, 7} and the Intersection is {3, 5, 6}.

Analysis

We can solve this problem using the same idea of merging sorted list.

Union of two sorted lists algorithm

define two index variables p1 and p2, initialized as 0

define List<Integer>

[Read More...]