Given two sorted arrays, the task is to merge them in a sorted manner.
Examples:

Input : arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8}
Output : arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}

Input : arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8}
Output : arr3[] = {4, 5, 7, 8, 8, 9}

[Naive Approach] Concatenate and Sort - O((n1 + n2) log(n1 + n2)) time and O(1) space

This approach involves two key steps: first, combining elements from two separate arrays into a third result array, and then sorting the result array.

Since it requires sorting the entire merged array, resulting in a time complexity of O((n1 + n2) log(n1 + n2)), where n is the total number of elements.

#include <bits/stdc++.h>
using namespace std;
void mergeArrays(vector<int>& arr1, vector<int>& arr2, 
                                    vector<int>& arr3) {
    int n1 = arr1.size();
    int n2 = arr2.size();
    int i = 0, j = 0, k = 0;
    // Traverse arr1 and insert its elements into arr3
    while (i < n1) {
        arr3[k++] = arr1[i++];
    // Traverse arr2 and insert its elements into arr3
    while (j < n2) {
        arr3[k++] = arr2[j++];
    // Sort the entire arr3
    sort(arr3.begin(), arr3.end());
// Driver code
int main() {
    vector<int> arr1 = {1, 3, 5, 7};
    vector<int> arr2 = {2, 4, 




    
6, 8};
    vector<int> arr3(arr1.size() + arr2.size());
    mergeArrays(arr1, arr2, arr3);
    for (int i = 0; i < arr3.size(); i++)
        cout << arr3[i] << " ";
    return 0;
    
#include <stdio.h>
#include <stdlib.h>
// Function to merge two arrays and sort the result
void mergeArrays(int* arr1, int n1, int* arr2, int n2, int* arr3) {
    int i = 0, j = 0, k = 0;
    // Traverse arr1 and insert its elements into arr3
    while (i < n1) {
        arr3[k++] = arr1[i++];
    // Traverse arr2 and insert its elements into arr3
    while (j < n2) {
        arr3[k++] = arr2[j++];
    // Sort the entire arr3
    qsort(arr3, n1 + n2, sizeof(int), [](const void* a, const void* b) {
        return (*(int*)a - *(int*)b);
// Driver code
int main() {
    int arr1[] = {1, 3, 5, 7};
    int arr2[] = {2, 4, 6, 8};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    int arr3[n1 + n2];
    mergeArrays(arr1, n1, arr2, n2, arr3);
    for (int i = 0; i < n1 + n2; i++)
        printf("%d ", arr3[i]);
    return 0;
    
import java.util.Arrays;
public class GfG {
    // Function to merge two arrays and sort the result
    public static void mergeArrays(int[] arr1, int[] arr2, int[] arr3) {
        int n1 = arr1.length;
        int n2 = arr2.length;
        int i = 0, j = 0, k = 0;
        // Traverse arr1 and insert its elements into arr3
        while (i < n1) {
            arr3[k++] = arr1[i++];
        // Traverse arr2 and insert its elements into arr3
        while (j < n2) {
            arr3[k++] = arr2[j++];
        // Sort the entire arr3
        Arrays.sort(arr3);
    // Driver code
    public static void main(String[] args) {
        int[] arr1 = {1, 3, 5, 7};
        int[] arr2 = {2, 4, 6, 8};
        int[] arr3 = new int[arr1.length + arr2.length];
        mergeArrays(arr1, arr2, arr3);
        for (int value : arr3)
            System.out.print(value + " ");
            Python
    
# Function to merge two arrays and sort the result
def merge_arrays(arr1, arr2):
    arr3 = arr1 + arr2
    arr3.sort()
    return arr3
# Driver code
arr1 = [1, 3, 5, 7]
arr2 = [




    
2, 4, 6, 8]
arr3 = merge_arrays(arr1, arr2)
for value in arr3:
    print(value, end=" ")
    
using System;
using System.Linq;
public class GfG {
    // Function to merge two arrays and sort the result
    public static int[] MergeArrays(int[] arr1, int[] arr2) {
        int[] arr3 = arr1.Concat(arr2).ToArray();
        Array.Sort(arr3);
        return arr3;
    // Driver code
    public static void Main() {
        int[] arr1 = {1, 3, 5, 7};
        int[] arr2 = {2, 4, 6, 8};
        int[] arr3 = MergeArrays(arr1, arr2);
        foreach (int value in arr3)
            Console.Write(value + " ");
            JavaScript
    
// Function to merge two arrays and sort the result
function mergeArrays(arr1, arr2) {
    let arr3 = arr1.concat(arr2);
    arr3.sort((a, b) => a - b);
    return arr3;
// Driver code
let arr1 = [1, 3, 5, 7];
let arr2 = [2, 4, 6, 8];
let arr3 = mergeArrays(arr1, arr2);
console.log(arr3.join(" "));

Output
1 2 3 4 5 6 7 8 

[Expected Approach] Using Merge of Merge Sort O(n1 + n2) time and O(n1 + n2) space

The idea is to use Merge function of Merge sort

  1. Create an array arr3[] of size n1 + n2.
  2. Simultaneously traverse arr1[] and arr2[]. 
    • Pick smaller of current elements in arr1[] and arr2[], copy this smaller element to next position in arr3[] and move ahead in arr3[] and the array whose element is picked.
  3. If there are remaining elements in arr1[] or arr2[], copy them also in arr3[].


#include <iostream>
#include <vector>
using namespace std;
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
void mergeArrays(vector<int>& ar1, vector<int>& ar2, 
                               vector<int>& ar3) {
    int i = 0, j = 0, k = 0;
    int n1 = ar1.size();
    int n2 = ar2.size();
    while (i < n1 && j < n2) {
        // Pick smaller of the two current
        // elements and move ahead in the
        // array of the picked element
        if (ar1[i] < ar2[j])
            ar3[k++] = ar1[i++];
            ar3[k++] = ar2[j++];
    // if there are remaining elements of
    // the first array, move them
    while (i < n1)
        ar3[k++] = ar1[i++];
    // Else if there are remaining elements of
    // the second array, move them
    while (j < n2)
        ar3[k++] = ar2[j++];
// Driver code
int main() {
    vector<int> ar1 = {1, 3, 5, 7};
    vector<int> ar2 = {2, 4, 6, 8};
    vector<int> ar3(ar1.size() + ar2.size());
    mergeArrays(ar1, ar2, ar3);
    for (int i = 0; i <




    
 ar3.size(); i++)
        cout << ar3[i] << " ";
    return 0;
    
#include <stdio.h>
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
void mergeArrays(int* ar1, int n1, int* ar2, int n2, int* ar3) {
    int i = 0, j = 0, k = 0;
    while (i < n1 && j < n2) {
        // Pick smaller of the two current elements and move ahead in the array of the picked element
        if (ar1[i] < ar2[j])
            ar3[k++] = ar1[i++];
            ar3[k++] = ar2[j++];
    // if there are remaining elements of the first array, move them
    while (i < n1)
        ar3[k++] = ar1[i++];
    // Else if there are remaining elements of the second array, move them
    while (j < n2)
        ar3[k++] = ar2[j++];
// Driver code
int main() {
    int ar1[] = {1, 3, 5, 7};
    int ar2[] = {2, 4, 6, 8};
    int n1 = sizeof(ar1) / sizeof(ar1[0]);
    int n2 = sizeof(ar2) / sizeof(ar2[0]);
    int ar3[n1 + n2];
    mergeArrays(ar1, n1, ar2, n2, ar3);
    for (int i = 0; i < n1 + n2; i++)
        printf("%d ", ar3[i]);
    return 0;
    
import java.util.*;
public class GfG {
    // Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
    public static void mergeArrays(int[] ar1, int[] ar2, int[] ar3) {
        int i = 0, j = 0, k = 0;
        int n1 = ar1.length;
        int n2 = ar2.length;
        while (i < n1 && j < n2) {
            // Pick smaller of the two current elements and move ahead in the array of the picked element
            if (ar1[i] < ar2[j])
                ar3[k++] = ar1[i++];
                ar3[k++] = ar2[j++];
        // if there are remaining elements of the first array, move them
        while (i < n1)
            ar3[k++] = ar1[i++];
        // Else if there are remaining elements of the second array, move them
        while (j < n2)
            ar3[k++] = ar2[j++];
    // Driver code
    public static void main(String[] args) {
        int[] ar1 = {1, 3, 5, 7};
        int[] ar2 = {2, 4, 6, 8};
        int[] ar3 = new int[ar1.length + ar2.length];
        mergeArrays(ar1, ar2, ar3);
        for (int value : ar3)
            System.out.print(value + " ");
            Python
    
# Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
def merge_arrays(ar1, ar2):
    i = 0
    j = 0
    k = 0
    n1 = len(ar1)
    n2 = len(ar2)
    ar3




    
 = []
    while i < n1 and j < n2:
        # Pick smaller of the two current elements and move ahead in the array of the picked element
        if ar1[i] < ar2[j]:
            ar3.append(ar1[i])
            i += 1
        else:
            ar3.append(ar2[j])
            j += 1
    # if there are remaining elements of the first array, move them
    while i < n1:
        ar3.append(ar1[i])
        i += 1
    # Else if there are remaining elements of the second array, move them
    while j < n2:
        ar3.append(ar2[j])
        j += 1
    return ar3
# Driver code
ar1 = [1, 3, 5, 7]
ar2 = [2, 4, 6, 8]
ar3 = merge_arrays(ar1, ar2)
print(" ".join(map(str, ar3)))
    
using System;
public class GfG {
    // Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
    public static void MergeArrays(int[] ar1, int[] ar2, int[] ar3) {
        int i = 0, j = 0, k = 0;
        int n1 = ar1.Length;
        int n2 = ar2.Length;
        while (i < n1 && j < n2) {
            // Pick smaller of the two current elements and move ahead in the array of the picked element
            if (ar1[i] < ar2[j])
                ar3[k++] = ar1[i++];
                ar3[k++] = ar2[j++];
        // if there are remaining elements of the first array, move them
        while (i < n1)
            ar3[k++] = ar1[i++];
        // Else if there are remaining elements of the second array, move them
        while (j < n2)
            ar3[k++] = ar2[j++];
    // Driver code
    public static void Main() {
        int[] ar1 = {1, 3, 5, 7};
        int[] ar2 = {2, 4, 6, 8};
        int[] ar3 = new int[ar1.Length + ar2.Length];
        MergeArrays(ar1, ar2, ar3);
        foreach (int value in ar3)
            Console.Write(value + " ");
            JavaScript
    
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
function mergeArrays(ar1, ar2) {
    let i = 0, j = 0, k = 0;
    const n1 = ar1.length;
    const n2 = ar2.length;
    const ar3 = [];
    while (i < n1 && j < n2) {
        // Pick smaller of the two current elements and move ahead in the array of the picked element
        if (ar1[i] < ar2[j])
            ar3[k++] = ar1[i++];
            ar3[k++] = ar2[j++];
    // if there are remaining elements of the first array, move them
    while (i < n1)
        ar3[k++] = ar1[i++];
    // Else if there are remaining elements of the second array, move them
    while (j < n2)
        ar3[k++] = ar2[j++];
    return ar3;
// Driver code
const ar1 = [1, 3, 5, 7];
const ar2 = [2, 4, 6, 8];
const ar3 = mergeArrays(ar1, ar2);
console.log(ar3.join(" "));

Output
1 2 3 4 5 6 7 8 

Problems related to Merging Two Sorted Arrays

Merge two sorted arrays with O(1) extra space 
Merge k sorted arrays
Union of Two Sorted Arrays
Intersection of Two Sorted Arrays
Merge 2 sorted linked list in reverse order
Sort last M elements
Merge 2 sorted linked list in reverse order
Merge Sort for Doubly Linked List

Similar Reads

Sorted merge in one array
Given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Merge B into A in sorted order. Examples: Input : a[] = {10, 12, 13, 14, 18, NA, NA, NA, NA, NA} b[] = {16, 17, 19, 20, 22};; Output : a[] = {10, 12, 13, 14, 16, 17, 18, 19, 20, 22} One way is to merge the two
7 min read
K-th Element of Merged Two Sorted Arrays
Given two sorted arrays of sizes m and n respectively, the task is to find the element that would be at the k-th position in the final sorted array formed by merging these two arrays.Examples: Input: a[] = [2, 3, 6, 7, 9], b[] = [1, 4, 8, 10], k = 5Output: 6Explanation: The final sorted array is [1,
15+ min read
Union of Two Sorted Arrays
Given two sorted arrays a[] and b[], the task is to return union of both the arrays in sorted order. Union of two arrays is an array having all distinct elements that are present in either array. The input arrays may contain duplicates.Examples:Input: a[] = {1, 1, 2, 2, 2, 4}, b[] = {2, 2, 4, 4}Outp
15+ min read
Merge Two Sorted Arrays Without Extra Space
Given two sorted arrays a[] and b[] of size n and m respectively, the task is to merge both the arrays and rearrange the elements such that the smallest n elements are in a[] and the remaining m elements are in b[]. All elements in a[] and b[] should be in sorted order.Examples: Input: a[] = [2, 4,
15+ min read
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !