📜  长度为 K 的子序列的最大位与值

📅  最后修改于: 2022-05-13 01:57:50.946000             🧑  作者: Mango

长度为 K 的子序列的最大位与值

给定一个大小为N的数组a和一个整数K 。任务是找到长度为K的任何子序列的元素的最大位和值。
注意:a[i] <= 10 9
例子:

朴素的方法:一种朴素的方法是递归地找到长度为K的所有子序列的按位和值,并且所有子序列中的最大值将是答案。
有效方法:一种有效的方法是使用位属性来解决它。以下是解决问题的步骤:

  • 从左边迭代(最初left = 312 32 > 10 9 )直到我们在第 i 位设置的向量 temp (最初temp = arr )中找到> K个数字。将新的一组数字更新为临时数组
  • 如果我们没有得到> K个数字,则临时数组中任何K个元素的&值将是可能的最大值&值。
  • 重复步骤 1 ,将 left 重新初始化为first-bit + 1

下面是上述方法的实现:

C++
// C++ program to find the sum of
// the addition of all possible subsets.
#include 
using namespace std;
 
// Function to perform step-1
vector findSubset(vector& temp, int& last, int k)
{
    vector ans;
 
    // Iterate from left till 0
    // till we get a bit set of K numbers
    for (int i = last; i >= 0; i--) {
        int cnt = 0;
 
        // Count the numbers whose
        // i-th bit is set
        for (auto it : temp) {
            int bit = it & (1 << i);
            if (bit > 0)
                cnt++;
        }
 
        // If the array has numbers>=k
        // whose i-th bit is set
        if (cnt >= k) {
            for (auto it : temp) {
                int bit = it & (1 << i);
                if (bit > 0)
                    ans.push_back(it);
            }
 
            // Update last
            last = i - 1;
 
            // Return the new set of numbers
            return ans;
        }
    }
 
    return ans;
}
 
// Function to find the maximum '&' value
// of K elements in subsequence
int findMaxiumAnd(int a[], int n, int k)
{
    int last = 31;
    // Temporary arrays
    vector temp1, temp2;
 
    // Initially temp = arr
    for (int i = 0; i < n; i++) {
        temp2.push_back(a[i]);
    }
 
    // Iterate till we have >=K elements
    while ((int)temp2.size() >= k) {
 
        // Temp array
        temp1 = temp2;
 
        // Find new temp array if
        // K elements are there
        temp2 = findSubset(temp2, last, k);
    }
 
    // Find the & value
    int ans = temp1[0];
    for (int i = 0; i < k; i++)
        ans = ans & temp1[i];
 
    return ans;
}
 
