Maximum occurred integer in n ranges | Set-3
Last Updated :
09 Sep, 2021
Given N ranges of the form L to 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.
Examples:
Input: points[] = { {1, 6}, {2, 3}, {2, 5}, {3, 8} }
Output: 3
Explanation :
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}
Hence, the most occurred integer is 3.
Input: points[] = { {1, 4}, {1, 9}, {1, 2}};
Output: 1
Approach: This is the improvised approach of Maximum occurred integer in n ranges. The main idea is to use coordinate compression using Hashing. So there is no need to create a frequency array that will exceed the memory limit for large ranges. Below is the step by step algorithm to solve this problem:
- Initialize an ordered map name points, to keep track of the starting and ending points of the given ranges.
- Increase the value of the point[L] by 1, for every starting index of the given range.
- Decrease the value of the point[R+1] by 1, for every ending index of the given range.
- Iterate the map in the increasing order of value of points. Note that the frequency[current point] = frequency[previous point] + points[current point]. Maintain the value of the frequency of the current point in a variable cur_freq.
- The point with the maximum value of cur_freq will be the answer.
- Store the index and return it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxOccurring( int range[][2], int n)
{
map< int , int > points;
for ( int i = 0; i < n; i++) {
int l = range[i][0];
int r = range[i][1];
points[l]++;
points[r + 1]--;
}
int cur_freq = 0;
int ans = 0;
int freq_ans = 0;
for ( auto x : points) {
cur_freq += x.second;
if (cur_freq > freq_ans) {
freq_ans = cur_freq;
ans = x.first;
}
}
cout << ans;
}
int main()
{
int range[][2]
= { { 1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } };
int n = sizeof (range) / sizeof (range[0]);
maxOccurring(range, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxOccurring( int range[][], int n)
{
HashMap<Integer,Integer> points = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < n; i++) {
int l = range[i][ 0 ];
int r = range[i][ 1 ];
if (points.containsKey(l)){
points.put(l, points.get(l)+ 1 );
}
else {
points.put(l, 1 );
if (points.containsKey(r + 1 )){
points.put(r + 1 , points.get(r + 1 )- 1 );
}
}
}
int cur_freq = 0 ;
int ans = 0 ;
int freq_ans = 0 ;
for (Map.Entry<Integer,Integer> entry :points.entrySet()) {
cur_freq += entry.getValue();
if (cur_freq > freq_ans) {
freq_ans = cur_freq;
ans = entry.getKey();
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int range[][]
= { { 1 , 6 }, { 2 , 3 }, { 2 , 5 }, { 3 , 8 } };
int n = range.length;
maxOccurring(range, n);
}
}
|
Python3
def maxOccurring(range1, n):
points = {}
for i in range (n):
l = range1[i][ 0 ]
r = range1[i][ 1 ]
if l in points:
points[l] + = 1
else :
points[l] = 1
if r + 1 in points:
points[r + 1 ] - = 1
else :
points[r + 1 ] = 0
cur_freq = 0
ans = 0
freq_ans = 0
for key,value in points.items():
cur_freq + = value
if (cur_freq > freq_ans):
freq_ans = cur_freq
ans = key
print (ans)
if __name__ = = '__main__' :
range1 = [[ 1 , 6 ],[ 2 , 3 ],[ 2 , 5 ],[ 3 , 8 ]]
n = len (range1)
maxOccurring(range1, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maxOccurring( int [,]range, int n)
{
Dictionary< int , int > points = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
int l = range[i,0];
int r = range[i,1];
if (points.ContainsKey(l))
points[l]++;
else
points.Add(l,1);
if (points.ContainsKey(r+1))
points[r + 1]--;
else
points.Add(r+1,0);
}
int cur_freq = 0;
int ans = 0;
int freq_ans = 0;
foreach (KeyValuePair< int , int > entry in points) {
cur_freq += entry.Value;
if (cur_freq > freq_ans) {
freq_ans = cur_freq;
ans = entry.Key;
}
}
Console.Write(ans);
}
public static void Main()
{
int [,]range = {{1, 6 }, { 2, 3 }, { 2, 5 }, { 3, 8 } };
int n = 4;
maxOccurring(range, n);
}
}
|
Javascript
<script>
function maxOccurring(range, n)
{
let points = new Map();
for (let i = 0; i < n; i++) {
let l = range[i][0];
let r = range[i][1];
if (points.has(l))
points.set(points.get(l), points.get(l) + 1);
else
points.set(l, 1);
if (points.has(r + 1)) {
points.set(points.get(r + 1), points.get(r + 1) - 1);
}
}
let cur_freq = 0;
let ans = 0;
let freq_ans = 0;
for (let [key, value] of points)
{
cur_freq = cur_freq + value;
if (cur_freq > freq_ans) {
freq_ans = cur_freq;
ans = key;
}
}
document.write(ans);
}
let range
= [[1, 6], [2, 3], [2, 5], [3, 8]];
let n = range.length;
maxOccurring(range, n);
</script>
|
Time Complexity: O(n Logn)
Space Complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...