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 occured 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 occured in the given range on doing a pre-sum.
Below is the implementation of the above approach.
Time-Complexity: O(N + max(rangeRvalue))
- 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 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.