// Driver Code
int main()
{
    int a[] = { 255, 127, 31, 5, 24, 37, 15 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 4;
 
    cout << findMaxiumAnd(a, n, k);
}


Java
// Java program to find the sum of
// the addition of all possible subsets.
import java.util.*;
class GFG
{
static int last;
 
// Function to perform step-1
static Vector
       findSubset(Vector temp, int k)
{
    Vector ans = new Vector();
 
    // Iterate from left till 0
    // till we get a bit set of K numbers
    for (int i = last; i >= 0; i--)
    {
        int cnt = 0;
 
        // Count the numbers whose
        // i-th bit is set
        for (Integer it : temp)
        {
            int bit = it & (1 << i);
            if (bit > 0)
                cnt++;
        }
 
        // If the array has numbers>=k
        // whose i-th bit is set
        if (cnt >= k)
        {
            for (Integer it : temp)
            {
                int bit = it & (1 << i);
                if (bit > 0)
                    ans.add(it);
            }
 
            // Update last
            last = i - 1;
 
            // Return the new set of numbers
            return ans;
        }
    }
    return ans;
}
 
// Function to find the maximum '&' value
// of K elements in subsequence
static int findMaxiumAnd(int a[], int n, int k)
{
    last = 31;
     
    // Temporary arrays
    Vector temp1 = new Vector();
    Vector temp2 = new Vector();;
 
    // Initially temp = arr
    for (int i = 0; i < n; i++)
    {
        temp2.add(a[i]);
    }
 
    // Iterate till we have >=K elements
    while ((int)temp2.size() >= k)
    {
 
        // Temp array
        temp1 = temp2;
 
        // Find new temp array if
        // K elements are there
        temp2 = findSubset(temp2, k);
    }
 
    // Find the & value
    int ans = temp1.get(0);
    for (int i = 0; i < k; i++)
        ans = ans & temp1.get(i);
 
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = { 255, 127, 31, 5, 24, 37, 15 };
    int n = a.length;
    int k = 4;
 
    System.out.println(findMaxiumAnd(a, n, k));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the sum of
# the addition of all possible subsets.
last = 31
 
# Function to perform step-1
def findSubset(temp, k):
    global last
    ans = []
 
    # Iterate from left till 0
    # till we get a bit set of K numbers
    for i in range(last, -1, -1):
        cnt = 0
 
        # Count the numbers whose
        # i-th bit is set
        for it in temp:
            bit = it & (1 << i)
            if (bit > 0):
                cnt += 1
 
        # If the array has numbers>=k
        # whose i-th bit is set
        if (cnt >= k):
            for it in temp:
                bit = it & (1 << i)
                if (bit > 0):
                    ans.append(it)
 
            # Update last
            last = i - 1
 
            # Return the new set of numbers
            return ans
 
    return ans
 
# Function to find the maximum '&' value
# of K elements in subsequence
def findMaxiumAnd(a, n, k):
    global last
 
    # Temporary arrays
    temp1, temp2, = [], []
 
    # Initially temp = arr
    for i in range(n):
        temp2.append(a[i])
 
    # Iterate till we have >=K elements
    while len(temp2) >= k:
 
        # Temp array
        temp1 = temp2
 
        # Find new temp array if
        # K elements are there
        temp2 = findSubset(temp2, k)
 
    # Find the & value
    ans = temp1[0]
    for i in range(k):
        ans = ans & temp1[i]
 
    return ans
 
# Driver Code
a = [255, 127, 31, 5, 24, 37, 15]
n = len(a)
k = 4
 
print(findMaxiumAnd(a, n, k))
 
# This code is contributed by Mohit Kumar


C#
// C# program to find the sum of
// the addition of all possible subsets.
using System;
using System.Collections.Generic;
 
class GFG
{
static int last;
 
// Function to perform step-1
static ListfindSubset(List temp, int k)
{
    List ans = new List();
 
    // Iterate from left till 0
    // till we get a bit set of K numbers
    for (int i = last; i >= 0; i--)
    {
        int cnt = 0;
 
        // Count the numbers whose
        // i-th bit is set
        foreach (int it in temp)
        {
            int bit = it & (1 << i);
            if (bit > 0)
                cnt++;
        }
 
        // If the array has numbers>=k
        // whose i-th bit is set
        if (cnt >= k)
        {
            foreach (int it in temp)
            {
                int bit = it & (1 << i);
                if (bit > 0)
                    ans.Add(it);
            }
 
            // Update last
            last = i - 1;
 
            // Return the new set of numbers
            return ans;
        }
    }
    return ans;
}
 
// Function to find the maximum '&' value
// of K elements in subsequence
static int findMaxiumAnd(int []a, int n, int k)
{
    last = 31;
     
    // Temporary arrays
    List temp1 = new List();
    List temp2 = new List();;
 
    // Initially temp = arr
    for (int i = 0; i < n; i++)
    {
        temp2.Add(a[i]);
    }
 
    // Iterate till we have >=K elements
    while ((int)temp2.Count >= k)
    {
 
        // Temp array
        temp1 = temp2;
 
        // Find new temp array if
        // K elements are there
        temp2 = findSubset(temp2, k);
    }
 
    // Find the & value
    int ans = temp1[0];
    for (int i = 0; i < k; i++)
        ans = ans & temp1[i];
 
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []a = { 255, 127, 31, 5, 24, 37, 15 };
    int n = a.Length;
    int k = 4;
 
    Console.WriteLine(findMaxiumAnd(a, n, k));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
24