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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, 2
rangeLvalue = [91, 92, 97]
rangeRvalue = [94, 97, 99]
print(countNumbers(n, k, rangeLvalue,
                         rangeRvalue))
  
# This code is contributed by mohit kumar

chevron_right


Output:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = 3
k = 2
  
rangeLvalue = [91, 92, 97]
rangeRvalue = [94, 97, 99]
  
print(countNumbers(n, k, rangeLvalue, rangeRvalue))
  
# This code is contributed by ankush_953

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

4

Time-Complexity: O(N + max(rangeRvalue[]))
Auxillary-Space: O(N)



My Personal Notes arrow_drop_up

Do your best to show the world what you are capable of

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.