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' 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;
}
Java
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));
}
}
Python3
# 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))
C#
//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));
}
}
JavaScript
//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));
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).
Similar Reads
Check if any Array pair has bitwise XOR greater than bitwise AND Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] â arr[j] > arr[i] & arr[j], (0 ⤠i < j ⤠N-1) where â represents the Bitwise XOR operator and & represents bitwise AND oper
9 min read
Modify a binary array to Bitwise AND of all elements as 1 Given an array, a[] consists of only 0 and 1. The task is to check if it is possible to transform the array such that the AND value between every pair of indices is 1. The only operation allowed is to: Take two indices i and j and replace the a[i] and a[j] with a[i] | a[j] where '|' means bitwise OR
4 min read
Generate original Array from the bitwise AND and Bitwise OR of adjacent elements Given an integer N denoting the size of an array and two arrays containing Bitwise AND and Bitwise OR of adjacent elements of the array and the first element of the array X, the task is to build the original array. Examples: Input: N = 2, X(First element) = 2Bitwise OR = {3}, Bitwise AND = {2}Output
4 min read
Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array Given an array arr[] consisting of N integers, the task is to find the sum of Bitwise AND of (arr[i] + arr[j]) and Bitwise AND of arr[i] and arr[j] for each pair of elements (arr[i], arr[j]) from the given array. Since the sum can be very large, print it modulo (109 + 7). Examples: Input: arr[] = {8
9 min read
Bitwise OR of Bitwise AND of all subarrays of an array Given an array arr[] consisting of N positive integers, the task is to find the Bitwise OR of Bitwise AND of all subarrays of the given arrays. Examples: Input: arr[] = {1, 2, 3}Output: 3Explanation:The following are Bitwise AND of all possible subarrays are: {1}, Bitwise AND is 1.{1, 2}, Bitwise AN
7 min read