Check if an Array exists with Bitwise OR as A and Bitwise AND as B
Last Updated :
09 Jan, 2024
Three integers N, A and B are given to you. you need to create an array arr of length ‘N‘ such that it satisfies the following conditions :
- Each elements of arr should be unique.
- arr[1] | arr[2] | arr[3] |….arr[N] = A
- arr[1] & arr[2] & arr[3] &….arr[N] = B
Where ‘|’ Represents Bitwise OR and ‘&’ Represents Bitwise AND. Your task is to determine if it is possible to Create Such an array or not.
Example :
Input: ‘N’ = 3 , ‘A’ = 10, ‘B’ = 2
Output: YES
Explanation: one of the possible array arr is: arr = [2,3,8] .
Input: ‘N’ = 5 ‘A’ = 15 ‘B’ = 16
Output: NO.
Approach:
- ‘A‘ is the bitwise OR of all array elements, and ‘B‘ is the bitwise AND of all array elements.
- If the ‘i-th‘ bit of ‘B‘ is ‘1‘, then every element in array arr has ‘i-th‘ bit ‘1‘ in their binary representation.
- If the ‘i-th’ bit of ‘A’ is ‘1’, then some elements may have ‘i-th‘ bit ‘1‘ in their binary representation.
- Now, if the ‘i-th‘ bit of ‘A‘ is ‘0‘, but the ‘i-th‘ bit of ‘B‘ is ‘1‘, then it’s a contradiction, and it is not possible to create such array ‘arr’, then the answer will be ‘NO‘.
- If the ‘i-th‘ bit of ‘A‘ is ‘1‘, but the ‘i-th‘ bit of ‘B‘ is ‘0‘, we have a choice to choose a number, such that it may have the ‘i-th‘ bit ‘0‘ or ‘1‘. So, we have ‘2‘ choices for a number if the ‘i-th‘ bit of ‘A‘ is ‘1‘, but the ‘i-th’ bit of ‘B‘ is ‘0‘.
- So, let’s consider the count of such positions ‘ i ‘, with the ‘i-th’ bit of ‘A’ as ‘1’, but the ‘i-th’ bit of ‘B’ as ‘0’, as ‘cnt‘. Then there are total ‘2^cnt‘ possible different numbers.
- If ‘2^cnt‘ is less than ‘N‘, the answer will be ‘NO‘, Otherwise, the answer will be ‘YES‘.
Below is the Implementation of Above approach :
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
string isPossible( int n, int a, int b)
{
if (n == 1) {
if (a == b) {
return "YES";
}
return "NO";
}
int cnt = 1;
for ( int i = 0; i <= log2(max(a, b)) + 1; i++) {
if (((a >> i) & 1) == 0 && ((b >> i) & 1) == 1) {
return "NO";
}
if (((a >> i) & 1) == 1 && ((b >> i) & 1) == 0) {
cnt *= 2;
}
}
if (cnt < n) {
return "NO";
}
return "YES";
}
int main()
{
cout << isPossible(9, 11, 51);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int isPossible( int n, int a, int b)
{
if (n == 1 ) {
return (a == b) ? 1 : 0 ;
}
int cnt = 1 ;
for ( int i = 0 ; i <= log2(Math.max(a, b)) + 1 ;
i++) {
if (((a >> i) & 1 ) == 0
&& ((b >> i) & 1 ) == 1 ) {
return 0 ;
}
if (((a >> i) & 1 ) == 1
&& ((b >> i) & 1 ) == 0 ) {
cnt *= 2 ;
}
}
if (cnt < n) {
return 0 ;
}
return 1 ;
}
static int log2( int num)
{
return ( int )(Math.log(num) / Math.log( 2 ));
}
public static void main(String[] args)
{
System.out.println(isPossible( 9 , 11 , 51 ));
}
}
|
Python3
import math
def is_possible(n, a, b):
if n = = 1 :
return 1 if a = = b else 0
cnt = 1
for i in range ( 0 , log2( max (a, b)) + 1 ):
if ((a >> i) & 1 ) = = 0 and ((b >> i) & 1 ) = = 1 :
return 0
if ((a >> i) & 1 ) = = 1 and ((b >> i) & 1 ) = = 0 :
cnt * = 2
if cnt < n:
return 0
return 1
def log2(num):
return int (math.log(num) / math.log( 2 ))
print (is_possible( 9 , 11 , 51 ))
|
C#
using System;
class GFG
{
static int IsPossible( int n, int a, int b)
{
if (n == 1)
{
return (a == b) ? 1 : 0;
}
int cnt = 1;
for ( int i = 0; i <= Log2(Math.Max(a, b)) + 1; i++)
{
if (((a >> i) & 1) == 0 && ((b >> i) & 1) == 1)
{
return 0;
}
if (((a >> i) & 1) == 1 && ((b >> i) & 1) == 0)
{
cnt *= 2;
}
}
if (cnt < n)
{
return 0;
}
return 1;
}
static int Log2( int num)
{
return ( int )(Math.Log(num) / Math.Log(2));
}
public static void Main( string [] args)
{
Console.WriteLine(IsPossible(9, 11, 51));
}
}
|
Javascript
function isPossible(n, a, b) {
if (n === 1) {
return a === b ? 1 : 0;
}
let cnt = 1;
for (let i = 0; i <= log2(Math.max(a, b)) + 1; i++) {
if (((a >> i) & 1) === 0 && ((b >> i) & 1) === 1) {
return 0;
}
if (((a >> i) & 1) === 1 && ((b >> i) & 1) === 0) {
cnt *= 2;
}
}
if (cnt < n) {
return 0;
}
return 1;
}
function log2(num) {
return Math.floor(Math.log(num) / Math.log(2));
}
console.log(isPossible(9, 11, 51));
|
Time Complexity : O(log2(z)) – where ‘z’ is the maximum of ‘A’ and ‘B’. (because we are traversing till the ‘log2(z)th bit .)
Auxiliary Space Complexity : O(1).
Share your thoughts in the comments
Please Login to comment...