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
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; } |
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
<?php // PHP program to check the most occuring // element in given range // Function that returns the maximum element. function maxOccuring(&$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 occuring 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 maxOccuring($range, $n); // This code is contributed by ita_c ?> |
3
Recommended Posts:
- Maximum occurred integer in n ranges
- Elements that occurred only once in the array
- Count Primes in Ranges
- Search an element in given N ranges
- Products of ranges in an array
- Compress the array into Ranges
- GCDs of given index ranges in an array
- Find the missing elements from 1 to M in given N ranges | Set-2
- Queries on probability of even or odd number in given ranges
- Find the missing elements from 1 to M in given N ranges
- Find a range that covers all the elements of given N ranges
- Queries to check if a number lies in N ranges of L-R
- Find the kth element in the series generated by the given N ranges
- Print matrix after applying increment operations in M ranges
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
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.



