0
0
CppProgramBeginner · 2 min read

C++ Program to Merge Two Arrays

To merge two arrays in C++, create a new array with size equal to the sum of both arrays, then copy elements from the first and second arrays into it using loops, like for (int i = 0; i < n; i++) merged[i] = arr1[i]; for (int j = 0; j < m; j++) merged[n + j] = arr2[j];.
📋

Examples

Inputarr1 = {1, 2}, arr2 = {3, 4}
OutputMerged array: 1 2 3 4
Inputarr1 = {5, 10, 15}, arr2 = {20, 25}
OutputMerged array: 5 10 15 20 25
Inputarr1 = {}, arr2 = {7, 8, 9}
OutputMerged array: 7 8 9
🧠

How to Think About It

To merge two arrays, think of putting all items from the first array into a new bigger array, then adding all items from the second array right after. This way, you keep all elements in order and combine them into one list.
📐

Algorithm

1
Get the size of the first array and the second array.
2
Create a new array with size equal to the sum of both arrays.
3
Copy all elements from the first array into the new array starting at index 0.
4
Copy all elements from the second array into the new array starting after the last element of the first array.
5
Print or return the merged array.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int arr1[] = {1, 2, 3};
    int arr2[] = {4, 5, 6, 7};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int m = sizeof(arr2) / sizeof(arr2[0]);
    int merged[n + m];

    for (int i = 0; i < n; i++) {
        merged[i] = arr1[i];
    }
    for (int j = 0; j < m; j++) {
        merged[n + j] = arr2[j];
    }

    cout << "Merged array: ";
    for (int k = 0; k < n + m; k++) {
        cout << merged[k] << " ";
    }
    cout << endl;
    return 0;
}
Output
Merged array: 1 2 3 4 5 6 7
🔍

Dry Run

Let's trace merging arr1 = {1, 2, 3} and arr2 = {4, 5, 6, 7} through the code

1

Calculate sizes

n = 3 (size of arr1), m = 4 (size of arr2)

2

Create merged array

merged array size = 7

3

Copy arr1 elements

merged[0] = 1, merged[1] = 2, merged[2] = 3

4

Copy arr2 elements

merged[3] = 4, merged[4] = 5, merged[5] = 6, merged[6] = 7

5

Print merged array

Output: 1 2 3 4 5 6 7

IndexValue after copying arr1Value after copying arr2
011
122
233
3-4
4-5
5-6
6-7
💡

Why This Works

Step 1: Create merged array

We make a new array big enough to hold all elements from both arrays using int merged[n + m];.

Step 2: Copy first array

We copy each element from the first array into the new array starting at index 0 with a simple for loop.

Step 3: Copy second array

We continue copying elements from the second array starting at index n to keep all elements in order.

Step 4: Print merged array

Finally, we print all elements of the merged array to show the combined result.

🔄

Alternative Approaches

Using std::vector
cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> arr1 = {1, 2, 3};
    vector<int> arr2 = {4, 5, 6, 7};
    vector<int> merged;

    merged.insert(merged.end(), arr1.begin(), arr1.end());
    merged.insert(merged.end(), arr2.begin(), arr2.end());

    cout << "Merged array: ";
    for (int val : merged) {
        cout << val << " ";
    }
    cout << endl;
    return 0;
}
This approach uses dynamic arrays (vectors) which handle resizing automatically and simplify merging.
Merge with dynamic memory allocation
cpp
#include <iostream>
using namespace std;

int main() {
    int arr1[] = {1, 2, 3};
    int arr2[] = {4, 5, 6, 7};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int m = sizeof(arr2) / sizeof(arr2[0]);
    int* merged = new int[n + m];

    for (int i = 0; i < n; i++) merged[i] = arr1[i];
    for (int j = 0; j < m; j++) merged[n + j] = arr2[j];

    cout << "Merged array: ";
    for (int k = 0; k < n + m; k++) cout << merged[k] << " ";
    cout << endl;

    delete[] merged;
    return 0;
}
This method uses dynamic memory allocation to create the merged array, useful when array sizes are not known at compile time.

Complexity: O(n + m) time, O(n + m) space

Time Complexity

The program loops through both arrays once to copy elements, so the time grows linearly with the total number of elements.

Space Complexity

A new array is created to hold all elements, so space used is proportional to the sum of both arrays' sizes.

Which Approach is Fastest?

Using raw arrays is fast but less flexible; vectors add overhead but simplify code and manage memory automatically.

ApproachTimeSpaceBest For
Static arraysO(n + m)O(n + m)Fixed size arrays known at compile time
std::vectorO(n + m)O(n + m)Dynamic size and safer memory management
Dynamic allocationO(n + m)O(n + m)When array sizes are only known at runtime
💡
Use std::vector for easier and safer array merging in modern C++.
⚠️
Forgetting to allocate enough space for the merged array causes out-of-bounds errors.