Check if an Array exists with Bitwise OR as A and Bitwise AND as B
Last Updated :
09 Jan, 2024
Improve
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 :
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
string isPossible(int n, int a, int b)
{
// If 'N' is equal to '1':
if (n == 1) {
// If 'X' is equal to 'Y', then return '1'.
// Otherwise return '0'.
if (a == b) {
return "YES";
}
return "NO";
}
// Initialize an integer variable 'cnt' with '1'.
int cnt = 1;
// For 'i' in the range '0' to log2(maximum of X and
// Y)':
for (int i = 0; i <= log2(max(a, b)) + 1; i++) {
// If the 'i-th' bit of 'X' is '0' and the 'i-th'
// bit of 'Y' is '1', then return '0'.
if (((a >> i) & 1) == 0 && ((b >> i) & 1) == 1) {
return "NO";
}
// If the 'i-th' bit of 'X' is '1' and the 'i-th'
// bit of 'Y' is '0', then multiply 'cnt' by '2'.
if (((a >> i) & 1) == 1 && ((b >> i) & 1) == 0) {
cnt *= 2;
}
}
// If 'cnt' is less than 'N', then return '0'.
if (cnt < n) {
return "NO";
}
// Return '1'.
return "YES";
}
int main()
{
cout << isPossible(9, 11, 51);
return 0;
}
import java.io.*;
class GFG {
static int isPossible(int n, int a, int b)
{
// If 'N' is equal to '1':
if (n == 1) {
// If 'X' is equal to 'Y', then return '1'.
// Otherwise, return '0'.
return (a == b) ? 1 : 0;
}
// Initialize an integer variable 'cnt' with '1'.
int cnt = 1;
// For 'i' in the range '0' to log2(maximum of X and
// Y)':
for (int i = 0; i <= log2(Math.max(a, b)) + 1;
i++) {
// If the 'i-th' bit of 'X' is '0' and the
// 'i-th' bit of 'Y' is '1', then return '0'.
if (((a >> i) & 1) == 0
&& ((b >> i) & 1) == 1) {
return 0;
}
// If the 'i-th' bit of 'X' is '1' and the
// 'i-th' bit of 'Y' is '0', then multiply 'cnt'
// by '2'.
if (((a >> i) & 1) == 1
&& ((b >> i) & 1) == 0) {
cnt *= 2;
}
}
// If 'cnt' is less than 'N', then return '0'.
if (cnt < n) {
return 0;
}
// Return '1'.
return 1;
}
// Helper method to calculate the logarithm base 2
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));
}
}
# code by flutterfly
import math
def is_possible(n, a, b):
# If 'N' is equal to '1':
if n == 1:
# If 'X' is equal to 'Y', then return '1'.
# Otherwise, return '0'.
return 1 if a == b else 0
# Initialize an integer variable 'cnt' with '1'.
cnt = 1
# For 'i' in the range '0' to log2(maximum of X and Y)':
for i in range(0, log2(max(a, b)) + 1):
# If the 'i-th' bit of 'X' is '0' and the
# 'i-th' bit of 'Y' is '1', then return '0'.
if ((a >> i) & 1) == 0 and ((b >> i) & 1) == 1:
return 0
# If the 'i-th' bit of 'X' is '1' and the
# 'i-th' bit of 'Y' is '0', then multiply 'cnt'
# by '2'.
if ((a >> i) & 1) == 1 and ((b >> i) & 1) == 0:
cnt *= 2
# If 'cnt' is less than 'N', then return '0'.
if cnt < n:
return 0
# Return '1'.
return 1
# Helper method to calculate the logarithm base 2
def log2(num):
return int(math.log(num) / math.log(2))
# Main function
print(is_possible(9, 11, 51))
//code by flutterfly
using System;
class GFG
{
static int IsPossible(int n, int a, int b)
{
// If 'N' is equal to '1':
if (n == 1)
{
// If 'X' is equal to 'Y', then return '1'.
// Otherwise, return '0'.
return (a == b) ? 1 : 0;
}
// Initialize an integer variable 'cnt' with '1'.
int cnt = 1;
// For 'i' in the range '0' to log2(maximum of X and Y)':
for (int i = 0; i <= Log2(Math.Max(a, b)) + 1; i++)
{
// If the 'i-th' bit of 'X' is '0' and the
// 'i-th' bit of 'Y' is '1', then return '0'.
if (((a >> i) & 1) == 0 && ((b >> i) & 1) == 1)
{
return 0;
}
// If the 'i-th' bit of 'X' is '1' and the
// 'i-th' bit of 'Y' is '0', then multiply 'cnt'
// by '2'.
if (((a >> i) & 1) == 1 && ((b >> i) & 1) == 0)
{
cnt *= 2;
}
}
// If 'cnt' is less than 'N', then return '0'.
if (cnt < n)
{
return 0;
}
// Return '1'.
return 1;
}
// Helper method to calculate the logarithm base 2
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));
}
}
//code by flutterfly
function isPossible(n, a, b) {
// If 'N' is equal to '1':
if (n === 1) {
// If 'X' is equal to 'Y', then return '1'.
// Otherwise, return '0'.
return a === b ? 1 : 0;
}
// Initialize an integer variable 'cnt' with '1'.
let cnt = 1;
// For 'i' in the range '0' to log2(maximum of X and Y)':
for (let i = 0; i <= log2(Math.max(a, b)) + 1; i++) {
// If the 'i-th' bit of 'X' is '0' and the
// 'i-th' bit of 'Y' is '1', then return '0'.
if (((a >> i) & 1) === 0 && ((b >> i) & 1) === 1) {
return 0;
}
// If the 'i-th' bit of 'X' is '1' and the
// 'i-th' bit of 'Y' is '0', then multiply 'cnt'
// by '2'.
if (((a >> i) & 1) === 1 && ((b >> i) & 1) === 0) {
cnt *= 2;
}
}
// If 'cnt' is less than 'N', then return '0'.
if (cnt < n) {
return 0;
}
// Return '1'.
return 1;
}
// Helper method to calculate the logarithm base 2
function log2(num) {
return Math.floor(Math.log(num) / Math.log(2));
}
// Main function
console.log(isPossible(9, 11, 51));
Output
0
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).