Check if an array can be converted to another given array by swapping pairs of unequal elements
Last Updated :
30 Apr, 2021
Given two arrays arr1[] and arr2[] of size N, consisting of binary integers, the task is to check if arr1[] can be converted to arr2[] by swapping any pair of array elements (arr1[i], arr1[j]) such that i < j and arr1[i] is 1 and arr1[j] is 0 (any number of times). If it is possible to do so, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr1[] = {0, 1, 1, 0}, arr2[] = {0, 0 1, 1}
Output: Yes
Explanation:
The array arr1[] can be made equal to arr2[] by swapping arr1[1] and arr1[3].
Input: arr1[] = {1, 0, 1}, arr2[] = {0, 1, 0}
Output: No
Approach: The idea to solve this problem is based on the following observations:
- The operation doesn’t change the frequency of the number of ones and zeros in array arr1[], so if the number of 0s or 1s are different among arrays, they can never become equal with the above operation.
- If some prefix of arr2[] contains more 1s than the prefix of arr1[] of the same length, then it is not possible to make arr1[] and arr2[] equal, since 1 can be shifted to right only.
- Otherwise, in all other cases, arrays can be made equal.
Follow the below steps to solve the problem:
- Initialize a variable, say count with 0, to store the differences of the prefix sum of arr1[] and arr2[].
- Count the number of 1s and 0s in both arrays and check if the number of 1s and 0s in arr1[] is not equal to the number of 1s and 0s in arr2[] and then print “No”.
- Iterate over the range [1, N – 1] using the variable i and do the following:
- Add the value (arr1[i] – arr2[i]) to the variable count.
- If the value of count is less than 0, then print “No” else continue for the next pair of elements.
- After completing the above steps, if the count isn’t negative at any step then print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void canMakeEqual( int arr1[], int arr2[], int N)
{
int count = 0;
int arr1_one = 0, arr1_zero = 0;
int arr2_one = 0, arr2_zero = 0;
for ( int i = 0; i < N; i++) {
if (arr1[i] == 1) {
arr1_one++;
}
else if (arr1[i] == 0) {
arr1_zero++;
}
if (arr2[i] == 1) {
arr2_one++;
}
else if (arr2[i] == 0) {
arr2_zero++;
}
}
if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
cout << "No" ;
return ;
}
for ( int i = 0; i < N; i++) {
count = count + (arr1[i] - arr2[i]);
if (count < 0) {
cout << "No" ;
return ;
}
}
cout << "Yes" ;
}
int main()
{
int arr1[] = { 0, 1, 1, 0 };
int arr2[] = { 0, 0, 1, 1 };
int N = sizeof (arr1) / sizeof (arr1[0]);
canMakeEqual(arr1, arr2, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void canMakeEqual( int []arr1, int []arr2, int N)
{
int count = 0 ;
int arr1_one = 0 , arr1_zero = 0 ;
int arr2_one = 0 , arr2_zero = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr1[i] == 1 ) {
arr1_one++;
}
else if (arr1[i] == 0 ) {
arr1_zero++;
}
if (arr2[i] == 1 ) {
arr2_one++;
}
else if (arr2[i] == 0 ) {
arr2_zero++;
}
}
if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
System.out.print( "No" );
return ;
}
for ( int i = 0 ; i < N; i++) {
count = count + (arr1[i] - arr2[i]);
if (count < 0 ) {
System.out.print( "No" );
return ;
}
}
System.out.print( "Yes" );
}
public static void main(String[] args)
{
int []arr1 = { 0 , 1 , 1 , 0 };
int []arr2 = { 0 , 0 , 1 , 1 };
int N = arr1.length;
canMakeEqual(arr1, arr2, N);
}
}
|
Python3
def canMakeEqual(arr1, arr2, N):
count = 0
arr1_one = 0
arr1_zero = 0
arr2_one = 0
arr2_zero = 0
for i in range (N):
if (arr1[i] = = 1 ):
arr1_one + = 1
elif (arr1[i] = = 0 ):
arr1_zero + = 1
if (arr2[i] = = 1 ):
arr2_one + = 1
elif (arr2[i] = = 0 ):
arr2_zero + = 1
if (arr1_one ! = arr2_one or arr1_zero ! = arr2_zero):
print ( "No" )
return
for i in range (N):
count = count + (arr1[i] - arr2[i])
if (count < 0 ):
print ( "No" )
return
print ( "Yes" )
if __name__ = = '__main__' :
arr1 = [ 0 , 1 , 1 , 0 ]
arr2 = [ 0 , 0 , 1 , 1 ]
N = len (arr1)
canMakeEqual(arr1, arr2, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void canMakeEqual( int []arr1, int []arr2, int N)
{
int count = 0;
int arr1_one = 0, arr1_zero = 0;
int arr2_one = 0, arr2_zero = 0;
for ( int i = 0; i < N; i++) {
if (arr1[i] == 1) {
arr1_one++;
}
else if (arr1[i] == 0) {
arr1_zero++;
}
if (arr2[i] == 1) {
arr2_one++;
}
else if (arr2[i] == 0) {
arr2_zero++;
}
}
if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
Console.WriteLine( "No" );
return ;
}
for ( int i = 0; i < N; i++) {
count = count + (arr1[i] - arr2[i]);
if (count < 0) {
Console.WriteLine( "No" );
return ;
}
}
Console.WriteLine( "Yes" );
}
public static void Main()
{
int []arr1 = { 0, 1, 1, 0 };
int []arr2 = { 0, 0, 1, 1 };
int N = arr1.Length;
canMakeEqual(arr1, arr2, N);
}
}
|
Javascript
<script>
function canMakeEqual(arr1, arr2, N)
{
var count = 0;
var arr1_one = 0, arr1_zero = 0;
var arr2_one = 0, arr2_zero = 0;
for ( var i = 0; i < N; i++)
{
if (arr1[i] == 1)
{
arr1_one++;
}
else if (arr1[i] == 0)
{
arr1_zero++;
}
if (arr2[i] == 1)
{
arr2_one++;
}
else if (arr2[i] == 0)
{
arr2_zero++;
}
}
if (arr1_one != arr2_one ||
arr1_zero != arr2_zero)
{
document.write( "No" );
return ;
}
for ( var i = 0; i < N; i++)
{
count = count + (arr1[i] - arr2[i]);
if (count < 0)
{
document.write( "No" );
return ;
}
}
document.write( "Yes" );
}
var arr1 = [ 0, 1, 1, 0 ];
var arr2 = [ 0, 0, 1, 1 ];
var N = arr1.length;
canMakeEqual(arr1, arr2, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...