Check if two arrays can be made equal by swapping pairs of one of the arrays | C ++ 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 ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , print " Yes " . Otherwise " No " ; Driver Code ; Given arrays ; Size of the array
#include <bits/stdc++.h> NEW_LINE 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 << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int arr1 [ ] = { 0 , 0 , 1 , 1 } ; int arr2 [ ] = { 1 , 1 , 0 , 0 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; checkArrays ( arr1 , arr2 , N ) ; return 0 ; }