📜  给定OR值的最长子序列:O(N)方法

📅  最后修改于: 2021-04-27 09:11:13             🧑  作者: Mango

给定数组arr [] ,任务是找到具有给定OR值M的最长子序列。如果没有这样的子序列,则打印0

例子:

天真的方法:解决此问题的一种简单方法是生成所有可能的子序列,然后在其中找到具有所需OR值的最大子序列。

高效的方法:一项关键的观察结果是,与M进行或运算后,所需子序列中的所有数字都应产生值M。因此,滤除所有与M等于M的元素。
现在,任务是在此过滤后的子集中找到最长的子序列。很明显,所有这些数字都将进行“或”运算。如果此OR的结果为M,则答案将等于此过滤后的集合的大小。否则答案将为0 。这是因为“或”仅设置未设置的位。因此,集合中的数字越大,它越优化。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the required length
int findLen(int* arr, int n, int m)
{
    // To store the filtered numbers
    vector 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;
}


Java
// 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 filter = new Vector();
  
    // 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


Python3
# 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


C#
// 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 filter = new List();
  
    // 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


输出:
4