Wednesday, 27 Oct 2021

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

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: YesExplanation: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++    #include using namespace std;    void checkArrays(int arr1[], int arr2[], int N) {               int count = 0;                  bool flag = true;             for (int i = 0; i < N; i++) {                     if (arr1[i] != arr2[i]) {                if (arr1[i] == 0)                                     count++;             else {                                     count--;                 if (count < 0) {                     flag = 0;                     break;                 }             }         }     }                  if (flag && count == 0)         cout