Maximum occurred integer in n ranges
Given n ranges of the form L and R, the task is to find the maximum occurred integer in all the ranges. If more than one such integer exists, print the smallest one.
0 <= Li, Ri < 1000000.
Examples :
Input : L1 = 1 R1 = 15
L2 = 4 R2 = 8
L3 = 3 R3 = 5
L3 = 1 R3 = 4
Output : 4
Input : L1 = 1 R1 = 15
L2 = 5 R2 = 8
L3 = 9 R3 = 12
L4 = 13 R4 = 20
L5 = 21 R5 = 30
Output : 5
Numbers having maximum occurrence i.e 2 are 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15. The smallest number
among all are 5.
A simple solution is to use hash table to store counts of all numbers. We traverse every interval from Li to Ri and increment count of all numbers present in every interval. Finally we traverse the hash table to find the number with maximum count. Time complexity of this solution is O(n * MAX_INTERVAL) where MAX_INTERVAL is maximum number of elements in an interval.
An efficient solution requires linear time. We create an array arr[] of size 1000000 (limit given on maximum value of an interval). The idea is to add +1 to each Li index and -1 to corresponding Ri index in arr[]. After this, find the prefix sum of the array. Adding +1 at Li shows the starting point of ith Range and adding -1 shows the ending point of ith range. Finally we return the array index that has maximum prefix sum
Algorithm to solve the problem:
- Initialize an array arr[] to 0.
- For each range i, add +1 at Li index and -1 at Ri of the array.
- Find the prefix sum of the array and find the smallest index having maximum prefix sum.
Below is the implementation of this approach:
C++
// C++ program to find maximum occured element in // given N ranges. #include <bits/stdc++.h> #define MAX 1000000 using namespace std; // Return the maximum occurred element in all ranges. int maximumOccuredElement(int L[], int R[], int n) { // Initalising all element of array to 0. int arr[MAX]; memset(arr, 0, sizeof arr); // Adding +1 at Li index and substracting 1 // at Ri index. for (int i = 0; i < n; i++) { arr[L[i]] += 1; arr[R[i] + 1] -= 1; } // Finding prefix sum and index having maximum // prefix sum. int msum = arr[0], ind; for (int i = 1; i < MAX; i++) { arr[i] += arr[i - 1]; if (msum < arr[i]) { msum = arr[i]; ind = i; } } return ind; } // Driven Program int main() { int L[] = { 1, 4, 9, 13, 21 }; int R[] = { 15, 8, 12, 20, 30 }; int n = sizeof(L) / sizeof(L[0]); cout << maximumOccuredElement(L, R, n) << endl; return 0; } |
Java
// Java program to find maximum occured // element in given N ranges. import java.io.*; class GFG { static int MAX = 1000000; // Return the maximum occurred element in all ranges. static int maximumOccuredElement(int[] L, int[] R, int n) { // Initalising all element of array to 0. int[] arr = new int[MAX]; // Adding +1 at Li index and // substracting 1 at Ri index. for (int i = 0; i < n; i++) { arr[L[i]] += 1; arr[R[i] + 1] -= 1; } // Finding prefix sum and index // having maximum prefix sum. int msum = arr[0]; int ind = 0; for (int i = 1; i < MAX; i++) { arr[i] += arr[i - 1]; if (msum < arr[i]) { msum = arr[i]; ind = i; } } return ind; } // Driver program static public void main(String[] args) { int[] L = { 1, 4, 9, 13, 21 }; int[] R = { 15, 8, 12, 20, 30 }; int n = L.length; System.out.println(maximumOccuredElement(L, R, n)); } } // This code is contributed by vt_m. |
Python3
# Python 3 program to find maximum occured # element in given N ranges. MAX = 1000000 # Return the maximum occurred element # in all ranges. def maximumOccuredElement(L, R, n): # Initalising all element of array to 0. arr = [0 for i in range(MAX)] # Adding +1 at Li index and substracting 1 # at Ri index. for i in range(0, n, 1): arr[L[i]] += 1 arr[R[i] + 1] -= 1 # Finding prefix sum and index # having maximum prefix sum. msum = arr[0] for i in range(1, MAX, 1): arr[i] += arr[i - 1] if (msum < arr[i]): msum = arr[i] ind = i return ind # Driver Code if __name__ == '__main__': L = [1, 4, 9, 13, 21] R = [15, 8, 12, 20, 30] n = len(L) print(maximumOccuredElement(L, R, n)) # This code is contributed by # Sanjit_Prasad |
C#
// C# program to find maximum // occured element in given N ranges. using System; class GFG { static int MAX = 1000000; // Return the maximum occurred element in all ranges. static int maximumOccuredElement(int[] L, int[] R, int n) { // Initalising all element of array to 0. int[] arr = new int[MAX]; // Adding +1 at Li index and // substracting 1 at Ri index. for (int i = 0; i < n; i++) { arr[L[i]] += 1; arr[R[i] + 1] -= 1; } // Finding prefix sum and index // having maximum prefix sum. int msum = arr[0]; int ind = 0; for (int i = 1; i < MAX; i++) { arr[i] += arr[i - 1]; if (msum < arr[i]) { msum = arr[i]; ind = i; } } return ind; } // Driver program static public void Main() { int[] L = { 1, 4, 9, 13, 21 }; int[] R = { 15, 8, 12, 20, 30 }; int n = L.Length; Console.WriteLine(maximumOccuredElement(L, R, n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find maximum occured // element in given N ranges. $MAX = 1000000; // Return the maximum occurred element // in all ranges. function maximumOccuredElement($L, $R, $n) { // Initalising all element // of array to 0. $arr = array(); for($i = 0; $i < $n; $i++) { $arr[] = "0"; } // Adding +1 at Li index and subtracting 1 // at Ri index. for ($i = 0; $i < $n; $i++) { $arr[$L[$i]] += 1; $arr[$R[$i] + 1] -= 1; } // Finding prefix sum and index // having maximum prefix sum. $msum = $arr[0]; for ($i = 1; $i <$n; $i++) { $arr[$i] += $arr[$i - 1]; if ($msum < $arr[$i]) { $msum = $arr[$i]; $ind = $i; } } return $ind; } // Driver Code $L = array(1, 4, 9, 13, 21); $R = array(15, 8, 12, 20, 30); $n = count($L); echo maximumOccuredElement($L, $R, $n); // This code is contributed by // Srathore ?> |
Output:
4
Time Complexity: O(n + MAX)
Exercise: Try for 0 <= Li, Ri <= 1000000000. (Hint: Use stl map).
Related Article : Maximum value in an array after m range increment operations
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Maximum occurred integer in n ranges | Set-2
- Elements that occurred only once in the array
- Products of ranges in an array
- Count Primes in Ranges
- Search an element in given N ranges
- Compress the array into Ranges
- 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 a range that covers all the elements of given N ranges
- Find the kth element in the series generated by the given N ranges
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Replace all ‘0’ with ‘5’ in an input Integer
- Square root of an integer



