Javatpoint Logo
Javatpoint Logo

Find Relative Complement of two Sorted Arrays

We will learn how to find the relative complement of two sorted arrays in this lesson.

A sorted array is one that has been organised in a specified order (alphabetic, chronological, ordinal, cardinal order). An unsorted array is one that is not ordered in any way.

Let us assume two sorted arrays: array1 and array2 of size p and q, respectively. We have to determine the relative complement of two arrays, array1 - array2, that indicates we have to locate all the elements in array1 but not in array2.

Examples:

  • Consider two pointers, i and j, which traverse array1 and array2, accordingly.
  • If the array1[i] element is lower than the array2[j] element, print it and increment i.
  • If array1 is higher than array2[j], then j is increased.
  • Else, increase the value of i and j.

Implementation:

C++ Program:

Output:

6 12 15
  • Time Complexity will be O(p + q).
  • Auxiliary Space will be O(1).






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA