📜  给定AND值的最长子序列|上)

📅  最后修改于: 2021-05-25 01:21:58             🧑  作者: Mango

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

例子:

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

高效方法:一项主要观察结果是,所需子序列中的所有数字与M进行“与”运算时,都应产生值M。所以,过滤掉所有那些并与M等于M使得元素。
现在,任务是在此过滤后的子集中找到最长的子序列。很明显,所有这些数字都将与在一起。如果此AND的结果为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 AND of all the
    // filtered elements
    int c_and = filter[0];
    for (int i = 1; i < filter.size(); i++)
        c_and &= filter[i];
  
    // Check if the AND is equal to m
    if (c_and == 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 AND of all the
    // filtered elements
    int c_and = filter.get(0);
    for (int i = 1; i < filter.size(); i++)
        c_and &= filter.get(i);
  
    // Check if the AND is equal to m
    if (c_and == 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.println(findLen(arr, n, m));
}
}
  
// This code is contributed by PrinciRaj1992


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_and = filter[0]; 
    for i in range(1, len(filter)) :
        c_and &= filter[i]; 
  
    # Check if the OR is equal to m 
    if (c_and == 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 AND of all the
    // filtered elements
    int c_and = filter[0];
    for (int i = 1; i < filter.Count; i++)
        c_and &= filter[i];
  
    // Check if the AND is equal to m
    if (c_and == m)
        return filter.Count;
  
    return 0;
}
  
// Driver code
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));
}
}
  
// This code is contributed by 29AjayKumar


输出:
4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。