Count the number of valid contiguous Subarrays
Last Updated :
27 Jun, 2023
Given an array, arr[] consisting of N integers and integer K, the task is to count the number of contiguous subarrays where each element is in the subarray at least K times.
Examples:
Input: N = 3, arr[] = {0, 0, 0}, K = 2
Output: 3
Explanation: [0], [0], [0] – 0
[0, 0], [0, 0] – 2
[0, 0, 0] – 1
total = 2 + 1 = 3
Input: N = 5, arr = [1, 2, 1, 2, 3], K = 2
Output: 1
Explanation: [1] [2] [1] [2] [3] – 0
[1, 2] [2, 1] [1, 2] [2, 3] – 0
[1, 2, 1] [2, 1, 2] [1, 2, 3] – 0
[1, 2, 1, 2] [2, 1, 2, 3] – 1
[1, 2, 1, 2, 3] – 0
total = 1
Approach: This can be solved by the following idea:
The simplest approach is to iterate through all the subarrays and find the frequency of each subarray and check if the subarray is valid or not and keep track of all the subarrays by using a variable count.
Steps involved in the implementation of code:
- Maintain a Map while iterating over the arrays.
- Check for each subarray whether the frequency of each element is less than k or not.
- If it is greater than k, increment the count.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool valid(unordered_map< int , int >& map, int k)
{
for ( auto key : map)
{
if (key.second < k)
return false ;
}
return true ;
}
int countSubarrays( int a[], int n, int k)
{
if (n == 0)
return 0;
int count = 0;
for ( int i = 0; i < n; i++)
{
unordered_map< int , int > map;
for ( int j = i; j < n; j++)
{
map[a[j]]++;
if (valid(map, k))
count++;
}
}
return count;
}
int main()
{
int N = 5;
int a[] = { 1, 2, 1, 2, 3 };
int K = 2;
cout << countSubarrays(a, N, K) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int countSubarrays( int a[], int n, int k)
{
if (n == 0 )
return 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
HashMap<Integer, Integer> map = new HashMap<>();
for ( int j = i; j < n; j++) {
map.put(a[j],
map.getOrDefault(a[j], 0 ) + 1 );
if (valid(map, k))
count++;
}
}
return count;
}
public static boolean
valid(HashMap<Integer, Integer> map, int k)
{
for ( int key : map.keySet()) {
if (map.get(key) < k)
return false ;
}
return true ;
}
public static void main(String[] args)
{
int N = 5 ;
int a[] = { 1 , 2 , 1 , 2 , 3 };
int K = 2 ;
System.out.println(countSubarrays(a, N, K));
}
}
|
Python3
def valid( map , k):
for key in map :
if map [key] < k:
return False
return True
def countSubarrays(a, n, k):
if n = = 0 :
return 0
count = 0
for i in range (n):
map = {}
for j in range (i, n):
if a[j] in map :
map [a[j]] + = 1
else :
map [a[j]] = 1
if valid( map , k):
count + = 1
return count
N = 5
a = [ 1 , 2 , 1 , 2 , 3 ]
K = 2
print (countSubarrays(a, N, K))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int countSubarrays( int [] a, int n, int k)
{
if (n == 0)
return 0;
int count = 0;
for ( int i = 0; i < n; i++) {
Dictionary< int , int > map
= new Dictionary< int , int >();
;
for ( int j = i; j < n; j++) {
if (map.ContainsKey(a[j]) == false )
map.Add(a[j], 1);
else
map[a[j]] += 1;
if (valid(map, k))
count++;
}
}
return count;
}
public static bool valid(Dictionary< int , int > map,
int k)
{
foreach (KeyValuePair< int , int > ele in map)
{
if (ele.Value < k)
return false ;
}
return true ;
}
static public void Main()
{
int N = 5;
int [] a = { 1, 2, 1, 2, 3 };
int K = 2;
Console.WriteLine(countSubarrays(a, N, K));
}
}
|
Javascript
function valid(map, k) {
for (let key of map.keys()) {
if (map.get(key) < k) {
return false ;
}
}
return true ;
}
function countSubarrays(a, n, k) {
if (n == 0) {
return 0;
}
let count = 0;
for (let i = 0; i < n; i++) {
let map = new Map();
for (let j = i; j < n; j++) {
map.set(a[j], (map.get(a[j]) || 0) + 1);
if (valid(map, k)) {
count++;
}
}
}
return count;
}
let N = 5;
let a = [1, 2, 1, 2, 3];
let K = 2;
console.log(countSubarrays(a, N, K));
|
Time Complexity: O(N3)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...