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.
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 occurred atleast 2 times are 92, 93, 94, 97.
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:
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 occurred in the given range on doing a pre-sum.
Below is the implementation of the above approach.
Time-Complexity: O(N + max(rangeRvalue))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the elements appearing even number of times in an Array
- Length of the longest substring with every character appearing even number of times
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Minimum window size containing atleast P primes in every window of given range
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Find the kth element in the series generated by the given N ranges
- Minimum K such that every substring of length atleast K contains a character c
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Find the number of points that have atleast 1 point above, below, left or right of it
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Find the smallest subarray having atleast one duplicate
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Count of Numbers in a Range where digit d occurs exactly K times
- Print matrix after applying increment operations in M ranges
- Minimum removals required to make ranges non-overlapping
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Count how many times the given digital clock shows identical digits
- Count of subarrays which contains a given number exactly K times
- Count number of times each Edge appears in all possible paths of a given Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : ankita_saini, mohit kumar 29, ankush_953, nidhi_biet, Akanksha_Rai, more