Count of subarrays having exactly K distinct elements
Given an array arr[] of size N and an integer K. The task is to find the count of subarrays such that each subarray has exactly K distinct elements.
Examples:
Input: arr[] = {2, 1, 2, 1, 6}, K = 2
Output: 7
{2, 1}, {1, 2}, {2, 1}, {1, 6}, {2, 1, 2},
{1, 2, 1} and {2, 1, 2, 1} are the only valid subarrays.
Input: arr[] = {1, 2, 3, 4, 5}, K = 1
Output: 5
Approach: To directly count the subarrays with exactly K different integers is hard but to find the count of subarrays with at most K different integers is easy. So the idea is to find the count of subarrays with at most K different integers, let it be C(K), and the count of subarrays with at most (K – 1) different integers, let it be C(K – 1) and finally take their difference, C(K) – C(K – 1) which is the required answer.
Count of subarrays with at most K different elements can be easily calculated through the sliding window technique. The idea is to keep expanding the right boundary of the window till the count of distinct elements in the window is less than or equal to K and when the count of distinct elements inside the window becomes more than K, start shrinking the window from the left till the count becomes less than or equal to K. Also for every expansion, keep counting the subarrays as right – left + 1 where right and left are the boundaries of the current window.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
#include<map>
using namespace std;
int atMostK( int arr[], int n, int k)
{
int count = 0;
int left = 0;
int right = 0;
unordered_map< int , int > map;
while (right < n) {
if (map.find(arr[right])==map.end())
map[arr[right]]=0;
map[arr[right]]++;
while (map.size() > k) {
map[arr[left]]= map[arr[left]] - 1;
if (map[arr[left]] == 0)
map.erase(arr[left]);
left++;
}
count += right - left + 1;
right++;
}
return count;
}
int exactlyK( int arr[], int n, int k)
{
return (atMostK(arr, n, k) - atMostK(arr, n, k - 1));
}
int main()
{
int arr[] = { 2, 1, 2, 1, 6 };
int n = sizeof (arr)/ sizeof (arr[0]);
int k = 2;
cout<<(exactlyK(arr, n, k));
}
|
Java
import java.util.*;
public class GfG {
private static int atMostK( int arr[], int n, int k)
{
int count = 0 ;
int left = 0 ;
int right = 0 ;
HashMap<Integer, Integer> map = new HashMap<>();
while (right < n) {
map.put(arr[right],
map.getOrDefault(arr[right], 0 ) + 1 );
while (map.size() > k) {
map.put(arr[left], map.get(arr[left]) - 1 );
if (map.get(arr[left]) == 0 )
map.remove(arr[left]);
left++;
}
count += right - left + 1 ;
right++;
}
return count;
}
private static int exactlyK( int arr[], int n, int k)
{
return (atMostK(arr, n, k)
- atMostK(arr, n, k - 1 ));
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 2 , 1 , 6 };
int n = arr.length;
int k = 2 ;
System.out.print(exactlyK(arr, n, k));
}
}
|
Python3
def atMostK(arr, n, k):
count = 0
left = 0
right = 0
map = {}
while (right < n):
if arr[right] not in map :
map [arr[right]] = 0
map [arr[right]] + = 1
while ( len ( map ) > k):
if arr[left] not in map :
map [arr[left]] = 0
map [arr[left]] - = 1
if map [arr[left]] = = 0 :
del map [arr[left]]
left + = 1
count + = right - left + 1
right + = 1
return count
def exactlyK(arr, n, k):
return (atMostK(arr, n, k) -
atMostK(arr, n, k - 1 ))
if __name__ = = "__main__" :
arr = [ 2 , 1 , 2 , 1 , 6 ]
n = len (arr)
k = 2
print (exactlyK(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GfG {
private static int atMostK( int [] arr, int n, int k)
{
int count = 0;
int left = 0;
int right = 0;
Dictionary< int , int > map
= new Dictionary< int , int >();
while (right < n) {
if (map.ContainsKey(arr[right]))
map[arr[right]] = map[arr[right]] + 1;
else
map.Add(arr[right], 1);
while (map.Count > k) {
if (map.ContainsKey(arr[left])) {
map[arr[left]] = map[arr[left]] - 1;
if (map[arr[left]] == 0)
map.Remove(arr[left]);
}
left++;
}
count += right - left + 1;
right++;
}
return count;
}
private static int exactlyK( int [] arr, int n, int k)
{
return (atMostK(arr, n, k)
- atMostK(arr, n, k - 1));
}
public static void Main(String[] args)
{
int [] arr = { 2, 1, 2, 1, 6 };
int n = arr.Length;
int k = 2;
Console.Write(exactlyK(arr, n, k));
}
}
|
Javascript
<script>
function atMostK(arr, n, k)
{
let count = 0;
let left = 0;
let right = 0;
let map = new Map();
while (right < n)
{
if (map.has(arr[right]))
map.set(arr[right],
map.get(arr[right]) + 1);
else
map.set(arr[right], 1);
while (map.size > k)
{
map.set(arr[left], map.get(arr[left]) - 1);
if (map.get(arr[left]) == 0)
map. delete (arr[left]);
left++;
}
count += right - left + 1;
right++;
}
return count;
}
function exactlyK(arr, n, k)
{
return (atMostK(arr, n, k) -
atMostK(arr, n, k - 1));
}
let arr = [ 2, 1, 2, 1, 6 ];
let n = arr.length;
let k = 2;
document.write(exactlyK(arr, n, k));
</script>
|
Time Complexity: O(N)
Space Complexity: O(N)
Another Approach: When you move the right cursor, keep tracking whether we have reach a count of K distinct integers, if yes, we process left cursor, here is how we process left cursor:
- check whether the element pointed by the left cursor is duplicated in the window, if yes, we remove it, and use a variable (e.g. prefix) to record that we have removed an element from the window). keep this process until we reduce the window size from to exactly K. now we can calculate the number of the valid good array as res += prefix;
- after process left cursor and all the stuff, the outer loop will continue and the right cursor will move forward, and then the window size will exceed K, we can simply drop the leftmost element of the window and reset prefix to 0. and continue on.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <map>
#include <vector>
using namespace std;
int subarraysWithKDistinct(vector< int >& A, int K)
{
unordered_map< int , int > mapp;
int begin = 0, end = 0, prefix = 0, cnt = 0;
int res = 0;
while (end < A.size())
{
mapp[A[end]]++;
if (mapp[A[end]] == 1) {
cnt++;
}
end++;
if (cnt > K)
{
mapp[A[begin]]--;
begin++;
cnt--;
prefix = 0;
}
while (mapp[A[begin]] > 1)
{
mapp[A[begin]]--;
begin++;
prefix++;
}
if (cnt == K)
{
res += prefix + 1;
}
}
return res;
}
int main()
{
vector< int > arr{ 2, 1, 2, 1, 6 };
int k = 2;
cout << (subarraysWithKDistinct(arr, k));
}
|
Java
import java.util.*;
class GFG
{
static int subarraysWithKDistinct( int A[], int K)
{
HashMap<Integer, Integer> mapp = new HashMap<>();
int begin = 0 , end = 0 , prefix = 0 , cnt = 0 ;
int res = 0 ;
while (end < A.length)
{
if (mapp.containsKey(A[end]))
{
mapp.put(A[end], mapp.get(A[end]) + 1 );
}
else
{
mapp.put(A[end], 1 );
}
if (mapp.get(A[end]) == 1 )
{
cnt++;
}
end++;
if (cnt > K)
{
if (mapp.containsKey(A[begin]))
{
mapp.put(A[begin], mapp.get(A[begin]) - 1 );
}
else
{
mapp.put(A[begin], - 1 );
}
begin++;
cnt--;
prefix = 0 ;
}
while (mapp.get(A[begin]) > 1 )
{
if (mapp.containsKey(A[begin]))
{
mapp.put(A[begin], mapp.get(A[begin]) - 1 );
}
else
{
mapp.put(A[begin], - 1 );
}
begin++;
prefix++;
}
if (cnt == K)
{
res += prefix + 1 ;
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 2 , 1 , 6 };
int k = 2 ;
System.out.println(subarraysWithKDistinct(arr, k));
}
}
|
Python3
def subarraysWithKDistinct(A, K):
mapp = {}
begin, end, prefix, cnt = 0 , 0 , 0 , 0
res = 0
while (end < len (A)):
mapp[A[end]] = mapp.get(A[end], 0 ) + 1
if (mapp[A[end]] = = 1 ):
cnt + = 1
end + = 1
if (cnt > K):
mapp[A[begin]] - = 1
begin + = 1
cnt - = 1
prefix = 0
while (mapp[A[begin]] > 1 ):
mapp[A[begin]] - = 1
begin + = 1
prefix + = 1
if (cnt = = K):
res + = prefix + 1
return res
if __name__ = = '__main__' :
arr = [ 2 , 1 , 2 , 1 , 6 ]
k = 2
print (subarraysWithKDistinct(arr, k))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int subarraysWithKDistinct(List< int > A, int K)
{
Dictionary< int , int > mapp = new Dictionary< int , int >();
int begin = 0, end = 0, prefix = 0, cnt = 0;
int res = 0;
while (end < A.Count)
{
if (mapp.ContainsKey(A[end]))
{
mapp[A[end]]++;
}
else {
mapp[A[end]] = 1;
}
if (mapp[A[end]] == 1) {
cnt++;
}
end++;
if (cnt > K)
{
if (mapp.ContainsKey(A[begin]))
{
mapp[A[begin]]--;
}
else {
mapp[A[begin]] = -1;
}
begin++;
cnt--;
prefix = 0;
}
while (mapp[A[begin]] > 1)
{
mapp[A[begin]]--;
begin++;
prefix++;
}
if (cnt == K)
{
res += prefix + 1;
}
}
return res;
}
static void Main()
{
List< int > arr = new List< int >( new int [] { 2, 1, 2, 1, 6 });
int k = 2;
Console.Write(subarraysWithKDistinct(arr, k));
}
}
|
Javascript
<script>
function subarraysWithKDistinct(A, K)
{
let mapp = new Map();
let begin = 0, end = 0, prefix = 0, cnt = 0;
let res = 0;
while (end < A.length)
{
if (mapp.has(A[end]))
{
mapp.set(A[end],
mapp.get(A[end]) + 1);
}
else
{
mapp.set(A[end], 1);
}
if (mapp.get(A[end]) == 1)
{
cnt++;
}
end++;
if (cnt > K)
{
if (mapp.has(A[begin]))
{
mapp.set(A[begin],
mapp.get(A[begin]) - 1);
}
else
{
mapp.set(A[begin], -1);
}
begin++;
cnt--;
prefix = 0;
}
while (mapp.get(A[begin]) > 1)
{
if (mapp.has(A[begin]))
{
mapp.set(A[begin],
mapp.get(A[begin]) - 1);
}
else
{
mapp.set(A[begin], -1);
}
begin++;
prefix++;
}
if (cnt == K)
{
res += prefix + 1;
}
}
return res;
}
let arr = [ 2, 1, 2, 1, 6 ];
let k = 2;
document.write(subarraysWithKDistinct(arr, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Optimized Sliding window :
Approach :
Iterate the array and perform these steps :
1. Just count the number of distinct numbers with each iteration.
2. If equals k then check the subarrays which can be formed with the elements between i and j.
3. If greater than k, then move i pointer with decresing number of elements in array and count too.
Then return the sum of subarrays.
Java
import java.io.*;
class GFG {
public static int subarraysWithKDistinct( int [] nums, int k) {
int n = nums.length;
int i = 0 ;
int j = 0 ;
int count = 0 ;
int ans = 0 ;
int []arr = new int [n + 5 ];
while (j < n){
arr[nums[j]]++;
if (arr[nums[j]] == 1 ){
count++;
}
while (count > k){
arr[nums[i]]--;
if (arr[nums[i]] == 0 ){
count--;
}
i++;
}
if (count == k){
int val = i;
while (count == k){
ans++;
int num = nums[val];
arr[num]--;
if (arr[num] == 0 ){
count--;
}
val++;
}
val--;
while (val >= i){
int num = nums[val];
arr[num]++;
if (arr[num] == 1 ){
count++;
}
val--;
}
}
j++;
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 2 , 1 , 2 , 1 , 6 };
int k = 2 ;
System.out.println(subarraysWithKDistinct(arr, k));
}
}
|
Python3
class GFG:
def subarraysWithKDistinct(nums, k):
n = len (nums)
i = 0
j = 0
count = 0
ans = 0
arr = [ 0 ] * (n + 5 )
while (j < n):
arr[nums[j]] + = 1
if (arr[nums[j]] = = 1 ):
count + = 1
while (count > k):
arr[nums[i]] - = 1
if (arr[nums[i]] = = 0 ):
count - = 1
i + = 1
if (count = = k):
val = i
while (count = = k):
ans + = 1
num = nums[val]
arr[num] - = 1
if (arr[num] = = 0 ):
count - = 1
val + = 1
val - = 1
while (val > = i):
num = nums[val]
arr[num] + = 1
if (arr[num] = = 1 ):
count + = 1
val - = 1
j + = 1
return ans
arr = [ 2 , 1 , 2 , 1 , 6 ]
k = 2
print (GFG.subarraysWithKDistinct(arr, k))
|
C++
#include <bits/stdc++.h>
using namespace std;
int subarraysWithKDistinct( int nums[], int n, int k) {
int i = 0, j = 0, count = 0, ans = 0;
int arr[n + 5] = {0};
while (j < n) {
arr[nums[j]]++;
if (arr[nums[j]] == 1)
count++;
while (count > k) {
arr[nums[i]]--;
if (arr[nums[i]] == 0)
count--;
i++;
}
if (count == k) {
int val = i;
while (count == k) {
ans++;
int num = nums[val];
arr[num]--;
if (arr[num] == 0)
count--;
val++;
}
val--;
while (val >= i) {
int num = nums[val];
arr[num]++;
if (arr[num] == 1)
count++;
val--;
}
}
j++;
}
return ans;
}
int main() {
int arr[] = {2, 1, 2, 1, 6};
int n = sizeof (arr) / sizeof (arr[0]);
int k = 2;
cout << subarraysWithKDistinct(arr, n, k) << endl;
return 0;
}
|
C#
using System;
public class GFG {
public static int SubarraysWithKDistinct( int [] nums, int k) {
int n = nums.Length;
int i = 0;
int j = 0;
int count = 0;
int ans = 0;
int [] arr = new int [n + 5];
while (j < n) {
arr[nums[j]]++;
if (arr[nums[j]] == 1) {
count++;
}
while (count > k) {
arr[nums[i]]--;
if (arr[nums[i]] == 0) {
count--;
}
i++;
}
if (count == k) {
int val = i;
while (count == k) {
ans++;
int num = nums[val];
arr[num]--;
if (arr[num] == 0) {
count--;
}
val++;
}
val--;
while (val >= i) {
int num = nums[val];
arr[num]++;
if (arr[num] == 1) {
count++;
}
val--;
}
}
j++;
}
return ans;
}
public static void Main( string [] args) {
int [] arr = { 2, 1, 2, 1, 6 };
int k = 2;
Console.WriteLine(SubarraysWithKDistinct(arr, k));
}
}
|
Javascript
function subarraysWithKDistinct(nums, k) {
const n = nums.length;
let i = 0;
let j = 0;
let count = 0;
let ans = 0;
const arr = new Array(n + 5).fill(0);
while (j < n) {
arr[nums[j]]++;
if (arr[nums[j]] == 1) {
count++;
}
while (count > k) {
arr[nums[i]]--;
if (arr[nums[i]] == 0) {
count--;
}
i++;
}
if (count == k) {
let val = i;
while (count == k) {
ans++;
const num = nums[val];
arr[num]--;
if (arr[num] == 0) {
count--;
}
val++;
}
val--;
while (val >= i) {
const num = nums[val];
arr[num]++;
if (arr[num] == 1) {
count++;
}
val--;
}
}
j++;
}
return ans;
}
const arr = [2, 1, 2, 1, 6];
const k = 2;
console.log(subarraysWithKDistinct(arr, k));
|
Time Complexity : O(N)
Auxiliary Space : O(N)
Last Updated :
04 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...