Count of Subsets containing only the given value K
Last Updated :
21 Dec, 2022
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))/2 subsets 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++
#include <iostream>
using namespace std;
int count( int arr[], int N, int K)
{
int count = 0, ans = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == K) {
count = count + 1;
}
else {
ans += (count * (count + 1)) / 2;
count = 0;
}
}
ans = ans + (count * (count + 1)) / 2;
return ans;
}
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
class GFG{
static int count( int arr[], int N, int K)
{
int count = 0 , ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == K) {
count = count + 1 ;
}
else {
ans += (count * (count + 1 )) / 2 ;
count = 0 ;
}
}
ans = ans + (count * (count + 1 )) / 2 ;
return ans;
}
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));
}
}
|
Python3
def count(arr, N, K):
count = 0
ans = 0
for i in range (N):
if (arr[i] = = K):
count = count + 1
else :
ans + = (count * (count + 1 )) / / 2
count = 0
ans = ans + (count * (count + 1 )) / / 2
return ans
if __name__ = = '__main__' :
arr = [ 1 , 0 , 0 , 1 , 1 , 0 , 0 ]
N = len (arr)
K = 0
print (count(arr, N, K))
|
C#
using System;
class GFG{
static int count( int []arr, int N, int K)
{
int count = 0, ans = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == K)
{
count = count + 1;
}
else
{
ans += (count * (count + 1)) / 2;
count = 0;
}
}
ans = ans + (count * (count + 1)) / 2;
return ans;
}
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));
}
}
|
Javascript
<script>
function count(arr, N, K)
{
let count = 0, ans = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] == K) {
count = count + 1;
}
else {
ans += (count * (count + 1)) / 2;
count = 0;
}
}
ans = ans + (count * (count + 1)) / 2;
return ans;
}
let arr = [ 1, 0, 0, 1, 1, 0, 0 ];
let N = arr.length;
let K = 0;
document.write(count(arr, N, K));
</script>
|
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1), As constant extra space is used.
Share your thoughts in the comments
Please Login to comment...