Check if there exists a subsequence such that its cumulative AND is equal to X
Last Updated :
11 Nov, 2023
Given an array A[] of length N along with an integer X. Then the task is to output the subsequence such that the cumulative AND of all elements that are present in the subsequence is equal to X. If such a subsequence is present, then print it else print -1.
Examples:
Input: N = 3, A[] = {67, 44, 23}, X = 7
Output: -1
Explanation: It can be verified that no such subsequence exists such that the bitwise AND of that subsequence is equal to 7. Therefore, output is -1.
Input: N = 5, A[] = {2, 8, 5, 4, 3}, X = 1
Output: {5, 3}
Explanation: The bitwise AND of 5 and 3 is 1.
Approach: To solve the problem follow the below idea:
The idea for the solving the problem is: Bitwise AND equal to X can be made only and only by those elements in which set bits are same as which are present in X. So, the simple intuition comes that, iterate over the given array A[] and collect all the elements of A[] into a list such that Bitwise AND of element with X is equal to X. After that find cumulative Bitwise AND of all elements present in list, If it is equal to X then output elements present in list else -1.
Steps were taken to solve the problem:
- Iterating over array
- If element & X == X (all bits which are set in X are also set in element(A[i])) Then add it to the list
- If there is no element in the list Output -1
- Else check that, the cumulative AND of All elements present in the list is equal to X
- If yes then output list elements else -1
Below is the code to implement the approach:
C++
#include <iostream>
#include <vector>
using namespace std;
void AND_Subsequence( int N, vector< int >& A, int X) {
vector< int > list;
for ( int element : A) {
if ((element & X) == X) {
list.push_back(element);
}
}
if (list.empty()) {
cout << -1 << endl;
} else {
int AND = list[0];
for ( int i = 1; i < list.size(); i++) {
AND = AND & list[i];
}
if (AND == X) {
for ( int element : list) {
cout << element << " " ;
}
cout << endl;
} else {
cout << -1 << endl;
}
}
}
int main() {
int N = 5;
vector< int > A = {2, 8, 5, 4, 3};
int X = 1;
AND_Subsequence(N, A, X);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
int N = 5 ;
int A[] = { 2 , 8 , 5 , 4 , 3 };
int X = 1 ;
AND_Subsequence(N, A, X);
}
public static void AND_Subsequence( int N, int A[],
int X)
{
ArrayList<Integer> list = new ArrayList<>();
for ( int element : A) {
if ((element & X) == X) {
list.add(element);
}
}
if (list.size() == 0 ) {
System.out.println(- 1 );
}
else {
int AND = list.get( 0 );
for ( int i = 1 ; i < list.size(); i++) {
AND = AND & list.get( 1 );
}
System.out.println(AND == X ? list : - 1 );
}
}
}
|
Python3
def AND_Subsequence(N, A, X):
lst = []
for element in A:
if (element & X) = = X:
lst.append(element)
if not lst:
print ( - 1 )
else :
AND = lst[ 0 ]
for i in range ( 1 , len (lst)):
AND = AND & lst[i]
if AND = = X:
for element in lst:
print (element, end = " " )
print ()
else :
print ( - 1 )
N = 5
A = [ 2 , 8 , 5 , 4 , 3 ]
X = 1
AND_Subsequence(N, A, X)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void AND_Subsequence( int N, List< int > A, int X)
{
List< int > list = new List< int >();
foreach ( int element in A)
{
if ((element & X) == X)
{
list.Add(element);
}
}
if (list.Count == 0)
{
Console.WriteLine(-1);
}
else
{
int AND = list[0];
for ( int i = 1; i < list.Count; i++)
{
AND = AND & list[i];
}
if (AND == X)
{
foreach ( int element in list)
{
Console.Write(element + " " );
}
Console.WriteLine();
}
else
{
Console.WriteLine(-1);
}
}
}
static void Main( string [] args)
{
int N = 5;
List< int > A = new List< int > { 2, 8, 5, 4, 3 };
int X = 1;
AND_Subsequence(N, A, X);
}
}
|
Javascript
function AND_Subsequence(N, A, X) {
const list = [];
for (const element of A) {
if ((element & X) === X) {
list.push(element);
}
}
if (list.length === 0) {
console.log(-1);
} else {
let AND = list[0];
for (let i = 1; i < list.length; i++) {
AND = AND & list[i];
}
if (AND === X) {
console.log(list.join( " " ));
} else {
console.log(-1);
}
}
}
const N = 5;
const A = [2, 8, 5, 4, 3];
const X = 1;
AND_Subsequence(N, A, X);
|
Time Complexity: O(N)
Auxiliary Space: O(N), an ArrayList is created to store relevant elements, The maximum size of ArrayList can be equal to A[].
Share your thoughts in the comments
Please Login to comment...