Skip to main content

JP Morgan

JP Morgan OA-4 2023

Problem Description

You are provided with two arrays, arr1 and arr2, with sizes N and M respectively. Your task is to perform the following operations:

  1. Intersection: Find all elements that appear in both arrays.
  2. Union: Find all unique elements from both arrays, combining them while removing any duplicates.

Notes:

  • Both arrays may contain duplicate elements.
  • The result for both the intersection and the union should be sorted in ascending order.
  • If no elements are found in the intersection or union, output -1 for that array.
  • No duplicates should be present in the final output arrays.

Input Format:

  • The first line contains two space-separated integers N and M, representing the sizes of the arrays.
  • The second line contains N space-separated integers, representing the elements of arr1.
  • The third line contains M space-separated integers, representing the elements of arr2.

Output Format:

  • The first line contains the elements of the intersection array, separated by spaces.
  • The second line contains the elements of the union array, separated by spaces.

Constraints:

  • 1 <= N <= 10^4
  • 1 <= M <= 10^4
  • -10^9 <= arr1[i], arr2[i] <= 10^9

Example:

Input:

5 4
1 2 2 3 4
2 2 3 5

Output:

2 3
1 2 3 4 5

Explanation:

  • Intersection: The common elements between arr1 and arr2 are 2 and 3.
  • Union: The unique elements from both arrays are 1, 2, 3, 4, 5. All duplicates have been removed, and the result is sorted in ascending order.