Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Merge Arrays in C++

Merge two integer arrays into one in C++ by copying the first array, then the second after it. Learn the n1 + i offset and how to size the merged array.

BeginnerArray ProgramsExample 3 of 4
merge-arrays.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int n1, n2;
6
7 cout << "Enter size of first array: ";
8 cin >> n1;
9 int arr1[n1];
10 cout << "Enter " << n1 << " elements: ";
11 for (int i = 0; i < n1; i++) {
12 cin >> arr1[i];
13 }
14
15 cout << "Enter size of second array: ";
16 cin >> n2;
17 int arr2[n2];
18 cout << "Enter " << n2 << " elements: ";
19 for (int i = 0; i < n2; i++) {
20 cin >> arr2[i];
21 }
22
23 int merged[n1 + n2];
24
25 // Copy first array
26 for (int i = 0; i < n1; i++) {
27 merged[i] = arr1[i];
28 }
29
30 // Copy second array
31 for (int i = 0; i < n2; i++) {
32 merged[n1 + i] = arr2[i];
33 }
34
35 cout << "Merged array: ";
36 for (int i = 0; i < n1 + n2; i++) {
37 cout << merged[i] << " ";
38 }
39 cout << endl;
40
41 return 0;
42}

Sample Output

Merged array: 1 2 3 4 5 6

arr1 then arr2.

In this one

  • How to size an array that holds two others
  • How the offset n1 + i places the second array after the first
  • Why run-time sized arrays are not standard C++
  • The difference between concatenating and a sorted merge

How it works

  1. 1

    Read n1 and arr1 (1 2 3).

  2. 2

    Read n2 and arr2 (4 5 6).

  3. 3

    Copy arr1 into merged[0..2].

  4. 4

    Copy arr2 into merged[3..5] using n1 + i.

  5. 5

    Print all six values.

Second copy loop, n1 = 3

Where each element of arr2 lands.

  1. 1.i = 0: merged[3] = 4
  2. 2.i = 1: merged[4] = 5
  3. 3.i = 2: merged[5] = 6

Edge cases

Empty second array

Enter 0 for n2.

The second copy loop does not run, and merged holds only arr1. Zero-length arrays are not allowed in standard C++, which is another reason to prefer vector.

Using merged[i] for the second copy

Drop the n1 + offset.

arr2 overwrites the start of merged, and the last n2 slots stay uninitialized, so garbage values print.

What's going on

Merging two arrays here means placing every element of the second array after every element of the first, in a brand new array. Nothing is sorted. 1 2 3 and 4 5 6 become 1 2 3 4 5 6.

cin >> n1; then int arr1[n1];

The size comes from the user. Note that an array with a run-time size like this is an extension offered by GCC and Clang, not standard C++. vector<int> is the portable tool. For learning the copy logic, the array version is fine.

The first input loop fills arr1[0] to arr1[n1 - 1]. The second does the same for arr2.

int merged[n1 + n2];

The new array must hold both. Size it as n1 + n2, not max(n1, n2). Too small and the second copy loop writes past the end, which is undefined behavior: it might crash, or quietly overwrite another variable.

merged[i] = arr1[i];

First copy. Positions 0 to n1 − 1 of merged get the first array, same index on both sides.

merged[n1 + i] = arr2[i];

Second copy. This is the line worth understanding. arr2[0] must land right after the first array, at index n1. So the target index is n1 + i. With n1 = 3: arr2[0] goes to merged[3], arr2[1] to merged[4], arr2[2] to merged[5].

Write merged[i] = arr2[i] by mistake and the second array overwrites the first. You get 4 5 6 followed by whatever garbage was in the rest of merged.

The last loop prints all n1 + n2 values with a space after each.

Trace with sizes 3 and 3:

after loop 1: merged = 1 2 3 _ _ _
i = 0: merged[3] = 4
i = 1: merged[4] = 5
i = 2: merged[5] = 6

The order of the result is simply arr1 then arr2. If both inputs are sorted and you want a sorted result, you need the two-pointer method from Merge Two Sorted Arrays instead.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

With n1 = 4, where does arr2[0] go?

Pick an answer to check it.

Question 2

What size must merged have?

Pick an answer to check it.

Questions people ask

Does this sort the result?

No. It only places the second array after the first.

How would I do this with vectors?

merged = a; merged.insert(merged.end(), b.begin(), b.end());

Can the arrays have different sizes?

Yes. The offset uses n1, so any sizes work.