Open In App

Maximum occurring integer in given ranges

Given two arrays L[] and R[] of size N where L[i] and R[i] (0 ? L[i], R[i] < 106)denotes a range of numbers, the task is to find the maximum occurred integer in all the ranges. If more than one such integer exists, print the smallest one. 

Examples: 

Input: L[] = {1, 4, 3, 1}, R[] = {15, 8, 5, 4}
Output: 4

Input: L[] = {1, 5, 9, 13, 21}, R[] = {15, 8, 12, 20, 30}
Output: 5
Explanation: 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.

Recommended Practice

Naive Approach:

Traverse through all the ranges. Then for every range, count frequencies, make a hash table or hash map to store every item. Then traverse through other ranges and increment the frequency of every item. The item with the highest frequency is our answer. 

Time Complexity: O(N*M). Here N is the number of ranges and M is the maximum number of elements in any of the ranges.
Auxiliary Space: O(M). For Hash table.

Maximum occurred integer in n ranges using Difference array technique.

Below is the idea to solve the problem:

The idea is to use the Difference array technique. Create a vector initialized with value zero. Iterate through every range and mark the presence of the beginning of every range by incrementing the start of the range with one i.e. arr[L[i]]++ and mark the end of the range by decrementing at index one greater than the end of range by one i.e. arr[R[i]+1]–.

Now when computing the prefix sum, Since the beginning is marked with one, all the values after beginning will be incremented by one. Now as increment is only targeted only till the end of the range, the decrement on index R[i]+1 prevents that for every range i.

Illustration:

L[] = {1, 2, 3} , R[] = {3, 5 , 7}

1. For beginning of range arr[L[i]]++ the array becomes {0,1,1,1,0,0,0,0,……}

2. For end of range arr[R[i]+1]– the array becomes  {0,1,1,1,-1, 0, -1, 0,-1,……}

3. After prefix sum the array becomes {0,1,2,3,2,2,1,1,0…}

Do prefix sum, the sum of elements after (1) is incremented by one because beginning was marked. Now elements after (3) must not be incremented so if there’s a range one, two, three, the values from one, two, three should only be incremented by one or their frequency should be incremented by one.

That is why decreasing the value of arr[R[i]+1] is needed so that elements after the end of this range have minus one subtracted to values. That is how to nullify the impact of incrementing the value when prefix will be taken.

So when taking the prefix, simply decrement every value after the range ends, since I want to increment elements only in the range. That’s the idea of this algorithm. 

Follow the below steps to Implement the idea:

Below is the Implementation of the above approach:




// 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)
{
    // Initialising all element of array to 0.
    int arr[MAX];
    memset(arr, 0, sizeof arr);
 
    // Adding +1 at Li index and subtracting 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 code
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;
}




// 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)
    {
        // Initialising all element of array to 0.
        int[] arr = new int[MAX];
 
        // Adding +1 at Li index and
        // subtracting 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.




# 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):
 
    # Initialising all element of array to 0.
    arr = [0 for i in range(MAX)]
 
    # Adding +1 at Li index and subtracting 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




// 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)
    {
        // Initialising all element of array to 0.
        int[] arr = new int[MAX];
 
        // Adding +1 at Li index and
        // subtracting 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.




<?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)
{
 
    // Initialising 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
?>




<script>
 
    // JavaScript program to find maximum
    // occurred element in given N ranges.
     
    let MAX = 1000000;
   
    // Return the maximum occurred element in all ranges.
    function maximumOccurredElement(L, R, n)
    {
        // Initialising all element of array to 0.
        let arr = new Array(MAX);
        arr.fill(0);
   
        // Adding +1 at Li index and
        // subtracting 1 at Ri index.
        for (let i = 0; i < n; i++) {
            arr[L[i]] += 1;
            arr[R[i] + 1] -= 1;
        }
   
        // Finding prefix sum and index
        // having maximum prefix sum.
        let msum = arr[0];
        let ind = 0;
        for (let i = 1; i < MAX; i++) {
            arr[i] += arr[i - 1];
            if (msum < arr[i]) {
                msum = arr[i];
                ind = i;
            }
        }
   
        return ind;
    }
     
    let L = [ 1, 4, 9, 13, 21 ];
    let R = [ 15, 8, 12, 20, 30 ];
    let n = L.length;
    document.write(maximumOccurredElement(L, R, n));
     
</script>

Output
4

Time Complexity: O(N + MAX) 
Auxiliary space: O(MAX)

Exercise: Try for 0 <= Li, Ri <= 1000000000. (Hint: Use stl map).

Related Article: Maximum value in an array after m range increment operations

 


Article Tags :