Construct the Array using given bitwise AND, OR and XOR
Last Updated :
17 Apr, 2023
Given Bitwise AND, OR, and XOR of N elements of an array denoted by a, b, c. The task is to find the elements of the array. If there exists no such array then print “-1”.
Examples:
Input: N = 3, a = 4, b = 6, c = 6.
Output: {4, 4, 6}
Explanation:
Bitwise AND of array = 4 & 4 & 6 = 4
Bitwise OR of array = 4 | 4 | 6 = 6
Bitwise XOR of array = 4 ^ 4 ^ 6 = 6
Input: N = 2, a = 4, b = 6, c = 6.
Output: -1
Approach:
- For Bitwise AND, if ith bit is set in a, then in the array every element must have ith bit set because if even one element’s ith bit is 0 then bitwise AND of the array will result in ith bit to be 0.
- Secondly, if ith bit is not set in a, then OR and XOR values need to be handled simultaneously. If ith bit is set in b, then at least one element must have ith bit set. So, set ith bit in the only first element of the array.
- Now, if ith bit was set in b then ith bit must be checked in c. If that bit is set in c then there’s no problem as the first element’s ith bit is already set so 1 ^ 0 = 1. If that bit is not set in c then set the ith bit of the second element. Now, there will not be any effect in b and for c, 1 ^ 1 will be 0.
- Then, just calculate Bitwise AND, OR, and XOR of the array to check if it’s equal or not. If results are not equal then the array is not possible else given array is the answer.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findArray( int n, int a, int b, int c)
{
int arr[n + 1] = {};
for ( int bit = 30; bit >= 0; bit--) {
int set = a & (1 << bit);
if (set) {
for ( int i = 0; i < n; i++)
arr[i] |= set;
}
else {
if (b & (1 << bit)) {
arr[0] |= (1 << bit);
if (!(c & (1 << bit))) {
arr[1] |= (1 << bit);
}
}
}
}
int aa = INT_MAX, bb = 0, cc = 0;
for ( int i = 0; i < n; i++) {
aa &= arr[i];
bb |= arr[i];
cc ^= arr[i];
}
if (a == aa && b == bb && c == cc) {
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
else
cout << "-1" ;
}
int main()
{
int n = 3, a = 4, b = 6, c = 6;
findArray(n, a, b, c);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void findArray( int n, int a,
int b, int c)
{
int arr[] = new int [n + 1 ];
for ( int bit = 30 ; bit >= 0 ; bit--)
{
int set = a & ( 1 << bit);
if (set != 0 )
{
for ( int i = 0 ; i < n; i++)
arr[i] |= set;
}
else
{
if ((b & ( 1 << bit)) != 0 )
{
arr[ 0 ] |= ( 1 << bit);
if ((c & ( 1 << bit)) == 0 )
{
arr[ 1 ] |= ( 1 << bit);
}
}
}
}
int aa = Integer.MAX_VALUE, bb = 0 , cc = 0 ;
for ( int i = 0 ; i < n; i++)
{
aa &= arr[i];
bb |= arr[i];
cc ^= arr[i];
}
if (a == aa && b == bb && c == cc)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
else
System.out.println( "-1" );
}
public static void main(String[] args)
{
int n = 3 , a = 4 , b = 6 , c = 6 ;
findArray(n, a, b, c);
}
}
|
Python3
import sys
def findArray(n, a, b, c):
arr = [ 0 ] * (n + 1 )
for bit in range ( 30 , - 1 , - 1 ):
set = a & ( 1 << bit)
if ( set ):
for i in range (n):
arr[i] | = set
else :
if (b & ( 1 << bit)):
arr[ 0 ] | = ( 1 << bit)
if ( not (c & ( 1 << bit))):
arr[ 1 ] | = ( 1 << bit)
aa = sys.maxsize
bb = 0
cc = 0
for i in range (n):
aa & = arr[i]
bb | = arr[i]
cc ^ = arr[i]
if (a = = aa and b = = bb and c = = cc):
for i in range (n):
print (arr[i], end = " " )
else :
print ( "-1" )
if __name__ = = "__main__" :
n = 3
a = 4
b = 6
c = 6
findArray(n, a, b, c)
|
C#
using System;
class GFG{
static void findArray( int n, int a,
int b, int c)
{
int []arr = new int [n + 1];
for ( int bit = 30; bit >= 0; bit--)
{
int set = a & (1 << bit);
if ( set != 0)
{
for ( int i = 0; i < n; i++)
arr[i] |= set ;
}
else
{
if ((b & (1 << bit)) != 0)
{
arr[0] |= (1 << bit);
if ((c & (1 << bit)) == 0)
{
arr[1] |= (1 << bit);
}
}
}
}
int aa = int .MaxValue, bb = 0, cc = 0;
for ( int i = 0; i < n; i++)
{
aa &= arr[i];
bb |= arr[i];
cc ^= arr[i];
}
if (a == aa && b == bb && c == cc)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
else
Console.WriteLine( "-1" );
}
public static void Main(String[] args)
{
int n = 3, a = 4, b = 6, c = 6;
findArray(n, a, b, c);
}
}
|
Javascript
<script>
function findArray(n, a, b, c) {
let arr = new Array(n + 1);
for (let bit = 30; bit >= 0; bit--) {
let set = a & (1 << bit);
if (set) {
for (let i = 0; i < n; i++)
arr[i] |= set;
}
else {
if (b & (1 << bit)) {
arr[0] |= (1 << bit);
if (!(c & (1 << bit))) {
arr[1] |= (1 << bit);
}
}
}
}
let aa = Number.MAX_SAFE_INTEGER, bb = 0, cc = 0;
for (let i = 0; i < n; i++) {
aa &= arr[i];
bb |= arr[i];
cc ^= arr[i];
}
if (a == aa && b == bb && c == cc) {
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
else
document.write( "-1" );
}
let n = 3, a = 4, b = 6, c = 6;
findArray(n, a, b, c);
</script>
|
Time Complexity: O(31*N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...