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

📅  最后修改于: 2021-05-25 04:10:18             🧑  作者: 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)