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:

  1. Initialize an array arr[] to 0.
  2. For each range i, add +1 at Li index and -1 at Ri of the array.
  3. Find the prefix sum of the array and find the smallest index having maximum prefix sum.

Below is the implementation of this approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum occurred element in
// given N ranges.
#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
  
// Return the maximum occurred element in all ranges.
int maximumOccurredElement(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.
    int maxi=-1;
    for (int i = 0; i < n; i++) {
        arr[L[i]] += 1;
        arr[R[i] + 1] -= 1;
        if(R[i]>maxi){
            maxi=R[i];
        }
    }
  
    // Finding prefix sum and index having maximum
    // prefix sum.
    int msum = arr[0],ind;
    for (int i = 1; i < maxi+1; 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 << maximumOccurredElement(L, R, n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum occurred 
// element in given N ranges.
import java.io.*;
  
class GFG {
  
    static int MAX = 1000000;
  
    // Return the maximum occurred element in all ranges.
    static int maximumOccurredElement(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.
        int maxi=-1;
        for (int i = 0; i < n; i++) {
            arr[L[i]] += 1;
            arr[R[i] + 1] -= 1;
            if(R[i]>maxi){
            maxi=R[i];
           }
        }
  
        // Finding prefix sum and index
        // having maximum prefix sum.
        int msum = arr[0];
        int ind = 0;
        for (int i = 1; i < maxi+1; 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(maximumOccurredElement(L, R, n));
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find maximum occurred 
# element in given N ranges.
  
MAX = 1000000
  
# Return the maximum occurred element 
# in all ranges.
def maximumOccurredElement(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(maximumOccurredElement(L, R, n))
      
# This code is contributed by
# Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum 
// occurred element in given N ranges.
using System;
  
class GFG {
  
    static int MAX = 1000000;
  
    // Return the maximum occurred element in all ranges.
    static int maximumOccurredElement(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(maximumOccurredElement(L, R, n));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find maximum occurred
// element in given N ranges.
$MAX = 1000000;
  
// Return the maximum occurred element
// in all ranges.
function maximumOccurredElement($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 maximumOccurredElement($L, $R, $n);
  
// This code is contributed by
// Srathore
?>

chevron_right



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.



My Personal Notes arrow_drop_up