📜  仅包含给定值K的子集数

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

给定一个数组arr []和一个至少在数组中存在一次的数字K ,任务是查找数组中子集的数量,以使每个子集仅包含给定值K。
例子:

方法:为了找到子集的数量,需要对为给定集合中的不同数量的元素形成的子集的数量进行观察。
因此,令N为我们需要为其找到子集的元素的数量。
然后,如果:

N = 1: Only one subset can be formed.
N = 2: Three subsets can be formed.
N = 3: Six subsets can be formed.
N = 4: Ten subsets can be formed.
.
.
.
N = K: (K * (K + 1))/2 subsets can be formed.

由于我们正在计算由值K的连续出现形成的子集的数量,因此我们的想法是找到给定数组中存在的连续K的计数,并使用给定的公式找到计数。
下面是上述方法的实现:

C++
// C++ implementation to find the
// number of subsets formed by
// the given value K
#include 
using namespace std;
 
// Function to find the number
// of subsets formed by the
// given value K
int count(int arr[], int N, int K)
{
    // Count is used to maintain the
    // number of continuous K's
    int count = 0, ans = 0;
 
    // Iterating through the array
    for (int i = 0; i < N; i++) {
 
        // If the element in the array
        // is equal to K
        if (arr[i] == K) {
            count = count + 1;
        }
        else {
 
            // count*(count+1)/2 is the
            // total number of subsets
            // with only K as their element
            ans += (count * (count + 1)) / 2;
 
            // Change count to 0 because
            // other element apart from
            // K has been found
            count = 0;
        }
    }
 
    // To handle the last set of K's
    ans = ans + (count * (count + 1)) / 2;
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 0, 0, 1, 1, 0, 0 };
    int N = sizeof(arr) / sizeof(int);
    int K = 0;
 
    cout << count(arr, N, K);
}


Java
// Java implementation to find the
// number of subsets formed by
// the given value K
class GFG{
  
// Function to find the number
// of subsets formed by the
// given value K
static int count(int arr[], int N, int K)
{
    // Count is used to maintain the
    // number of continuous K's
    int count = 0, ans = 0;
  
    // Iterating through the array
    for (int i = 0; i < N; i++) {
  
        // If the element in the array
        // is equal to K
        if (arr[i] == K) {
            count = count + 1;
        }
        else {
  
            // count*(count+1)/2 is the
            // total number of subsets
            // with only K as their element
            ans += (count * (count + 1)) / 2;
  
            // Change count to 0 because
            // other element apart from
            // K has been found
            count = 0;
        }
    }
  
    // To handle the last set of K's
    ans = ans + (count * (count + 1)) / 2;
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 0, 0, 1, 1, 0, 0 };
    int N = arr.length;
    int K = 0;
  
    System.out.print(count(arr, N, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 implementation to find the
# number of subsets formed by
# the given value K
 
# Function to find the number
# of subsets formed by the
# given value K
def count(arr, N, K):
    # Count is used to maintain the
    # number of continuous K's
    count = 0
    ans = 0
 
    # Iterating through the array
    for i in range(N):
        # If the element in the array
        # is equal to K
        if (arr[i] == K):
            count = count + 1
    
        else:
            # count*(count+1)/2 is the
            # total number of subsets
            # with only K as their element
            ans += (count * (count + 1)) // 2
 
            # Change count to 0 because
            # other element apart from
            # K has been found
            count = 0
 
    # To handle the last set of K's
    ans = ans + (count * (count + 1)) // 2
    return ans
 
# Driver code
if __name__ == '__main__':
    arr =  [1, 0, 0, 1, 1, 0, 0]
    N = len(arr)
    K = 0
 
    print(count(arr, N, K))
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to find the
// number of subsets formed by
// the given value K
using System;
 
class GFG{
 
// Function to find the number
// of subsets formed by the
// given value K
static int count(int []arr, int N, int K)
{
    // Count is used to maintain the
    // number of continuous K's
    int count = 0, ans = 0;
 
    // Iterating through the array
    for(int i = 0; i < N; i++)
    {
 
       // If the element in the array
       // is equal to K
       if (arr[i] == K)
       {
           count = count + 1;
            
       }
       else
       {
           // count*(count+1)/2 is the
           // total number of subsets
           // with only K as their element
           ans += (count * (count + 1)) / 2;
            
           // Change count to 0 because
           // other element apart from
           // K has been found
           count = 0;
            
       }
    }
 
    // To handle the last set of K's
    ans = ans + (count * (count + 1)) / 2;
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 0, 0, 1, 1, 0, 0 };
    int N = arr.Length;
    int K = 0;
 
    Console.Write(count(arr, N, K));
}
}
 
//This is contributed by shivanisinghss2110


Javascript


输出:
6

时间复杂度: O(N) ,其中N是数组的大小。