Union of Two Sorted Arrays

Logic Building: Rotations & Sets DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Find the union of two sorted arrays, Arrays, Map, Hash Tables, Set, Two Pointers, Time Complexity, Space Complexity, Frequency Counting, complexity analysis, array, frequency counting, set, duplicate handling, set operations, two pointer technique, Combinatorics & Binomial Coefficients, Frequency Counting, Sets & Hash Sets, Duplicate Removal.

Given two sorted arrays, arr1 of size n and arr2 of size m, find their union. The union of two arrays is defined as the collection of common and distinct elements present in both arrays. Note: Elements in the union should be in ascending order. Input Specification: The first line contains an integer n, the size of arr1. The second line contains n space separated integers, the elements of arr1. The third line contains an integer m, the size of arr2. The fourth line contains m space separated integers, the elements of arr2. Output Specification: Print the union of arr1 and arr2 as space separated integers in ascending order. Examples: Example 1: Input: n = 5, m = 5 arr1[] = {1, 2, 3, 4, 5} arr2[] = {2, 3, 4, 4, 5} Output: {1, 2, 3, 4, 5} Explanation: Common Elements in arr1 and arr2 are: 2, 3, 4, 5 Distinct Elements in arr1 are : 1 Distinct Elements in arr2 are : No distinct elements. Union of arr1 and arr2 is {1, 2, 3, 4, 5} Example 2: Input: n = 10, m = 7 arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} arr2[] = {2, 3, 4, 4, 5, 11, 12} Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Explanation: Common Elements in arr1 and arr2 are: 2, 3, 4, 5 Distinct Elements in arr1 are : 1, 6, 7, 8, 9, 10 Distinct Elements in arr2 are : 11, 12 Union of arr1 and arr2 is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}