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 <= 10^{6}

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

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

- Initialize a freq array with 0, let the size of the array be 10^6 as this is the maximum possible.
- Increase the
*freq[l] by 1*, for every starting index of the given range. - Decrease the
*freq[r+1] by 1*for every ending index of the given range. - Iterate from the minimum L to the maximum R and add the frequencies by
**freq[i] += freq[i-1]**. - The index with the maximum value of freq[i] will be the answer.
- 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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the maximum element. ` `int` `maxOccuring(` `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 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[][2] = { { 1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } }; ` ` ` `int` `n = 4; ` ` ` ` ` `// function call ` ` ` `cout << maxOccuring(range, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Maximum occurred integer in n ranges
- Elements that occurred only once in the array
- Products of ranges in an array
- Count Primes in Ranges
- GCDs of given index ranges in an array
- Queries on probability of even or odd number in given ranges
- Find the missing elements from 1 to M in given N ranges | Set-2
- Find the missing elements from 1 to M in given N ranges
- Queries to check if a number lies in N ranges of L-R
- Find a range that covers all the elements of given N ranges
- Print matrix after applying increment operations in M ranges
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Replace all ‘0’ with ‘5’ in an input Integer
- Find the missing integer in an array if mean is given
- First negative integer in every window of size k

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.