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++
// C++ Implementation
#include<bits/stdc++.h>
using namespace std;
// Function to check whether sub-array
// is valid or not
bool valid(unordered_map<int, int>& map, int k)
{
// Iterate through all the keys in
// the map check if the frequency
// more than k or not
for (auto key : map)
{
// Iterate through all the keys in
// the map check if the frequency
// more than 2 or not
if (key.second < k)
return false;
}
return true;
}
// Function to count sub-arrays
int countSubarrays(int a[], int n, int k)
{
if (n == 0)
return 0;
int count = 0;
// Iterate through all subarrays
for (int i = 0; i < n; i++)
{
unordered_map<int, int> map;
for (int j = i; j < n; j++)
{
// Check if given subarray
// from i to j is valid
map[a[j]]++;
if (valid(map, k))
count++;
}
}
// Return the count of subarrays
return count;
}
// Driver Code
int main()
{
int N = 5;
int a[] = { 1, 2, 1, 2, 3 };
int K = 2;
// Function call
cout << countSubarrays(a, N, K) << endl;
return 0;
}
// This code is contributed by Prasad Kandekar(prasad264)
Java
// Java Implementation
import java.io.*;
import java.util.*;
class GFG {
// Function to count sub-arrays
public static int countSubarrays(int a[], int n, int k)
{
if (n == 0)
return 0;
int count = 0;
// Iterate through all subarrays
for (int i = 0; i < n; i++) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int j = i; j < n; j++) {
// Check if given subarray
// from i tp j is valid
map.put(a[j],
map.getOrDefault(a[j], 0) + 1);
if (valid(map, k))
count++;
}
}
// Return the count of subarrays
return count;
}
// Function to check whether sub-array
// is valid or not
public static boolean
valid(HashMap<Integer, Integer> map, int k)
{
// Iterate through all the keys in
// the map check if the frequency
// more than 2 or not
for (int key : map.keySet()) {
if (map.get(key) < k)
return false;
}
return true;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int a[] = { 1, 2, 1, 2, 3 };
int K = 2;
// Function call
System.out.println(countSubarrays(a, N, K));
}
}
Python3
# Python3 implementation
def valid(map, k):
# Iterate through all the keys in
# the map check if the frequency
# more than k or not
for key in map:
# Iterate through all the keys in
# the map check if the frequency
# more than k or not
if map[key] < k:
return False
return True
# Function to count sub-arrays
def countSubarrays(a, n, k):
if n == 0:
return 0
count = 0
# Iterate through all subarrays
for i in range(n):
map = {}
for j in range(i, n):
# Check if given subarray
# from i to j is valid
if a[j] in map:
map[a[j]] += 1
else:
map[a[j]] = 1
if valid(map, k):
count += 1
# Return the count of subarrays
return count
# Driver Code
N = 5
a = [1, 2, 1, 2, 3]
K = 2
# Function call
print(countSubarrays(a, N, K))
C#
// C# Implementation
using System;
using System.Collections.Generic;
public class GFG {
// Function to count sub-arrays
public static int countSubarrays(int[] a, int n, int k)
{
if (n == 0)
return 0;
int count = 0;
// Iterate through all subarrays
for (int i = 0; i < n; i++) {
Dictionary<int, int> map
= new Dictionary<int, int>();
;
for (int j = i; j < n; j++) {
// Check if given subarray
// from i tp j is valid
if (map.ContainsKey(a[j]) == false)
map.Add(a[j], 1);
else
map[a[j]] += 1;
if (valid(map, k))
count++;
}
}
// Return the count of subarrays
return count;
}
// Function to check whether sub-array
// is valid or not
public static bool valid(Dictionary<int, int> map,
int k)
{
// Iterate through all the keys in
// the map check if the frequency
// more than 2 or not
foreach(KeyValuePair<int, int> ele in map)
{
if (ele.Value < k)
return false;
}
return true;
}
// Driver Code
static public void Main()
{
int N = 5;
int[] a = { 1, 2, 1, 2, 3 };
int K = 2;
// Function call
Console.WriteLine(countSubarrays(a, N, K));
}
}
// This code is contributed by Rohit Pradhan
JavaScript
// JavaScript Implementation
// Function to check whether sub-array
// is valid or not
function valid(map, k) {
// Iterate through all the keys in
// the map check if the frequency
// more than k or not
for (let key of map.keys()) {
// Iterate through all the keys in
// the map check if the frequency
// more than 2 or not
if (map.get(key) < k) {
return false;
}
}
return true;
}
// Function to count sub-arrays
function countSubarrays(a, n, k) {
if (n == 0) {
return 0;
}
let count = 0;
// Iterate through all subarrays
for (let i = 0; i < n; i++) {
let map = new Map();
for (let j = i; j < n; j++) {
// Check if given subarray
// from i to j is valid
map.set(a[j], (map.get(a[j]) || 0) + 1);
if (valid(map, k)) {
count++;
}
}
}
// Return the count of subarrays
return count;
}
// Driver Code
let N = 5;
let a = [1, 2, 1, 2, 3];
let K = 2;
// Function call
console.log(countSubarrays(a, N, K));
// This code is contributed by prasad264
Time Complexity: O(N3)
Auxiliary Space: O(N)
Similar Reads
Count the number of non-increasing subarrays Given an array of N integers. The task is to count the number of subarrays (of size at least one) that are non-increasing. Examples: Input : arr[] = {1, 4, 3} Output : 4 The possible subarrays are {1}, {4}, {3}, {4, 3}. Input :{4, 3, 2, 1} Output : 10 The possible subarrays are: {4}, {3}, {2}, {1},
6 min read
Find the total number of Subarrays of 0's Given an array arr[] of length N of 0's and 1's, the task is to find the total number of subarrays of 0's. Examples: Input: N = 4, arr[] = {0, 0, 1, 0}Output: 4Explanation: Following are the subarrays of length 1: {0}, {0}, {0} - 3 length 2: {0, 0} - 1. Total Subarrays: 3 + 1 = 4 Input: N = 4, arr[]
9 min read
Count of subarrays which contains a given number exactly K times Given an array A[] of N elements consisting of values from 1 to N with duplicates, the task is to find the total number of subarrays that contain a given number num exactly K times. Examples: Input: A[] = {1, 2, 1, 5, 1}, num = 1, K = 2 Output: 2 Explanation: Subarrays {1, 2, 1, 5}, {1, 2, 1}, {2, 1
8 min read
Count subarrays with equal number of 1's and 0's Given an array arr[] of size n containing 0 and 1 only. The problem is to count the subarrays having an equal number of 0's and 1's. Examples: Input: arr[] = {1, 0, 0, 1, 0, 1, 1}Output: 8Explanation: The index range for the 8 sub-arrays are: (0, 1), (2, 3), (0, 3), (3, 4), (4, 5)(2, 5), (0, 5), (1,
14 min read
Count of subarrays with maximum value as K Given an array arr[] of N integers and an integer K. The task is to find the number of subarrays with a maximum value is equal to K. Examples: Input: arr[ ] = {2, 1, 3, 4}, K = 3Output: 3Explanation: Sub-arrays with maximum value is equals K are { 2, 1, 3 }, { 1, 3 }, { 3 }, hence the answer is 3. I
8 min read
Sum of Count of Unique Numbers in all Subarrays Given an array of n integers, the task is to count the sum of unique numbers in all subarrays. Examples: Input: [2, 1, 2]Output: 9Explanation: There are total 6 subarrays which are [2], [2, 1], [2, 1, 2], [1], [1, 2], [2]. The count of unique numbers in these subarrays is 1, 2, 2, 1, 2, 1 respective
15+ min read