# Maximum occurred integer in n ranges | Set-2

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: 3
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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 occuring ` `// element in given range ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns the maximum element. ` `int` `maxOccuring(``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 occuring 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 << maxOccuring(range, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check the most occuring ` `// element in given range ` `class` `GFG  ` `{  ` ` `  `// Function that returns the maximum element. ` `static` `int` `maxOccuring(``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 occuring 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(maxOccuring(range, n)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992  `

## Python3

 `# Python3 program to check the most  ` `# occuring element in given range ` ` `  `# Function that returns the  ` `# maximum element. ` `def` `maxOccuring(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 occuring 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``(maxOccuring(range1, n)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# program to check the most occuring ` `// element in given range ` `using` `System; ` `     `  `class` `GFG  ` `{  ` ` `  `// Function that returns the maximum element. ` `static` `int` `maxOccuring(``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 occuring 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(maxOccuring(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` `maxOccuring(``\$range``, ``\$n``); ` ` `  `// This code is contributed by ita_c ` `?> `

Output:

```3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.