📌  相关文章
📜  计算由恰好具有 K 个设置位的元素组成的子数组

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

给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是计算可能由恰好具有K 个设置位的元素组成的子数组的数量。

例子:

朴素的方法:解决问题的最简单的方法是生成给定数组的所有可能的子数组,并对由恰好具有K 个设置位的元素组成的子数组进行计数。最后,打印此类子数组的计数。

时间复杂度: O(N 3 log(M)),其中M数组中最大的元素
辅助空间: O(1)

有效的方法:这个想法是跟踪具有K 个集合位的连续数组元素,并找到具有这些连续元素集的子数组的计数。请按照以下步骤解决问题:

  • 初始化一个变量,比如res0 ,以存储由具有K 个设置位的元素组成的子数组的总数。初始化一个变量,比如count0 ,以存储具有K 个设置位的一组连续元素的计数。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 如果当前元素arr[i]K 个设置位,则将count增加1
    • 否则,将res的值增加(count*(count – 1)) / 2作为由前一个连续元素形成的子数组的总数,并将count设置为0
  • 完成上述步骤后,将res的值打印为子数组的结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number
// of set bits in an integer N
int countSet(int N)
{
 
  // Stores the count of set bits
  int ans = 0;
 
  // While N is non-zero
  while(N)
  {
 
    // If the LSB is 1, then
    // increment ans by 1
    ans += N & 1;
    N >>= 1;
  }
 
  // Return the total set bits
  return ans;
}
 
// Function to count the number of
// subarrays having made up of
// elements having K set bits
int countSub(int *arr,int k)
{
 
  // Stores the total count of
  // resultant subarrays
  int ans = 0;
  int setK = 0;
 
  // Traverse the given array
  for(int i = 0; i < 5; i++)
  {
 
    // If the current element
    // has K set bits
    if(countSet(arr[i]) == k)
      setK += 1;
 
    // Otherwise
    else
      setK = 0;
 
    // Increment count of subarrays
    ans += setK;
  }
 
  // Return total count of subarrays
  return ans;
}
 
// Driver Code
int main()
{
  int arr[] = {4, 2, 1, 5, 6};
  int K = 2;
 
  // Function Call
  cout<<(countSub(arr, K));
  return 0;
}
 
// This code is contributed by rohitsingh07052.


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the number
// of set bits in an integer N
static int countSet(int N)
{
     
    // Stores the count of set bits
    int ans = 0;
     
    // While N is non-zero
    while(N > 0)
    {
         
        // If the LSB is 1, then
        // increment ans by 1
        ans += N & 1;
        N >>= 1;
    }
     
    // Return the total set bits
    return ans;
}
 
// Function to count the number of
// subarrays having made up of
// elements having K set bits
static int countSub(int []arr,int k)
{
     
    // Stores the total count of
    // resultant subarrays
    int ans = 0;
    int setK = 0;
     
    // Traverse the given array
    for(int i = 0; i < 5; i++)
    {
     
        // If the current element
        // has K set bits
        if (countSet(arr[i]) == k)
            setK += 1;
         
        // Otherwise
        else
            setK = 0;
         
        // Increment count of subarrays
        ans += setK;
    }
     
    // Return total count of subarrays
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = {4, 2, 1, 5, 6};
    int K = 2;
     
    // Function Call
    System.out.print(countSub(arr, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to count the number
# of set bits in an integer N
def countSet(N):
 
    # Stores the count of set bits
    ans = 0
 
    # While N is non-zero
    while N:
 
        # If the LSB is 1, then
        # increment ans by 1
        ans += N & 1
        N >>= 1
 
    # Return the total set bits
    return ans
 
# Function to count the number of
# subarrays having made up of
# elements having K set bits
def countSub(arr, k):
 
    # Stores the total count of
    # resultant subarrays
    ans = 0
    setK = 0
 
    # Traverse the given array
    for i in arr:
 
        # If the current element
        # has K set bits
        if countSet(i) == k:
            setK += 1
 
        # Otherwise
        else:
            setK = 0
 
        # Increment count of subarrays
        ans += setK
 
    # Return total count of subarrays
    return ans
 
 
# Driver Code
arr = [4, 2, 1, 5, 6]
K = 2
 
# Function Call
print(countSub(arr, K))


C#
// C# program for the above approach
using System;
 
class GFG {
 
  // Function to count the number
  // of set bits in an integer N
  static int countSet(int N)
  {
 
    // Stores the count of set bits
    int ans = 0;
 
    // While N is non-zero
    while (N > 0)
    {
 
      // If the LSB is 1, then
      // increment ans by 1
      ans += N & 1;
      N >>= 1;
    }
 
    // Return the total set bits
    return ans;
  }
 
  // Function to count the number of
  // subarrays having made up of
  // elements having K set bits
  static int countSub(int[] arr, int k)
  {
 
    // Stores the total count of
    // resultant subarrays
    int ans = 0;
    int setK = 0;
 
    // Traverse the given array
    for (int i = 0; i < 5; i++) {
 
      // If the current element
      // has K set bits
      if (countSet(arr[i]) == k)
        setK += 1;
 
      // Otherwise
      else
        setK = 0;
 
      // Increment count of subarrays
      ans += setK;
    }
 
    // Return total count of subarrays
    return ans;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] arr = { 4, 2, 1, 5, 6 };
    int K = 2;
 
    // Function Call
    Console.WriteLine(countSub(arr, K));
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:
3

时间复杂度: O(N*log(M)),其中 M 是数组中最大的元素
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live