Count the number of valid contiguous Subarrays
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 = 3Input: 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 notbool 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-arraysint 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 Codeint 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 Implementationimport 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)); }} |
1
Time Complexity: O(N* N * log N)
Auxiliary Space: O(N)






Please Login to comment...