Skip to content
Related Articles

Related Articles

Maximum occurred integer in n ranges | Set-2

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 30 Dec, 2021

Given N ranges of L-R. The task is to print the number which occurs the maximum number of times in the given ranges. 
Note: 1 <= L <= R <= 106
Examples: 
 

Input: range[] = { {1, 6}, {2, 3}, {2, 5}, {3, 8} } 
Output:
1 occurs in 1 range {1, 6} 
2 occurs 3 in 3 range {1, 6}, {2, 3}, {2, 5} 
3 occurs 4 in 4 range {1, 6}, {2, 3}, {2, 5}, {3, 8} 
4 occurs 3 in 3 range {1, 6}, {2, 5}, {3, 8} 
5 occurs 3 in 3 range {1, 6}, {2, 5}, {3, 8} 
6 occurs 2 in 2 range {1, 6}, {3, 8} 
7 occurs 1 in 1 range {3, 8} 
8 occurs 1 in 1 range {3, 8}
Input: range[] = { {1, 4}, {1, 9}, {1, 2}}; 
Output: 1

 

Approach: The approach is similar to Maximum occurred integer in n ranges. The only thing that is different is to find the lower and upper bound of ranges. So that there is no need to traverse from 1 to MAX.
Below is the step by step algorithm to solve this problem: 
 

  1. Initialize a freq array with 0, let the size of the array be 10^6 as this is the maximum possible.
  2. Increase the freq[l] by 1, for every starting index of the given range.
  3. Decrease the freq[r+1] by 1 for every ending index of the given range.
  4. Iterate from the minimum L to the maximum R and add the frequencies by freq[i] += freq[i-1].
  5. The index with the maximum value of freq[i] will be the answer.
  6. Store the index and return it.

Below is the implementation of above approach: 
 

C++




// C++ program to check the most occurring
// element in given range
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns the maximum element.
int maxOccurring(int range[][2], int n)
{
 
    // freq array to store the frequency
    int freq[(int)(1e6 + 2)] = { 0 };
 
    int first = 0, last = 0;
 
    // iterate and mark the hash array
    for (int i = 0; i < n; i++) {
        int l = range[i][0];
        int r = range[i][1];
 
        // increase the hash array by 1 at L
        freq[l] += 1;
 
        // Decrease the hash array by 1 at R
        freq[r + 1] -= 1;
 
        first = min(first, l);
        last = max(last, r);
    }
 
    // stores the maximum frequency
    int maximum = 0;
    int element = 0;
 
    // check for the most occurring element
    for (int i = first; i <= last; i++) {
 
        // increase the frequency
        freq[i] = freq[i - 1] + freq[i];
 
        // check if is more than the previous one
        if (freq[i] > maximum) {
            maximum = freq[i];
            element = i;
        }
    }
 
    return element;
}
 
// Driver code
int main()
{
    int range[][2] = { { 1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } };
    int n = 4;
 
    // function call
    cout << maxOccurring(range, n);
 
    return 0;
}

Java




// Java program to check the most occurring
// element in given range
class GFG
{
 
// Function that returns the maximum element.
static int maxOccurring(int range[][], int n)
{
 
    // freq array to store the frequency
    int []freq = new int[(int)(1e6 + 2)];
 
    int first = 0, last = 0;
 
    // iterate and mark the hash array
    for (int i = 0; i < n; i++)
    {
        int l = range[i][0];
        int r = range[i][1];
 
        // increase the hash array by 1 at L
        freq[l] += 1;
 
        // Decrease the hash array by 1 at R
        freq[r + 1] -= 1;
 
        first = Math.min(first, l);
        last = Math.max(last, r);
    }
 
    // stores the maximum frequency
    int maximum = 0;
    int element = 0;
 
    // check for the most occurring element
    for (int i = first+1; i <= last; i++)
    {
 
        // increase the frequency
        freq[i] = freq[i - 1] + freq[i];
 
        // check if is more than the previous one
        if (freq[i] > maximum)
        {
            maximum = freq[i];
            element = i;
        }
    }
    return element;
}
 
// Driver code
public static void main(String[] args)
{
    int range[][] = { { 1, 6 }, { 2, 3 },
                      { 2, 5 }, { 3, 8 } };
    int n = 4;
 
    // function call
    System.out.println(maxOccurring(range, n));
}
}
 
// This code is contributed by PrinciRaj1992

Python3




# Python3 program to check the most
# occurring element in given range
 
# Function that returns the
# maximum element.
def maxOccurring(range1, n):
     
    # freq array to store the frequency
    freq = [0] * 1000002;
     
    first = 0;
    last = 0;
     
    # iterate and mark the hash array
    for i in range(n):
        l = range1[i][0];
        r = range1[i][1];
         
        # increase the hash array by 1 at L
        freq[l] += 1;
         
        # Decrease the hash array by 1 at R
        freq[r + 1] -= 1;
        first = min(first, l);
        last = max(last, r);
     
    # stores the maximum frequency
    maximum = 0;
    element = 0;
     
    # check for the most occurring element
    for i in range(first, last + 1):
         
        # increase the frequency
        freq[i] = freq[i - 1] + freq[i];
         
        # check if is more than the
        # previous one
        if(freq[i] > maximum):
            maximum = freq[i];
            element = i;
    return element;
 
