# Check if two arrays can be made equal by swapping pairs of one of the arrays

• Difficulty Level : Medium
• Last Updated : 06 May, 2021

Given two binary arrays arr1[] and arr2[] of the same size, the task is to make both the arrays equal by swapping pairs of arr1[ ] only if arr1[i] = 0 and arr1[j] = 1 (0 ≤ i < j < N)). If it is possible to make both the arrays equal, print “Yes”. Otherwise, print “No”.

Examples:

Input: arr1[] = {0, 0, 1, 1}, arr2[] = {1, 1, 0, 0}
Output: Yes
Explanation:
Swap arr1 and arr1, it becomes arr1[] = {0, 1, 1, 0}.
Swap arr1 and arr1, it becomes arr1[] = {1, 1, 0, 0}.

Input: arr1[] = {1, 0, 1, 0, 1}, arr2[] = {0, 1, 0, 0, 1}
Output: No

Approach: Follow the steps below to solve the problem:

• Initialize two variable, say count and flag (= true).
• Traverse the array and for every array element, perform the following operations:
• If arr1[i] != arr2[i]:
• If arr1[i] == 0, increment count by 1.
• Otherwise, decrement count by 1 and if count < 0, update flag = false.
• If flag is equal to true, print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program for above approach` `#include ``using` `namespace` `std;` `// Function to check if two arrays``// can be made equal or not by swapping``// pairs of only one of the arrays``void` `checkArrays(``int` `arr1[], ``int` `arr2[], ``int` `N)``{``    ``// Stores elements required``    ``// to be replaced``    ``int` `count = 0;` `    ``// To check if the arrays``    ``// can be made equal or not``    ``bool` `flag = ``true``;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// If array elements are not equal``        ``if` `(arr1[i] != arr2[i]) {` `            ``if` `(arr1[i] == 0)` `                ``// Increment count by 1``                ``count++;``            ``else` `{` `                ``// Decrement count by 1``                ``count--;``                ``if` `(count < 0) {``                    ``flag = 0;``                    ``break``;``                ``}``            ``}``        ``}``    ``}` `    ``// If flag is true and count is 0,``    ``// print "Yes". Otherwise "No"``    ``if` `(flag && count == 0)``        ``cout << ``"Yes"` `<< endl;``    ``else``        ``cout << ``"No"` `<< endl;``}` `// Driver Code``int` `main()``{``    ``// Given arrays``    ``int` `arr1[] = { 0, 0, 1, 1 };``    ``int` `arr2[] = { 1, 1, 0, 0 };` `    ``// Size of the array``    ``int` `N = ``sizeof``(arr1) / ``sizeof``(arr1);``    ``checkArrays(arr1, arr2, N);` `    ``return` `0;``}`

## Java

 `// Java program for above approach``public` `class` `GFG``{` `  ``// Function to check if two arrays``  ``// can be made equal or not by swapping``  ``// pairs of only one of the arrays``  ``static` `void` `checkArrays(``int` `arr1[], ``int` `arr2[], ``int` `N)``  ``{` `    ``// Stores elements required``    ``// to be replaced``    ``int` `count = ``0``;` `    ``// To check if the arrays``    ``// can be made equal or not``    ``boolean` `flag = ``true``;` `    ``// Traverse the array``    ``for` `(``int` `i = ``0``; i < N; i++) {` `      ``// If array elements are not equal``      ``if` `(arr1[i] != arr2[i])``      ``{``        ``if` `(arr1[i] == ``0``)` `          ``// Increment count by 1``          ``count++;``        ``else``        ``{` `          ``// Decrement count by 1``          ``count--;``          ``if` `(count < ``0``)``          ``{``            ``flag = ``false``;``            ``break``;``          ``}``        ``}``      ``}``    ``}` `    ``// If flag is true and count is 0,``    ``// print "Yes". Otherwise "No"``    ``if` `((flag && (count == ``0``)) == ``true``)``      ``System.out.println(``"Yes"``);``    ``else``      ``System.out.println(``"No"``);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main (String[] args)``  ``{` `    ``// Given arrays``    ``int` `arr1[] = { ``0``, ``0``, ``1``, ``1` `};``    ``int` `arr2[] = { ``1``, ``1``, ``0``, ``0` `};` `    ``// Size of the array``    ``int` `N = arr1.length;  ``    ``checkArrays(arr1, arr2, N);``  ``}``}` `// This code is contributed by AnkThon`

## Python3

 `# Python3 program for above approach` `# Function to check if two arrays``# can be made equal or not by swapping``# pairs of only one of the arrays``def` `checkArrays(arr1, arr2, N):``  ` `    ``# Stores elements required``    ``# to be replaced``    ``count ``=` `0` `    ``# To check if the arrays``    ``# can be made equal or not``    ``flag ``=` `True` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):` `        ``# If array elements are not equal``        ``if` `(arr1[i] !``=` `arr2[i]):` `            ``if` `(arr1[i] ``=``=` `0``):` `                ``# Increment count by 1``                ``count ``+``=` `1``            ``else``:` `                ``# Decrement count by 1``                ``count ``-``=` `1``                ``if` `(count < ``0``):``                    ``flag ``=` `0``                    ``break` `    ``# If flag is true and count is 0,``    ``# pr"Yes". Otherwise "No"``    ``if` `(flag ``and` `count ``=``=` `0``):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given arrays``    ``arr1 ``=` `[``0``, ``0``, ``1``, ``1``]``    ``arr2 ``=` `[``1``, ``1``, ``0``, ``0``]` `    ``# Size of the array``    ``N ``=` `len``(arr1)` `    ``checkArrays(arr1, arr2, N)` `    ``# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to check if two arrays``  ``// can be made equal or not by swapping``  ``// pairs of only one of the arrays``  ``static` `void` `checkArrays(``int``[] arr1, ``int``[] arr2, ``int` `N)``  ``{` `    ``// Stores elements required``    ``// to be replaced``    ``int` `count = 0;` `    ``// To check if the arrays``    ``// can be made equal or not``    ``bool` `flag = ``true``;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `      ``// If array elements are not equal``      ``if` `(arr1[i] != arr2[i])``      ``{``        ``if` `(arr1[i] == 0)` `          ``// Increment count by 1``          ``count++;``        ``else``        ``{` `          ``// Decrement count by 1``          ``count--;``          ``if` `(count < 0)``          ``{``            ``flag = ``false``;``            ``break``;``          ``}``        ``}``      ``}``    ``}` `    ``// If flag is true and count is 0,``    ``// print "Yes". Otherwise "No"``    ``if` `((flag && (count == 0)) == ``true``)``      ``Console.WriteLine(``"Yes"``);``    ``else``      ``Console.WriteLine(``"No"``);``  ``}` `// Driver Code``static` `public` `void` `Main()``{``    ``// Given arrays``    ``int``[] arr1 = { 0, 0, 1, 1 };``    ``int``[] arr2 = { 1, 1, 0, 0 };` `    ``// Size of the array``    ``int` `N = arr1.Length;  ``    ``checkArrays(arr1, arr2, N);``}``}` `// This code is contributed by susmitakundugoaldanga.`

## Javascript

 ``

Output:

`Yes`

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

My Personal Notes arrow_drop_up