Given two array of size **n** and m. The task is to find the number of ways we can merge the given arrays into one array such that order of elements of each array doesn’t change.

Examples:

Input : n = 2, m = 2 Output : 6 Let first array of size n = 2 be [1, 2] and second array of size m = 2 be [3, 4]. So, possible merged array of n + m elements can be: [1, 2, 3, 4] [1, 3, 2, 4] [3, 4, 1, 2] [3, 1, 4, 2] [1, 3, 4, 2] [3, 1, 2, 4] Input : n = 4, m = 6 Output : 210

The idea is to use the concept of combinatorics. Suppose we have two array A{a1, a2, …., am} and B{b1, b2, …., bn} having m and n elements respectively and now we have to merge them without loosing their order.

After merging we know that the total number of element will be (m + n) element after merging. So, now we just need the ways to choose m places out of (m + n) where you will place element of array A in its actual order, which is ^{m + n}C_{n}.

After placing m element of array A, n spaces will be left, which can be filled by the n elements of B array in its actual order.

So, total number of ways to merge two array such that their order in merged array is same is ^{m + n}C_{n}

Below is the implementation of this appraoch:

## C++

[sourcecode language=”CPP”]

// CPP Program to find number of ways

// to merge two array such that their

// order in merged array is same

#include <bits/stdc++.h>

using namespace std;

// function to find the binomial coefficient

int binomialCoeff(int n, int k)

{

int C[k + 1];

memset(C, 0, sizeof(C));

C[0] = 1; // nC0 is 1

for (int i = 1; i <= n; i++) {

// Compute next row of pascal triangle

// using the previous row

for (int j = min(i, k); j > 0; j–)

C[j] = C[j] + C[j – 1];

}

return C[k];

}

// function to find number of ways

// to merge two array such that their

// order in merged array is same

int numOfWays(int n, int m)

{

return binomialCoeff(m + n, m);

}

// Driven Program

int main()

{

int n = 2, m = 2;

cout << numOfWays(n, m) << endl;

return 0;

}

[/sourcecode]

## Java

[sourcecode language=”Java”]

// Java Program to find number of ways

// to merge two array such that their

// order in merged array is same

import java.io.*;

class GFG {

// function to find the binomial

// coefficient

static int binomialCoeff(int n, int k)

{

int C[] = new int[k + 1];

// memset(C, 0, sizeof(C));

C[0] = 1; // nC0 is 1

for (int i = 1; i <= n; i++) {

// Compute next row of pascal

// triangle using the previous

// row

for (int j = Math.min(i, k);

j > 0; j–)

C[j] = C[j] + C[j – 1];

}

return C[k];

}

// function to find number of ways

// to merge two array such that their

// order in merged array is same

static int numOfWays(int n, int m)

{

return binomialCoeff(m + n, m);

}

// Driven Program

public static void main (String[] args)

{

int n = 2, m = 2;

System.out.println(numOfWays(n, m));

}

}

// This code is contributed by anuj_67.

[/sourcecode]

## C#

[sourcecode language=”CSHARP”]

// C# Program to find number of ways

// to merge two array such that their

// order in merged array is same

using System;

class GFG {

// function to find the binomial

// coefficient

static int binomialCoeff(int n, int k)

{

int []C = new int[k + 1];

// memset(C, 0, sizeof(C));

C[0] = 1; // nC0 is 1

for (int i = 1; i <= n; i++) {

// Compute next row of pascal

// triangle using the previous

// row

for (int j = Math.Min(i, k);

j > 0; j–)

C[j] = C[j] + C[j – 1];

}

return C[k];

}

// function to find number of ways

// to merge two array such that their

// order in merged array is same

static int numOfWays(int n, int m)

{

return binomialCoeff(m + n, m);

}

// Driven Program

public static void Main ()

{

int n = 2, m = 2;

Console.WriteLine(numOfWays(n, m));

}

}

// This code is contributed by anuj_67.

[/sourcecode]

**Output:**

6

We can solve above problem in linear time using linear time implementation of binomial coefficient.

## Recommended Posts:

- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Merge 3 Sorted Arrays
- Merge two sorted arrays
- Merge k sorted arrays | Set 1
- Merge two sorted arrays in Python using heapq
- Merge two sorted arrays with O(1) extra space
- Maximum array from two given arrays keeping order same
- Merging two unsorted arrays in sorted order
- Maximum sum of increasing order elements from n arrays
- Find array with k number of merge sort calls
- Find minimum number of merge operations to make an array palindrome
- Ways to represent a number as a sum of 1's and 2's
- Performance analysis of Row major and Column major order of storing arrays in C
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to cut a stick of length N into K pieces

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.