Count of numbers appearing in the given ranges at-least K times
Given N ranges and a number K, the task is to find total count of numbers that appears at least K times in the given ranges.
Examples:
Input:
N = 3, K = 2
Range 1: [91, 94]
Range 2: [92, 97]
Range 3: [97, 99]
Output : 4
Explanation: Ranges are 91 to 94, 92 to 97, 97 to 99 and the numbers that occured atleast 2 times are 92, 93, 94, 97.
Input :
N = 2, K = 3
Range 1 = [1, 4]
Range 2 = [5, 9]
Output : 0
Explanation: No element occurred 3 times in the given ranges.
Naive Approach: A naive approach would be to traverse each range and increase the count for each element, finally check whether the count of each element suffices the required value.
Below is the implementation of the above approach:
C++
// C++ brute-force program to find count of // numbers appearing in the given // ranges at-least K times #include <bits/stdc++.h> using namespace std; // Function to find the no of occurrence int countNumbers(int n, int k, int rangeLvalue[], int rangeRvalue[]) { int count = 0; // Map to store frequency of elements // in the range map<int, int> freq; for (int i = 0; i < n; i++) { // increment the value of the elements // in all of the ranges for (int j = rangeLvalue[i]; j <= rangeRvalue[i]; j++) { if (freq.find(j) == freq.end()) freq.insert(make_pair(j, 1)); else freq[j]++; } } // Traverse the map to check the frequency // of numbers greater than equals to k for (auto itr = freq.begin(); itr != freq.end(); itr++) { // check if a number appears atleast k times if ((*itr).second >= k) { // increase the counter // if condition satisfies count++; } } // return the result return count; } // Driver Code int main() { int n = 3, k = 2; int rangeLvalue[] = { 91, 92, 97 }; int rangeRvalue[] = { 94, 97, 99 }; cout << countNumbers(n, k, rangeLvalue, rangeRvalue); return 0; } |
Python3
# Python3 brute-force program to find # count of numbers appearing in the # given ranges at-least K times # Function to find the no of occurrence def countNumbers(n, k, rangeLvalue, rangeRvalue): count = 0 # Map to store frequency of elements # in the range freq = dict() for i in range(n): # increment the value of the elements # in all of the ranges for j in range(rangeLvalue[i], rangeRvalue[i] + 1): freq[j] = freq.get(j, 0) + 1 # Traverse the map to check the frequency # of numbers greater than equals to k for itr in freq: # check if a number appears # atleast k times if (freq[itr] >= k): # increase the counter # if condition satisfies count += 1 # return the result return count # Driver Code n, k = 3, 2rangeLvalue = [91, 92, 97] rangeRvalue = [94, 97, 99] print(countNumbers(n, k, rangeLvalue, rangeRvalue)) # This code is contributed by mohit kumar |
4
Efficient Solution: A better solution is to keep track of the ranges by incrementing the value of the leftmost element of the range and decrementing the next element of the rightmost element of the given range in the counter array. Do this for all ranges. This is done as it gives the idea of how many times a number occured in the given range on doing a pre-sum.
Below is the implementation of the above approach.
C++
// C++ efficient program to find count of // numbers appearing in the given // ranges at-least K times #include <bits/stdc++.h> using namespace std; // Function to find the no of occurrence int countNumbers(int n, int k, int rangeLvalue[], int rangeRvalue[]) { int count = 0; // maximum value of the range int maxn = INT_MIN; for (int i = 0; i < n; i++) if (rangeRvalue[i] > maxn) maxn = rangeRvalue[i]; // counter array int preSum[maxn + 5] = { 0 }; // incrementing and decrementing the // leftmost and next value of rightmost value // of each range by 1 respectively for (int i = 0; i < n; i++) { preSum[rangeLvalue[i]]++; preSum[rangeRvalue[i] + 1]--; } // presum gives the no of occurrence of // each element for (int i = 1; i <= maxn; i++) { preSum[i] += preSum[i - 1]; } for (int i = 1; i <= maxn; i++) { // check if the number appears atleast k times if (preSum[i] >= k) { // increase the counter if // condition satisfies count++; } } // return the result return count; } // Driver Code int main() { int n = 3, k = 2; int rangeLvalue[] = { 91, 92, 97 }; int rangeRvalue[] = { 94, 97, 99 }; cout << countNumbers(n, k, rangeLvalue, rangeRvalue); return 0; } |
Java
// Java efficient program to find count of // numbers appearing in the given // ranges at-least K times class Geeks { // Function to find the no of occurrence static int countNumbers(int n, int k, int rangeLvalue[], int rangeRvalue[]) { int count = 0; // maximum value of the range int maxn = Integer.MIN_VALUE; for (int i = 0; i < n; i++) if (rangeRvalue[i] > maxn) maxn = rangeRvalue[i]; // counter array int preSum[] = new int[maxn + 5]; for(int i = 0; i < (maxn + 5); i++) preSum[i] = 0; // incrementing and decrementing the // leftmost and next value of rightmost value // of each range by 1 respectively for (int i = 0; i < n; i++) { preSum[rangeLvalue[i]]++; preSum[rangeRvalue[i] + 1]--; } // presum gives the no of occurrence of // each element for (int i = 1; i <= maxn; i++) { preSum[i] += preSum[i - 1]; } for (int i = 1; i <= maxn; i++) { // check if the number appears atleast k times if (preSum[i] >= k) { // increase the counter if // condition satisfies count++; } } // return the result return count; } // Driver Code public static void main(String args[]) { int n = 3, k = 2; int rangeLvalue[] = { 91, 92, 97 }; int rangeRvalue[] = { 94, 97, 99 }; System.out.println(countNumbers(n, k, rangeLvalue, rangeRvalue)); } } // This code is contributed by ankita_saini |
Python
# Python efficient program to find count of # numbers appearing in the given # ranges at-least K times # Function to find the no of occurrence def countNumbers(n, k, rangeLvalue, rangeRvalue): count = 0 # maximum value of the range maxn = -float('inf') for i in range(n): if rangeRvalue[i] > maxn: maxn = rangeRvalue[i] # counter array preSum = [0]*(maxn + 5) # incrementing and decrementing the # leftmost and next value of rightmost value # of each range by 1 respectively for i in range(n): preSum[rangeLvalue[i]] += 1 preSum[rangeRvalue[i] + 1] -= 1 # presum gives the no of occurrence of # each element for i in range(1, maxn+1): preSum[i] += preSum[i - 1] for i in range(1, maxn+1): # check if the number appears atleast k times if preSum[i] >= k: # increase the counter if # condition satisfies count += 1 # return the result return count # Driver Code n = 3k = 2 rangeLvalue = [91, 92, 97] rangeRvalue = [94, 97, 99] print(countNumbers(n, k, rangeLvalue, rangeRvalue)) # This code is contributed by ankush_953 |
C#
// C# efficient program to // find count of numbers // appearing in the given // ranges at-least K times using System; class GFG { // Function to find the // no of occurrence static int countNumbers(int n, int k, int []rangeLvalue, int []rangeRvalue) { int count = 0; // maximum value of the range int maxn = Int32.MinValue; for (int i = 0; i < n; i++) if (rangeRvalue[i] > maxn) maxn = rangeRvalue[i]; // counter array int []preSum = new int[maxn + 5]; for(int i = 0; i < (maxn + 5); i++) preSum[i] = 0; // incrementing and decrementing // the leftmost and next value // of rightmost value of each // range by 1 respectively for (int i = 0; i < n; i++) { preSum[rangeLvalue[i]]++; preSum[rangeRvalue[i] + 1]--; } // presum gives the no of // occurrence of each element for (int i = 1; i <= maxn; i++) { preSum[i] += preSum[i - 1]; } for (int i = 1; i <= maxn; i++) { // check if the number // appears atleast k times if (preSum[i] >= k) { // increase the counter if // condition satisfies count++; } } // return the result return count; } // Driver Code public static void Main(String []args) { int n = 3, k = 2; int []rangeLvalue = { 91, 92, 97 }; int []rangeRvalue = { 94, 97, 99 }; Console.WriteLine(countNumbers(n, k, rangeLvalue, rangeRvalue)); } } // This code is contributed // by ankita_saini |
4
Time-Complexity: O(N + max(rangeRvalue[]))
Auxillary-Space: O(N)
Recommended Posts:
- Count of Numbers in a Range where digit d occurs exactly K times
- Count substrings with each character occurring at most k times
- Count distinct substrings that contain some characters at most k times
- Count all elements in the array which appears at least K times after their first occurrence
- Count how many times the given digital clock shows identical digits
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Minimum K such that every substring of length atleast K contains a character c
- Find the number of points that have atleast 1 point above, below, left or right of it
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Count Triplets such that one of the numbers can be written as sum of the other two
- Count of numbers below N whose sum of prime divisors is K
- Count Numbers with N digits which consists of even number of 0’s
- Count of Numbers such that difference between the number and sum of its digits not less than L
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



