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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if arr1[] can be
// converted to arr2[] by swapping pair
// (i, j) such that i < j and arr[i] is
// 1 and arr[j] is 0
void canMakeEqual(int arr1[], int arr2[], int N)
{
// Stores the differences of prefix
// sum of arr1 and arr2
int count = 0;
// Stores the count of 1 and
// zero of arr1
int arr1_one = 0, arr1_zero = 0;
// Stores the count of 1 and
// zero of arr2
int arr2_one = 0, arr2_zero = 0;
// Iterate in the range [0, N - 1]
for (int i = 0; i < N; i++) {
// If arr1[i] is 1, then
// increment arr1_one by one
if (arr1[i] == 1) {
arr1_one++;
}
// Otherwise increment
// arr1_zero by one
else if (arr1[i] == 0) {
arr1_zero++;
}
// If arr2[i] is 1, then
// increment arr2_one by one
if (arr2[i] == 1) {
arr2_one++;
}
// Otherwise increment
// arr2_zero by one
else if (arr2[i] == 0) {
arr2_zero++;
}
}
// Check if number of 1s and 0s
// of arr1 is equal to number of
// 1s and 0s of arr2 respectievly
if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
cout << "No";
return;
}
// Iterate over the range [0, N-1]
for (int i = 0; i < N; i++) {
// Increment count by differences
// arr1[i] and arr2[i]
count = count + (arr1[i] - arr2[i]);
// Check if number of 1's in
// arr2 are more than arr1 and
// then print "No"
if (count < 0) {
cout << "No";
return;
}
}
// Finally, print "Yes"
cout << "Yes";
}
// Driver Code
int main()
{
// Given input arrays
int arr1[] = { 0, 1, 1, 0 };
int arr2[] = { 0, 0, 1, 1 };
// Size of the array
int N = sizeof(arr1) / sizeof(arr1[0]);
// Function Call
canMakeEqual(arr1, arr2, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if arr1[] can be
// converted to arr2[] by swapping pair
// (i, j) such that i < j and arr[i] is
// 1 and arr[j] is 0
static void canMakeEqual(int []arr1, int []arr2, int N)
{
// Stores the differences of prefix
// sum of arr1 and arr2
int count = 0;
// Stores the count of 1 and
// zero of arr1
int arr1_one = 0, arr1_zero = 0;
// Stores the count of 1 and
// zero of arr2
int arr2_one = 0, arr2_zero = 0;
// Iterate in the range [0, N - 1]
for (int i = 0; i < N; i++) {
// If arr1[i] is 1, then
// increment arr1_one by one
if (arr1[i] == 1) {
arr1_one++;
}
// Otherwise increment
// arr1_zero by one
else if (arr1[i] == 0) {
arr1_zero++;
}
// If arr2[i] is 1, then
// increment arr2_one by one
if (arr2[i] == 1) {
arr2_one++;
}
// Otherwise increment
// arr2_zero by one
else if (arr2[i] == 0) {
arr2_zero++;
}
}
// Check if number of 1s and 0s
// of arr1 is equal to number of
// 1s and 0s of arr2 respectievly
if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
System.out.print("No");
return;
}
// Iterate over the range [0, N-1]
for (int i = 0; i < N; i++) {
// Increment count by differences
// arr1[i] and arr2[i]
count = count + (arr1[i] - arr2[i]);
// Check if number of 1's in
// arr2 are more than arr1 and
// then print "No"
if (count < 0) {
System.out.print("No");
return;
}
}
// Finally, print "Yes"
System.out.print("Yes");
}
// Driver Code
public static void main(String[] args)
{
// Given input arrays
int []arr1 = { 0, 1, 1, 0 };
int []arr2 = { 0, 0, 1, 1 };
// Size of the array
int N = arr1.length;
// Function Call
canMakeEqual(arr1, arr2, N);
}
}
// This code is contributed by code_hunt.
Python3
# Python 3 program for the above approach
# Function to check if arr1[] can be
# converted to arr2[] by swapping pair
# (i, j) such that i < j and arr[i] is
# 1 and arr[j] is 0
def canMakeEqual(arr1, arr2, N):
# Stores the differences of prefix
# sum of arr1 and arr2
count = 0
# Stores the count of 1 and
# zero of arr1
arr1_one = 0
arr1_zero = 0
# Stores the count of 1 and
# zero of arr2
arr2_one = 0
arr2_zero = 0
# Iterate in the range [0, N - 1]
for i in range(N):
# If arr1[i] is 1, then
# increment arr1_one by one
if (arr1[i] == 1):
arr1_one += 1
# Otherwise increment
# arr1_zero by one
elif(arr1[i] == 0):
arr1_zero += 1
# If arr2[i] is 1, then
# increment arr2_one by one
if (arr2[i] == 1):
arr2_one += 1
# Otherwise increment
# arr2_zero by one
elif (arr2[i] == 0):
arr2_zero += 1
# Check if number of 1s and 0s
# of arr1 is equal to number of
# 1s and 0s of arr2 respectievly
if (arr1_one != arr2_one or arr1_zero != arr2_zero):
print("No")
return
# Iterate over the range [0, N-1]
for i in range(N):
# Increment count by differences
# arr1[i] and arr2[i]
count = count + (arr1[i] - arr2[i])
# Check if number of 1's in
# arr2 are more than arr1 and
# then print "No"
if (count < 0):
print("No")
return
# Finally, print "Yes"
print("Yes")
# Driver Code
if __name__ == '__main__':
# Given input a
arr1 = [0, 1, 1, 0]
arr2 = [0, 0, 1, 1]
# Size of the array
N = len(arr1)
# Function Call
canMakeEqual(arr1, arr2, N)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if arr1[] can be
// converted to arr2[] by swapping pair
// (i, j) such that i < j and arr[i] is
// 1 and arr[j] is 0
static void canMakeEqual(int []arr1, int []arr2, int N)
{
// Stores the differences of prefix
// sum of arr1 and arr2
int count = 0;
// Stores the count of 1 and
// zero of arr1
int arr1_one = 0, arr1_zero = 0;
// Stores the count of 1 and
// zero of arr2
int arr2_one = 0, arr2_zero = 0;
// Iterate in the range [0, N - 1]
for (int i = 0; i < N; i++) {
// If arr1[i] is 1, then
// increment arr1_one by one
if (arr1[i] == 1) {
arr1_one++;
}
// Otherwise increment
// arr1_zero by one
else if (arr1[i] == 0) {
arr1_zero++;
}
// If arr2[i] is 1, then
// increment arr2_one by one
if (arr2[i] == 1) {
arr2_one++;
}
// Otherwise increment
// arr2_zero by one
else if (arr2[i] == 0) {
arr2_zero++;
}
}
// Check if number of 1s and 0s
// of arr1 is equal to number of
// 1s and 0s of arr2 respectievly
if (arr1_one != arr2_one || arr1_zero != arr2_zero) {
Console.WriteLine("No");
return;
}
// Iterate over the range [0, N-1]
for (int i = 0; i < N; i++) {
// Increment count by differences
// arr1[i] and arr2[i]
count = count + (arr1[i] - arr2[i]);
// Check if number of 1's in
// arr2 are more than arr1 and
// then print "No"
if (count < 0) {
Console.WriteLine("No");
return;
}
}
// Finally, print "Yes"
Console.WriteLine("Yes");
}
// Driver Code
public static void Main()
{
// Given input arrays
int []arr1 = { 0, 1, 1, 0 };
int []arr2 = { 0, 0, 1, 1 };
// Size of the array
int N = arr1.Length;
// Function Call
canMakeEqual(arr1, arr2, N);
}
}
// This code is contributed by bgangwar59.
JavaScript
<script>
// Javascript program for the above approach
// Function to check if arr1[] can be
// converted to arr2[] by swapping pair
// (i, j) such that i < j and arr[i] is
// 1 and arr[j] is 0
function canMakeEqual(arr1, arr2, N)
{
// Stores the differences of prefix
// sum of arr1 and arr2
var count = 0;
// Stores the count of 1 and
// zero of arr1
var arr1_one = 0, arr1_zero = 0;
// Stores the count of 1 and
// zero of arr2
var arr2_one = 0, arr2_zero = 0;
// Iterate in the range [0, N - 1]
for(var i = 0; i < N; i++)
{
// If arr1[i] is 1, then
// increment arr1_one by one
if (arr1[i] == 1)
{
arr1_one++;
}
// Otherwise increment
// arr1_zero by one
else if (arr1[i] == 0)
{
arr1_zero++;
}
// If arr2[i] is 1, then
// increment arr2_one by one
if (arr2[i] == 1)
{
arr2_one++;
}
// Otherwise increment
// arr2_zero by one
else if (arr2[i] == 0)
{
arr2_zero++;
}
}
// Check if number of 1s and 0s
// of arr1 is equal to number of
// 1s and 0s of arr2 respectievly
if (arr1_one != arr2_one ||
arr1_zero != arr2_zero)
{
document.write( "No");
return;
}
// Iterate over the range [0, N-1]
for(var i = 0; i < N; i++)
{
// Increment count by differences
// arr1[i] and arr2[i]
count = count + (arr1[i] - arr2[i]);
// Check if number of 1's in
// arr2 are more than arr1 and
// then print "No"
if (count < 0)
{
document.write( "No");
return;
}
}
// Finally, print "Yes"
document.write("Yes");
}
// Driver Code
// Given input arrays
var arr1 = [ 0, 1, 1, 0 ];
var arr2 = [ 0, 0, 1, 1 ];
// Size of the array
var N = arr1.length;
// Function Call
canMakeEqual(arr1, arr2, N);
// This code is contributed by rutvik_56
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Check if an array can be sorted by swapping pairs from indices consisting of unequal elements in another array Given an array A[] of size N and a binary array B[] of size N, the task is to check if the array A[] can be converted into a sorted array by swapping pairs (A[i], A[j]) if B[i] is not equal to B[j]. If the array A[] can be sorted, then print "Yes". Otherwise, print "No". Examples: Input: A[] = {3, 1
15 min read
Check if all array elements can be converted to K using given operations Given an integer array arr of size N and an integer K, the task is to make all elements of the array equal to K using the following operations: Choose an arbitrary subarray [l....r] of the input arrayReplace all values of this subarray equal to the [((r - l) + 2) / 2]th value in sorted subarray [l..
9 min read
Count of possible unique arrays after swapping elements at same index of given Arrays Given two arrays arr1[] and arr2[] with distinct elements of size N.The task is to count the total number of possible combinations after swapping elements at the same index of both the arrays such that there are no duplicates in both the arrays after performing the operation. Examples: Input: arr1[]
12 min read
Check if an array of pairs can be sorted by swapping pairs with different first elements Given an array arr[] consisting of N pairs, where each pair represents the value and ID respectively, the task is to check if it is possible to sort the array by the first element by swapping only pairs having different IDs. If it is possible to sort, then print "Yes". Otherwise, print "No". Example
11 min read
Sort an array by swapping elements of different type specified by another array Given two arrays a[] and b[] which contain the integer elements and their respective types (either type 0 or type 1) respectively, the task is to check if it is possible to sort the array in non-decreasing order by swapping elements of different types only. Examples: Input: a[] = {30, 20, 20, 10}, b
9 min read
Check if an Array can be converted to other by replacing pairs with GCD Given arrays A[] and B[] each of length N and A[i] has all the elements from 1 to N, the task is to check if it is possible to convert A[] to B[] by replacing any pair of A[] with their GCD. Examples: Input: N = 2, A[] = {1, 2}, B[] = {1, 1}Output: YES Explanation:First Operation - For A[], choose i
15 min read