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[1] and arr1[3], it becomes arr1[] = {0, 1, 1, 0}.
Swap arr1[0] and arr1[2], 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 <bits/stdc++.h>
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;
}
|
Java
public class GFG
{
static void checkArrays( int arr1[], int arr2[], int N)
{
int count = 0 ;
boolean 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 = false ;
break ;
}
}
}
}
if ((flag && (count == 0 )) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args)
{
int arr1[] = { 0 , 0 , 1 , 1 };
int arr2[] = { 1 , 1 , 0 , 0 };
int N = arr1.length;
checkArrays(arr1, arr2, N);
}
}
|
Python3
def checkArrays(arr1, arr2, N):
count = 0
flag = True
for i in range (N):
if (arr1[i] ! = arr2[i]):
if (arr1[i] = = 0 ):
count + = 1
else :
count - = 1
if (count < 0 ):
flag = 0
break
if (flag and count = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
arr1 = [ 0 , 0 , 1 , 1 ]
arr2 = [ 1 , 1 , 0 , 0 ]
N = len (arr1)
checkArrays(arr1, arr2, N)
|
C#
using System;
class GFG{
static 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 = false ;
break ;
}
}
}
}
if ((flag && (count == 0)) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static public void Main()
{
int [] arr1 = { 0, 0, 1, 1 };
int [] arr2 = { 1, 1, 0, 0 };
int N = arr1.Length;
checkArrays(arr1, arr2, N);
}
}
|
Javascript
<script>
function checkArrays(arr1,arr2,N)
{
let count = 0;
let flag = true ;
for (let i = 0; i < N; i++) {
if (arr1[i] != arr2[i])
{
if (arr1[i] == 0)
count++;
else
{
count--;
if (count < 0)
{
flag = false ;
break ;
}
}
}
}
if ((flag && (count == 0)) == true )
document.write( "Yes" );
else
document.write( "No" );
}
let arr1 = [ 0, 0, 1, 1 ];
let arr2 = [ 1, 1, 0, 0 ];
let N = arr1.length;
checkArrays(arr1, arr2, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- Define two binary arrays arr1[] and arr2[] of the same size.
- Calculate the size of the arrays using the sizeof() operator and store it in the variable n.
- Initialize three integer variables zero_count, one_count, and mismatch_count to zero.
- Use a for loop to iterate through the arrays from 0 to n-1.
- Inside the for loop, check if the current element of arr1 is zero. If yes, increment the zero_count variable. Otherwise, increment the one_count variable.
- Also inside the for loop, check if the current element of arr1 is not equal to the current element of arr2. If yes, increment the mismatch_count variable.
- After the for loop, check if the zero_count and one_count variables are equal and the mismatch_count variable is even. If yes, print “Yes”. Otherwise, print “No”.
- End the program.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int main() {
int arr1[] = {0, 0, 1, 1};
int arr2[] = {1, 1, 0, 0};
int n = sizeof (arr1) / sizeof (arr1[0]);
int zero_count = 0, one_count = 0, mismatch_count = 0;
for ( int i = 0; i < n; i++) {
if (arr1[i] == 0) {
zero_count++;
} else {
one_count++;
}
if (arr1[i] != arr2[i]) {
mismatch_count++;
}
}
if (zero_count == one_count && mismatch_count % 2 == 0) {
cout << "Yes" ;
} else {
cout << "No" ;
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
int [] arr1 = { 0 , 0 , 1 , 1 };
int [] arr2 = { 1 , 1 , 0 , 0 };
int n = arr1.length;
int zeroCount = 0 , oneCount = 0 , mismatchCount = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr1[i] == 0 ) {
zeroCount++;
} else {
oneCount++;
}
if (arr1[i] != arr2[i]) {
mismatchCount++;
}
}
if (zeroCount == oneCount && mismatchCount % 2 == 0 ) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
arr1 = [ 0 , 0 , 1 , 1 ]
arr2 = [ 1 , 1 , 0 , 0 ]
n = len (arr1)
zero_count = 0
one_count = 0
mismatch_count = 0
for i in range (n):
if arr1[i] = = 0 :
zero_count + = 1
else :
one_count + = 1
if arr1[i] ! = arr2[i]:
mismatch_count + = 1
if zero_count = = one_count and mismatch_count % 2 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class Program
{
static void Main()
{
int [] arr1 = { 0, 0, 1, 1 };
int [] arr2 = { 1, 1, 0, 0 };
int n = arr1.Length;
int zeroCount = 0, oneCount = 0, mismatchCount = 0;
for ( int i = 0; i < n; i++)
{
if (arr1[i] == 0)
{
zeroCount++;
}
else
{
oneCount++;
}
if (arr1[i] != arr2[i])
{
mismatchCount++;
}
}
if (zeroCount == oneCount && mismatchCount % 2 == 0)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function main() {
let arr1 = [0, 0, 1, 1];
let arr2 = [1, 1, 0, 0];
let n = arr1.length;
let zero_count = 0;
let one_count = 0;
let mismatch_count = 0;
for (let i = 0; i < n; i++) {
if (arr1[i] == 0) {
zero_count++;
} else {
one_count++;
}
if (arr1[i] !== arr2[i]) {
mismatch_count++;
}
}
if (zero_count === one_count && mismatch_count % 2 === 0) {
console.log( "Yes" );
} else {
console.log( "No" );
}
}
main();
|
Output:
Yes
Time Complexity: The time complexity of the given code is O(n), where n is the size of the arrays. This is because the code uses a single loop to iterate through both arrays, and each operation inside the loop takes constant time.
Auxiliary Space: The space complexity of the code is O(1), as it uses only a few variables to store the counts and does not allocate any additional memory based on the input size.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Oct, 2023
Like Article
Save Article