Construct Binary Array having same number of unequal elements with two other Arrays
Given two binary arrays A[] and B[] of size N, the task is to construct the lexicographically smallest binary array X[] such that the number of non-equal elements in A and X is equal to the number of non-equal elements in B and X. If such an array does not exist return -1.
Note: If there are multiple possible answers, print any of them
Examples:
Input: N = 5, A = {0, 0, 1, 0, 0}, B = {1, 0, 0, 1, 1}
Output: {0, 0, 0, 0, 1}
Explanation: Consider arrays X = {0, 0, 0, 0, 1}. It can be seen 3rd and 5th elements of A[] and X[] are not equal. So, there are 2 unequal elements in A and X. Similarly, it can be seen that 1st and 4th elements of X[] and B[] are unequal. Again, the number of unequal elements is 2, which is same as the number of unequal elements in A[] and X[]. Hence, X = {0, 0, 0, 0, 1} is the required array.
Input: N = 1, A = {0}, B = {1}
Output: -1
Approach: To solve the problem follow the below observations:
Let f(X, Y) denote the number of unequal elements in two arrays X and Y. Let D = f(X, A) – f(X, B). Here, we want to find the lexicographically smallest array X such that D = 0.
Here, we can observe that :
- For i such that A[i] = B[i], whether X[i] is 0 or 1 does not impact the D.
- For i such that A[i] ? B[i], X[i] = A[i] adds 1 to the D and X[i] = B[i] adds (-1) to the D.
Thus, to get D = 0, among the indices where A[i] != B[i], the indices where X[i] = A[i] must be equal to the indices having
X[i] = B[i]. It can be concluded from the above observation that it is impossible to make D = 0 (and hence to construct the array X), if the indices such that A[i] != B[i] are odd in number.
To make sure the array is lexicographically smallest:
- As we have already seen, the indices such that A[i] = B[i] do not contribute to D, hence we would make X[i] = 0 for such indices.
- For indices with A[i] ? B[i], following greedy approach is used to ensure the lexicographically smallest resultant array:
- Iterate through such indices. Prioritize assigning 0 to X[i] as long as it is possible [i.e., difference with any one of A[i] or B[i] reaches D/2] and then fill the other indices with 1.
Follow the steps mentioned below to implement the idea:
- Iterate the arrays from i = 0 to N-1:
- Find the value of D (i.e. the number of indices where A[i] and B[i] are different).
- If D is odd return -1.
- Otherwise, do the following:
- Create an array to store X[].
- Now iterate from i = 0 to N-1:
- If A[i] and B[i] are same push 0 in X[].
- Otherwise, push 0 if difference with any of A[] or B[] has not reached the value of D/2.
- Else, push the value of A[i] or B[i] depending on the difference with which has not yet become D/2.
- Return X[] as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > printArray( int N, int A[], int B[])
{
int count = 0;
vector< int > ans;
for ( int i = 0; i < N; i++) {
if (A[i] != B[i]) {
count++;
}
}
if (count % 2 != 0) {
ans.push_back(-1);
return ans;
}
int countA = 0;
int countB = 0;
for ( int i = 0; i < N; i++) {
if (A[i] != B[i]) {
if (A[i] == 0) {
countA++;
if (countA <= count / 2) {
ans.push_back(0);
}
else {
ans.push_back(1);
}
}
if (B[i] == 0) {
countB++;
if (countB <= count / 2) {
ans.push_back(0);
}
else {
ans.push_back(1);
}
}
}
else {
ans.push_back(0);
}
}
return ans;
}
int main()
{
int A[] = { 0, 0, 1, 0, 0 };
int B[] = { 1, 0, 0, 1, 1 };
int N = sizeof (A) / sizeof (A[0]);
vector< int > res = printArray(N, A, B);
for ( int x : res)
cout << x << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static ArrayList<Integer>
printArray( int N, int A[], int B[])
{
int count = 0 ;
ArrayList<Integer> ans = new ArrayList<Integer>();
for ( int i = 0 ; i < N; i++) {
if (A[i] != B[i]) {
count++;
}
}
if (count % 2 != 0 ) {
ans.add(- 1 );
return ans;
}
int countA = 0 ;
int countB = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] != B[i]) {
if (A[i] == 0 ) {
countA++;
if (countA <= count / 2 ) {
ans.add( 0 );
}
else {
ans.add( 1 );
}
}
if (B[i] == 0 ) {
countB++;
if (countB <= count / 2 ) {
ans.add( 0 );
}
else {
ans.add( 1 );
}
}
}
else {
ans.add( 0 );
}
}
return ans;
}
public static void main(String[] args)
{
int A[] = { 0 , 0 , 1 , 0 , 0 };
int B[] = { 1 , 0 , 0 , 1 , 1 };
int N = A.length;
ArrayList<Integer> res = printArray(N, A, B);
for (Integer x : res)
System.out.print(x + " " );
}
}
|
Python3
def printArray(N, A, B):
count = 0
ans = [ 0 ] * N
for i in range ( 0 , N):
if (A[i] ! = B[i]):
count + = 1
if (count % 2 ! = 0 ):
ans[i] = - 1
return ans
countA = 0
countB = 0
for i in range ( 0 , N):
if (A[i] ! = B[i]):
if (A[i] = = 0 ):
countA + = 1
if (countA < = count / 2 ):
ans[i] = 0
else :
ans[i] = 1
if (B[i] = = 0 ):
countB + = 1
if (countB < = count / 2 ):
ans[i] = 0
else :
ans[i] = 1
else :
ans[i] = 0
return ans
A = [ 0 , 0 , 1 , 0 , 0 ]
B = [ 1 , 0 , 0 , 1 , 1 ]
N = len (A)
res = printArray(N, A, B)
for x in range ( 0 , len (res)):
print (res[x])
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int [] printArray( int N, int [] A, int [] B)
{
int count = 0;
int [] ans = new int [N];
int k = 0;
for ( int i = 0; i < N; i++) {
if (A[i] != B[i]) {
count++;
}
}
if (count % 2 != 0) {
ans[k++] = -1;
return ans;
}
int countA = 0;
int countB = 0;
for ( int i = 0; i < N; i++) {
if (A[i] != B[i]) {
if (A[i] == 0) {
countA++;
if (countA <= count / 2) {
ans[k++] = 0;
}
else {
ans[k++] = 1;
}
}
if (B[i] == 0) {
countB++;
if (countB <= count / 2) {
ans[k++] = 0;
}
else {
ans[k++] = 1;
}
}
}
else {
ans[k++] = 0;
}
}
return ans;
}
public static void Main( string [] args)
{
int [] A = { 0, 0, 1, 0, 0 };
int [] B = { 1, 0, 0, 1, 1 };
int N = 5;
int [] res = printArray(N, A, B);
for ( int i = 0; i < res.Length; i++)
Console.Write(res[i] + " " );
return ;
}
}
|
Javascript
const printArray = (N, A, B) => {
let count = 0;
let ans = [];
for (let i = 0; i < N; i++) {
if (A[i] != B[i]) {
count++;
}
}
if (count % 2 != 0) {
ans.push(-1);
return ans;
}
let countA = 0;
let countB = 0;
for (let i = 0; i < N; i++) {
if (A[i] != B[i]) {
if (A[i] == 0) {
countA++;
if (countA <= parseInt(count / 2)) {
ans.push(0);
}
else {
ans.push(1);
}
}
if (B[i] == 0) {
countB++;
if (countB <= parseInt(count / 2)) {
ans.push(0);
}
else {
ans.push(1);
}
}
}
else {
ans.push(0);
}
}
return ans;
}
let A = [0, 0, 1, 0, 0];
let B = [1, 0, 0, 1, 1];
let N = A.length;
let res = printArray(N, A, B);
for (let x in res)
console.log(`${res[x]} `);
|
PHP
<?php
function printArray( $N , $A , $B ) {
$count = 0;
$ans = array ();
for ( $i = 0; $i < $N ; $i ++) {
if ( $A [ $i ] != $B [ $i ])
$count ++;
}
if ( $count % 2 != 0) {
array_push ( $ans , -1);
return $ans ;
}
$countA = 0;
$countB = 0;
for ( $i = 0; $i < $N ; $i ++) {
if ( $A [ $i ] != $B [ $i ]) {
if ( $A [ $i ] == 0) {
$countA ++;
if ( $countA <= $count / 2)
array_push ( $ans , 0);
else
array_push ( $ans , 1);
}
if ( $B [ $i ] == 0) {
$countB ++;
if ( $countB <= $count / 2)
array_push ( $ans , 0);
else
array_push ( $ans , 1);
}
}
else {
array_push ( $ans , 0);
}
}
return $ans ;
}
$A = array (0, 0, 1, 0, 0);
$B = array (1, 0, 0, 1, 1);
$N = count ( $A );
$res = printArray( $N , $A , $B );
for ( $x = 0; $x < count ( $res ); $x ++)
echo $res [ $x ] . " " ;
?>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...