Longest sub-sequence with a given OR value : O(N) Approach
Last Updated :
09 Nov, 2021
Given an array arr[], the task is to find the longest subsequence with a given OR value M. If there is no such sub-sequence then print 0.
Examples:
Input: arr[] = {3, 7, 2, 3}, M = 3
Output: 3
{3, 2, 3} is the required subsequence
3 | 2 | 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 OR 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 ORed with M. So filter out all of such elements whose OR 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 ORed together. If the result of this OR is M then the answer will be equal to the size of this filtered set. Otherwise answer will be 0. This is because OR only sets the unset 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_or = filter[0];
for ( int i = 1; i < filter.size(); i++)
c_or |= filter[i];
if (c_or == 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<Integer>();
for ( int i = 0 ; i < n; i++)
if ((arr[i] | m) == m)
filter.add(arr[i]);
if (filter.size() == 0 )
return 0 ;
int c_or = filter.get( 0 );
for ( int i = 1 ; i < filter.size(); i++)
c_or |= filter.get(i);
if (c_or == 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.print(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_or = filter [ 0 ];
for i in range ( 1 , len ( filter )) :
c_or | = filter [i];
if (c_or = = 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_or = filter[0];
for ( int i = 1; i < filter.Count; i++)
c_or |= filter[i];
if (c_or == m)
return filter.Count;
return 0;
}
public static void Main()
{
int []arr = { 7, 3, 3, 1, 3 };
int n = arr.Length;
int m = 3;
Console.Write(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_or = filter[0];
for ( var i = 1; i < filter.length; i++)
c_or |= filter[i];
if (c_or == 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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...