# Maximum occurred integer in n ranges | Set-2

• Difficulty Level : Hard
• Last Updated : 10 May, 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

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ``using` `namespace` `std;` `// Function that returns the maximum element.``int` `maxOccurring(``int` `range[], ``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];``        ``int` `r = range[i];` `        ``// 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[] = { { 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

 ` ``\$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

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