# Count of Subsets containing only the given value K

Given an array **arr[]** and a number **K** which is present in the array at least once, the task is to find the number of subsets in the array such that each subset contains only the given value **K**.

**Examples:**

Input:arr[] = {1, 0, 0, 1, 0, 1, 2, 5, 2, 1}, K = 0

Output:4

Explanation:

From the two 0’s present in the array at the index 2 and 3, 3 subsequences can be formed: {0}, {0}, {0, 0}

From the 0 present in the array at the index 5, 1 subsequence can be formed: {0}

Therefore, a total of 4 subsequences are formed.

Input:arr[] = {1, 0, 0, 1, 1, 0, 0, 2, 3, 5}, K = 1

Output:4

**Approach:** In order to find the number of subsets, one observation needs to be made on the number of subsets formed for the different number of elements in the given set.

So, let N be the number of elements for which we need to find the subsets.

Then, if:

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))/2subsets can be formed.

Since we are calculating the number of subsets formed by the continuous occurrence of the value K, the idea is to find the count of continuous K’s present in the given array and find the count by using the given formula.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// number of subsets formed by ` `// the given value K ` `#include <iostream> ` `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 continous 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); ` `} ` |

*chevron_right*

*filter_none*

## 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 continous 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 ` |

*chevron_right*

*filter_none*

## 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 continous 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 ` |

*chevron_right*

*filter_none*

## 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 continous 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** *O(N)*, where N is the size of the array.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count number of subsets having a particular XOR value
- Count of subsets having sum of min and max element less than K
- Count no. of ordered subsets having a particular XOR value
- Count of subsets with sum equal to X
- Count of subsets not containing adjacent elements
- Count of Subsets of a given Set with element X present in it
- Count subsets having distinct even numbers
- Count of subsets with sum equal to X using Recursion
- Count of subsets of integers from 1 to N having no adjacent elements
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Count number of subsets whose median is also present in the same subset
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count non-adjacent subsets from numbers arranged in Circular fashion
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Count number of subsets of a set with GCD equal to a given number
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Subsets having Sum between A and B

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.