# Driver code
range1= [[ 1, 6 ], [ 2, 3 ],
         [ 2, 5 ], [ 3, 8 ]];
n = 4;
     
# function call
print(maxOccurring(range1, n));
 
# This code is contributed by mits

C#




// C# program to check the most occurring
// element in given range
using System;
     
class GFG
{
 
// Function that returns the maximum element.
static int maxOccurring(int [,]range, int n)
{
 
    // freq array to store the frequency
    int []freq = new int[(int)(1e6 + 2)];
 
    int first = 0, last = 0;
 
    // iterate and mark the hash array
    for (int i = 0; i < n; i++)
    {
        int l = range[i, 0];
        int r = range[i, 1];
 
        // increase the hash array by 1 at L
        freq[l] += 1;
 
        // Decrease the hash array by 1 at R
        freq[r + 1] -= 1;
 
        first = Math.Min(first, l);
        last = Math.Max(last, r);
    }
 
    // stores the maximum frequency
    int maximum = 0;
    int element = 0;
 
    // check for the most occurring element
    for (int i = first + 1; i <= last; i++)
    {
 
        // increase the frequency
        freq[i] = freq[i - 1] + freq[i];
 
        // check if is more than the previous one
        if (freq[i] > maximum)
        {
            maximum = freq[i];
            element = i;
        }
    }
    return element;
}
 
// Driver code
public static void Main(String[] args)
{
    int [,]range = {{ 1, 6 }, { 2, 3 },
                    { 2, 5 }, { 3, 8 }};
    int n = 4;
 
    // function call
    Console.WriteLine(maxOccurring(range, n));
}
}
 
// This code is contributed by Princi Singh

PHP




<?php
// PHP program to check the most occurring
// element in given range
 
// Function that returns the maximum element.
function maxOccurring(&$range, $n)
{
    // freq array to store the frequency
    $freq = array(0, 1000002, NULL);
 
    $first = 0;
    $last = 0;
 
    // iterate and mark the hash array
    for ($i = 0; $i < $n; $i++)
    {
        $l = $range[$i][0];
        $r = $range[$i][1];
 
        // increase the hash array
        // by 1 at L
        $freq[$l] += 1;
 
        // Decrease the hash array
        // by 1 at R
        $freq[$r + 1] -= 1;
 
        $first = min($first, $l);
        $last = max($last, $r);
    }
 
    // stores the maximum frequency
    $maximum = 0;
    $element = 0;
 
    // check for the most occurring element
    for ($i = $first; $i <= $last; $i++)
    {
 
        // increase the frequency
        $freq[$i] = $freq[$i - 1] + $freq[$i];
 
        // check if is more than the
        // previous one
        if ($freq[$i] > $maximum)
        {
            $maximum = $freq[$i];
            $element = $i;
        }
    }
 
    return $element;
}
 
// Driver code
$range = array(array( 1, 6 ),
               array( 2, 3 ),
               array( 2, 5 ),
               array( 3, 8 ));
$n = 4;
 
// function call
echo maxOccurring($range, $n);
 
// This code is contributed by ita_c
?>

Javascript




<script>
 
// Javascript program to check the most occurring
// element in given range   
 
// Function that returns the maximum element.
    function maxOccurring(range , n)
    {
 
        // freq array to store the frequency
        var freq = Array(parseInt(1e6 + 2)).fill(0);
 
        var first = 0, last = 0;
 
        // iterate and mark the hash array
        for (i = 0; i < n; i++) {
            var l = range[i][0];
            var r = range[i][1];
 
            // increase the hash array by 1 at L
            freq[l] += 1;
 
            // Decrease the hash array by 1 at R
            freq[r + 1] -= 1;
 
            first = Math.min(first, l);
            last = Math.max(last, r);
        }
 
        // stores the maximum frequency
        var maximum = 0;
        var element = 0;
 
        // check for the most occurring element
        for (i = first + 1; i <= last; i++) {
 
            // increase the frequency
            freq[i] = freq[i - 1] + freq[i];
 
            // check if is more than the previous one
            if (freq[i] > maximum) {
                maximum = freq[i];
                element = i;
            }
        }
        return element;
    }
 
    // Driver code
     
        var range = [ [ 1, 6 ], [ 2, 3 ],
                      [ 2, 5 ], [ 3, 8 ] ];
        var n = 4;
 
        // function call
        document.write(maxOccurring(range, n));
 
// This code contributed by gauravrajput1
 
</script>
Output: 
3

 

Note: If values the values of L and T are of order 108 then above method won’t work as there will be a memory error. We need a different but similar approach for these limits. You may think in terms of hashing.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!