Merge Two Sorted Arrays
Merge Two Sorted Arrays
AfterAcademy
Admin AfterAcademy
17 Aug 2020
Difficulty: Medium
https://afteracademy.com/blog/merge-two-sorted-arrays/ 1/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
You are given two sorted arrays arr1[] and arr2[] of sizes m and n
respectively. Write a program to merge them in such a way that the
resultant array is sorted too.
Problem Note:
Your final array should be the first array i.e. array arr1[] .
Example 1
Input: arr1[] = [3, 9, 10, 18, 23], arr2[] = [5, 12, 15, 20, 21, 25]
m = 5, n = 6
Output: [3, 5, 9, 10, 12, 15, 18, 20, 21, 23, 25]
Explanation: The resultant array i.e. arr1[] has all the elements of arr1[]
Example 2
Example 3
Solutions
We will be discussing two different solutions to this problem:-
https://afteracademy.com/blog/merge-two-sorted-arrays/ 2/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
2. Two pointers : Compare the two values from the end of ar1 and ar2
and store in ar1 while decrementing pointers accordingly.
https://afteracademy.com/blog/merge-two-sorted-arrays/ 3/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
https://afteracademy.com/blog/merge-two-sorted-arrays/ 4/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
Solution Steps
While traversing through both the arrays: Pick the smaller of current
elements of arr1 and arr2 , save the smaller value of both the
arrays in the auxiliary array, thereby increment the positions
accordingly.
Now copy the auxiliary array to arr1 as the final array should be
arr1
Pseudo Code
https://afteracademy.com/blog/merge-two-sorted-arrays/ 5/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
Complexity Analysis
On what basis we are storing the value in the auxiliary array from arr1
and arr2 ?
2. Two Pointers
The problem statement requires the output after merge operation in arr1
. This states that arr1 has enough space to accommodate the elements of
arr1 and arr2 .
Now, the two given array are already sorted, so we can put two pointers at
the end of each array, say i is pointing arr1[n-1] and j is pointing
arr2[m-1] . A third pointer k pointing to arr1[n+m-1] . then we start
comparing values at the pointers i and j , the larger value will be stored
at the pointer k thereby decreasing the pointer with larger value by 1 and
https://afteracademy.com/blog/merge-two-sorted-arrays/ 6/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
step 1)
i k
arr1 = [3, 9, 10, 18, 23, _ , _ , _ , _ , _ , _]
j
arr2 = [5, 12, 15, 20, 21, 25]
step 2) arr1[i] < arr2[j] => arr1[k] = arr2[j], j--, k--
i k
arr1 = [3, 9, 10, 18, 23, _ , _ , _ , _ , _ , 25]
j
arr2 = [5, 12, 15, 20, 21, 25]
step 3) arr1[i] > arr2[j] => arr1[k] = arr1[i], i--, k--
i k
arr1 = [3, 9, 10, 18, 23, _ , _ , _ , _ , 23 , 25]
j
arr2 = [5, 12, 15, 20, 21, 25]
step 4) arr1[i] < arr2[j] => arr1[k] = arr2[j], j--, k--
i k
arr1 = [3, 9, 10, 18, 23, _ , _ , _ , 21 , 23 , 25]
j
arr2 = [5, 12, 15, 20, 21, 25]
step 5) arr1[i] < arr2[j] => arr1[k] = arr2[j], j--, k--
i k
arr1 = [3, 9, 10, 18, 23, _ , _ , 20 , 21 , 23 , 25]
j
arr2 = [5, 12, 15, 20, 21, 25]
.
.
step 10) arr1[i] < arr2[j] => arr1[k] = arr2[j], j--, k--
i k
arr1 = [3, 5, 9, 10, 12, 15 , 18 , 20 , 21 , 23 , 25]
j
arr2 = [5, 12, 15, 20, 21, 25]
Solution Steps
https://afteracademy.com/blog/merge-two-sorted-arrays/ 7/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
Return arr1
Pseudo Code
Complexity Analysis
Please comment down below if you have a better insight in the above approach.
https://afteracademy.com/blog/merge-two-sorted-arrays/ 9/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
https://afteracademy.com/blog/merge-two-sorted-arrays/ 11/12
1/2/25, 12:17 PM Merge Two Sorted Arrays
https://afteracademy.com/blog/merge-two-sorted-arrays/ 12/12