# Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array

• Last Updated : 23 Apr, 2021

Given two sorted arrays A[] and B[] of size N, the task is to check if it is possible to merge two given sorted arrays into a new sorted array such that no two consecutive elements are from the same array.

Examples:

Input: A[] = {3, 5, 8}, B[] = {2, 4, 6}
Output: Yes

Explanation: Merged array = {B, A, B, A, B, A}
Since the resultant array is sorted array, the required output is Yes.

Input: A[] = {12, 4, 2, 5, 3}, B[] = {32, 13, 43, 10, 8}
Output: No

Approach: Follow the steps below to solve the problem:

• Initialize a variable, say flag = true to check if it is possible to form a new sorted array by merging the given two sorted arrays such that no two consecutive elements are from the same array.
• Initialize a variable, say prev to check if the previous element of the merge array are from the array A[] or the array B[]. If prev == 1 then the previous element are from the array A[] and if prev == 0 then the previous element are from the array B[].
• Traverse both the array using variables, i and j and check the following conditions:
• If A[i] < B[j] and prev != 0 then increment the value of i and update the value of prev to 0.
• If B[j] < A[i[ and prev != 1 then increment the value of j and update the value of prev to 1.
• If A[i] == B[j] and prev != 1 then increment the value of j and update the value of prev to 1.
• If A[i] == B[j] and prev != 0 then increment the value of i and update the value of prev to 0.
• If none of the above condition satisfy then update flag = false.
• Finally, print the value of flag.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to check if it is possible to merge``// the two given arrays with given conditions``bool` `checkIfPossibleMerge(``int` `A[], ``int` `B[], ``int` `N)``{``    ``// Stores index of``    ``// the array A[]``    ``int` `i = 0;` `    ``// Stores index of``    ``// the array  B[]``    ``int` `j = 0;` `    ``// Check if the previous element are from``    ``// the array A[] or from the array B[]``    ``int` `prev = -1;` `    ``// Check if it is possible to merge the two``    ``// given sorted arrays with given conditions``    ``int` `flag = 1;` `    ``// Traverse both the arrays``    ``while` `(i < N && j < N) {` `        ``// If A[i] is less than B[j] and``        ``// previous element are not from A[]``        ``if` `(A[i] < B[j] && prev != 0) {` `            ``// Update prev``            ``prev = 0;` `            ``// Update i``            ``i++;``        ``}` `        ``// If B[j] is less than A[i] and``        ``// previous element are not from B[]``        ``else` `if` `(B[j] < A[i] && prev != 1) {` `            ``// Update prev``            ``prev = 1;` `            ``// Update j``            ``j++;``        ``}` `        ``// If A[i] equal to B[j]``        ``else` `if` `(A[i] == B[j]) {` `            ``// If previous element``            ``// are not from B[]``            ``if` `(prev != 1) {` `                ``// Update prev``                ``prev = 1;` `                ``// Update j``                ``j++;``            ``}` `            ``// If previous element is``            ``// not from A[]``            ``else` `{` `                ``// Update prev``                ``prev = 0;` `                ``// Update i``                ``i++;``            ``}``        ``}` `        ``// If it is not possible to merge two``        ``// sorted arrays with given conditions``        ``else` `{` `            ``// Update flag``            ``flag = 0;``            ``break``;``        ``}``    ``}` `    ``return` `flag;``}` `// Driver Code``int` `main()``{``    ``int` `A = { 3, 5, 8 };``    ``int` `B = { 2, 4, 6 };``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``if` `(checkIfPossibleMerge(A, B, N)) {``        ``cout << ``"Yes"``;``    ``}``    ``else` `{``        ``cout << ``"No"``;``    ``}``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.io.*;` `class` `GFG{` `// Function to check if it is possible to merge``// the two given arrays with given conditions``static` `boolean` `checkIfPossibleMerge(``int``[] A, ``int``[] B,``                                    ``int` `N)``{``    ` `    ``// Stores index of``    ``// the array A[]``    ``int` `i = ``0``;` `    ``// Stores index of``    ``// the array  B[]``    ``int` `j = ``0``;` `    ``// Check if the previous element are from``    ``// the array A[] or from the array B[]``    ``int` `prev = -``1``;` `    ``// Check if it is possible to merge the two``    ``// given sorted arrays with given conditions``    ``boolean` `flag = ``true``;` `    ``// Traverse both the arrays``    ``while` `(i < N && j < N)``    ``{``        ` `        ``// If A[i] is less than B[j] and``        ``// previous element are not from A[]``        ``if` `(A[i] < B[j] && prev != ``0``)``        ``{``            ` `            ``// Update prev``            ``prev = ``0``;` `            ``// Update i``            ``i++;``        ``}` `        ``// If B[j] is less than A[i] and``        ``// previous element are not from B[]``        ``else` `if` `(B[j] < A[i] && prev != ``1``)``        ``{``            ` `            ``// Update prev``            ``prev = ``1``;` `            ``// Update j``            ``j++;``        ``}` `        ``// If A[i] equal to B[j]``        ``else` `if` `(A[i] == B[j])``        ``{``            ` `            ``// If previous element``            ``// are not from B[]``            ``if` `(prev != ``1``)``            ``{``                ` `                ``// Update prev``                ``prev = ``1``;` `                ``// Update j``                ``j++;``            ``}` `            ``// If previous element is``            ``// not from A[]``            ``else``            ``{``                ` `                ``// Update prev``                ``prev = ``0``;` `                ``// Update i``                ``i++;``            ``}``        ``}` `        ``// If it is not possible to merge two``        ``// sorted arrays with given conditions``        ``else``        ``{``            ` `            ``// Update flag``            ``flag = ``false``;``            ``break``;``        ``}``    ``}``    ``return` `flag;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[] A = { ``3``, ``5``, ``8` `};``    ``int``[] B = { ``2``, ``4``, ``6` `};``    ``int` `N = A.length;` `    ``if` `(checkIfPossibleMerge(A, B, N))``    ``{``        ``System.out.println(``"Yes"``);``    ``}``    ``else``    ``{``        ``System.out.println(``"No"``);``    ``}``}``}` `// This code is contributed by akhilsaini`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to check if it is possible``# to merge the two given arrays with``# given conditions``def` `checkIfPossibleMerge(A, B, N):``    ` `    ``# Stores index of``    ``# the array A[]``    ``i ``=` `0` `    ``# Stores index of``    ``# the array  B[]``    ``j ``=` `0` `    ``# Check if the previous element``    ``# are from the array A[] or from``    ``# the array B[]``    ``prev ``=` `-``1` `    ``# Check if it is possible to merge``    ``# the two given sorted arrays with``    ``# given conditions``    ``flag ``=` `1` `    ``# Traverse both the arrays``    ``while` `(i < N ``and` `j < N):` `        ``# If A[i] is less than B[j] and``        ``# previous element are not from A[]``        ``if` `(A[i] < B[j] ``and` `prev !``=` `0``):` `            ``# Update prev``            ``prev ``=` `0` `            ``# Update i``            ``i ``+``=` `1` `        ``# If B[j] is less than A[i] and``        ``# previous element are not from B[]``        ``elif` `(B[j] < A[i] ``and` `prev !``=` `1``):` `            ``# Update prev``            ``prev ``=` `1` `            ``# Update j``            ``j ``+``=` `1` `        ``# If A[i] equal to B[j]``        ``elif` `(A[i] ``=``=` `B[j]):` `            ``# If previous element``            ``# are not from B[]``            ``if` `(prev !``=` `1``):``                ` `                ``# Update prev``                ``prev ``=` `1` `                ``# Update j``                ``j ``+``=` `1` `            ``# If previous element is``            ``# not from A[]``            ``else``:` `                ``# Update prev``                ``prev ``=` `0` `                ``# Update i``                ``i ``+``=` `1` `        ``# If it is not possible to merge two``        ``# sorted arrays with given conditions``        ``else``:` `            ``# Update flag``            ``flag ``=` `0``            ``break` `    ``return` `flag` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:` `    ``A ``=` `[ ``3``, ``5``, ``8` `]``    ``B ``=` `[ ``2``, ``4``, ``6` `]``    ``N ``=` `len``(A)` `    ``if` `(checkIfPossibleMerge(A, B, N)):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# This code is contributed by akhilsaini`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG{` `// Function to check if it is possible to merge``// the two given arrays with given conditions``static` `bool` `checkIfPossibleMerge(``int``[] A, ``int``[] B,``                                 ``int` `N)``{``    ` `    ``// Stores index of``    ``// the array A[]``    ``int` `i = 0;` `    ``// Stores index of``    ``// the array  B[]``    ``int` `j = 0;` `    ``// Check if the previous element are``    ``// from the array A[] or from the``    ``// array B[]``    ``int` `prev = -1;` `    ``// Check if it is possible to merge``    ``// the two given sorted arrays with``    ``// given conditions``    ``bool` `flag = ``true``;` `    ``// Traverse both the arrays``    ``while` `(i < N && j < N)``    ``{``        ` `        ``// If A[i] is less than B[j] and``        ``// previous element are not from A[]``        ``if` `(A[i] < B[j] && prev != 0)``        ``{``            ` `            ``// Update prev``            ``prev = 0;` `            ``// Update i``            ``i++;``        ``}` `        ``// If B[j] is less than A[i] and``        ``// previous element are not from B[]``        ``else` `if` `(B[j] < A[i] && prev != 1)``        ``{``            ` `            ``// Update prev``            ``prev = 1;` `            ``// Update j``            ``j++;``        ``}` `        ``// If A[i] equal to B[j]``        ``else` `if` `(A[i] == B[j])``        ``{``            ` `            ``// If previous element``            ``// are not from B[]``            ``if` `(prev != 1)``            ``{``                ` `                ``// Update prev``                ``prev = 1;` `                ``// Update j``                ``j++;``            ``}` `            ``// If previous element is``            ``// not from A[]``            ``else``            ``{``                ` `                ``// Update prev``                ``prev = 0;` `                ``// Update i``                ``i++;``            ``}``        ``}` `        ``// If it is not possible to merge two``        ``// sorted arrays with given conditions``        ``else``        ``{``            ` `            ``// Update flag``            ``flag = ``false``;``            ``break``;``        ``}``    ``}``    ``return` `flag;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] A = { 3, 5, 8 };``    ``int``[] B = { 2, 4, 6 };``    ``int` `N = A.Length;` `    ``if` `(checkIfPossibleMerge(A, B, N))``    ``{``        ``Console.WriteLine(``"Yes"``);``    ``}``    ``else``    ``{``        ``Console.WriteLine(``"No"``);``    ``}``}``}` `// This code is contributed by akhilsaini`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up