Longest sub-sequence with a given OR value : O(N) Approach

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the required length
int findLen(int* arr, int n, int m)
{
    // To store the filtered numbers
    vector<int> filter;
  
    // Filtering the numbers
    for (int i = 0; i < n; i++)
        if ((arr[i] | m) == m)
            filter.push_back(arr[i]);
  
    // If there are no elements to check
    if (filter.size() == 0)
        return 0;
  
    // Find the OR of all the
    // filtered elements
    int c_or = filter[0];
    for (int i = 1; i < filter.size(); i++)
        c_or |= filter[i];
  
    // Check if the OR is equal to m
    if (c_or == m)
        return filter.size();
  
    return 0;
}
  
// Driver code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to return the required length
static int findLen(int arr[], int n, int m)
{
    // To store the filtered numbers
    Vector<Integer> filter = new Vector<Integer>();
  
    // Filtering the numbers
    for (int i = 0; i < n; i++)
        if ((arr[i] | m) == m)
            filter.add(arr[i]);
  
    // If there are no elements to check
    if (filter.size() == 0)
        return 0;
  
    // Find the OR of all the
    // filtered elements
    int c_or = filter.get(0);
    for (int i = 1; i < filter.size(); i++)
        c_or |= filter.get(i);
  
    // Check if the OR is equal to m
    if (c_or == m)
        return filter.size();
  
    return 0;
}
  
// Driver code
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));
}
}
  
// This code is contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the required length 
def findLen(arr, n, m) : 
  
    # To store the filtered numbers 
    filter = []; 
  
    # Filtering the numbers 
    for i in range(n) : 
        if ((arr[i] | m) == m) :
            filter.append(arr[i]); 
  
    # If there are no elements to check 
    if (len(filter) == 0) :
        return 0
  
    # Find the OR of all the 
    # filtered elements 
    c_or = filter[0]; 
    for i in range(1, len(filter)) :
        c_or |= filter[i]; 
  
    # Check if the OR is equal to m 
    if (c_or == m) :
        return len(filter); 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 7, 3, 3, 1, 3 ]; 
    n = len(arr); 
    m = 3
  
    print(findLen(arr, n, m)); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
using System.Collections.Generic; 
  
class GFG
{
  
// Function to return the required length
static int findLen(int [] arr, int n, int m)
{
    // To store the filtered numbers
    List<int> filter = new List<int>();
  
    // Filtering the numbers
    for (int i = 0; i < n; i++)
        if ((arr[i] | m) == m)
            filter.Add(arr[i]);
  
    // If there are no elements to check
    if (filter.Count == 0)
        return 0;
  
    // Find the OR of all the
    // filtered elements
    int c_or = filter[0];
    for (int i = 1; i < filter.Count; i++)
        c_or |= filter[i];
  
    // Check if the OR is equal to m
    if (c_or == m)
        return filter.Count;
  
    return 0;
}
  
// Driver code
public static void Main()
{
    int []arr = { 7, 3, 3, 1, 3 };
    int n = arr.Length;
    int m = 3;
  
    Console.Write(findLen(arr, n, m));
}
}
  
// This code is contributed by Mohit kumar 29

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.