Given an array arr[], the task is to find the longest subsequence with a given AND value M. If there is no such sub-sequence then print 0.
Examples:
Input: arr[] = {3, 7, 2, 3}, M = 3
Output: 3
{3, 7, 3} is the required subsequence.
3 & 7 & 3 = 3
Input: arr[] = {2, 2}, M = 3
Output: 0
Naive approach: A simple way to solve this problem is to generate all the possible sub-sequences and then find the largest among them with the required AND value.
Efficient approach: One key observation is that all of the numbers in the required sub-sequence should yield the value M when they get ANDed with M. So filter out all of such elements whose AND with M equals to M.
Now, the task is to find the longest sub-sequence among this filtered subset. It’s pretty obvious that all of these numbers will be ANDed together. If the result of this AND is M then the answer will be equal to the size of this filtered set. Otherwise answer will be 0. This is because AND only unsets the set bits. So, the larger the numbers in the set, the more optimal it is.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findLen( int * arr, int n, int m)
{
vector< int > filter;
for ( int i = 0; i < n; i++)
if ((arr[i] & m) == m)
filter.push_back(arr[i]);
if (filter.size() == 0)
return 0;
int c_and = filter[0];
for ( int i = 1; i < filter.size(); i++)
c_and &= filter[i];
if (c_and == m)
return filter.size();
return 0;
}
int main()
{
int arr[] = { 7, 3, 3, 1, 3 };
int n = sizeof (arr) / sizeof ( int );
int m = 3;
cout << findLen(arr, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findLen( int []arr, int n, int m)
{
Vector<Integer> filter = new Vector<>();
for ( int i = 0 ; i < n; i++)
if ((arr[i] & m) == m)
filter.add(arr[i]);
if (filter.size() == 0 )
return 0 ;
int c_and = filter.get( 0 );
for ( int i = 1 ; i < filter.size(); i++)
c_and &= filter.get(i);
if (c_and == m)
return filter.size();
return 0 ;
}
public static void main(String []args)
{
int arr[] = { 7 , 3 , 3 , 1 , 3 };
int n = arr.length;
int m = 3 ;
System.out.println(findLen(arr, n, m));
}
}
|
Python3
def findLen(arr, n, m) :
filter = [];
for i in range (n) :
if ((arr[i] & m) = = m) :
filter .append(arr[i]);
if ( len ( filter ) = = 0 ) :
return 0 ;
c_and = filter [ 0 ];
for i in range ( 1 , len ( filter )) :
c_and & = filter [i];
if (c_and = = m) :
return len ( filter );
if __name__ = = "__main__" :
arr = [ 7 , 3 , 3 , 1 , 3 ];
n = len (arr);
m = 3 ;
print (findLen(arr, n, m));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findLen( int []arr, int n, int m)
{
List< int > filter = new List< int >();
for ( int i = 0; i < n; i++)
if ((arr[i] & m) == m)
filter.Add(arr[i]);
if (filter.Count == 0)
return 0;
int c_and = filter[0];
for ( int i = 1; i < filter.Count; i++)
c_and &= filter[i];
if (c_and == m)
return filter.Count;
return 0;
}
public static void Main(String []args)
{
int []arr = { 7, 3, 3, 1, 3 };
int n = arr.Length;
int m = 3;
Console.WriteLine(findLen(arr, n, m));
}
}
|
Javascript
<script>
function findLen(arr, n, m)
{
var filter = [];
for ( var i = 0; i < n; i++)
if ((arr[i] & m) == m)
filter.push(arr[i]);
if (filter.length == 0)
return 0;
var c_and = filter[0];
for ( var i = 1; i < filter.length; i++)
c_and &= filter[i];
if (c_and == m)
return filter.length;
return 0;
}
var arr = [7, 3, 3, 1, 3];
var n = arr.length;
var m = 3;
document.write( findLen(arr, n, m));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)