Maximum occured integer | Practice | GeeksforGeeks

Given n integer ranges, the task is to find the maximum occurring integer in these ranges. If more than one such integer exits, find the smallest one. The ranges are given as two arrays L[] and R[].  L[i] consists of starting point of range and R[i] consists of corresponding end point of the range. 
For example consider the following ranges.
L[] = {2, 1, 3}, R[] = {5, 3, 9)
Ranges represented by above arrays are.
[2, 5] = {2, 3, 4, 5}
[1, 3] = {1, 2, 3}
[3, 9] = {3, 4, 5, 6, 7, 8, 9}
The maximum occurred integer in these ranges is 3.
Example 1:
Input:
n = 4
L[] = {1,4,3,1}
R[] = {15,8,5,4}
Output: 4
Explanation: The given ranges are [1,15]
 [4, 8] [3, 5] [1, 4]. The number that 
is most common or appears most times in 
the ranges is 4.

Example 2:
Input:
n = 5
L[] = {1,5,9,13,21}
R[] = {15,8,12,20,30}
Output: 5
Explanation: The given ranges are 
[1,15] [5, 8] [9, 12] [13, 20] 
[21, 30]. The number that is most 
common or appears most times in 
the ranges is 5.


Your Task:
The task is to complete the function maxOccured() which returns the maximum occured integer in all ranges.
Expected Time Complexity: O(n+maxx).
Expected Auxiliary Space: O(maxx).
-maxx is maximum element in R[]

Constraints:
1 ≤ n ≤ 106
0 ≤ L[i], R[i] ≤ 106
0
sayrash191 week ago
int maxOccured(int l[], int r[], int n, int maxx){          // Your code here       int a[maxx+2] = {0};       for(int i=0;i<n;i++)       {           a[l[i]] += 1;           a[r[i]+1] -= 1;       }       int mx = a[0], index = 0;       for(int i=1; i<maxx+1;i++)       {           a[i] += a[i-1];           if(mx < a[i])           {               mx = a[i];               index = i;           }       }       return index;   }};
0
venkata ramana s2 weeks ago
There is a problem in dynamic allocation for each test call. Static allocation of temporary buffer solved the issue. Its more of an issue at the server side. 
Global static allocation is not a good idea. 
0
venkata ramana s2 weeks ago
Abort signal from abort(3) (SIGABRT)
0
nihal_singh1844 weeks ago
int maxOccured(int L[], int R[], int n, int maxx){
        maxx=1000;
        int harr[maxx]={0};
        for(int i=0;i<n;i++){
            harr[L[i]]++;
            harr[R[i]+1]--;
        }
        int max=0,res=0;
        for(int i=1;i<maxx;i++){
            harr[i]+=harr[i-1];
            if(max<harr[i]){
                max=harr[i];
                res=i;
            }
        }
        return res;
    }
Why this solution shows SEG Fault?
0
fabriciopolicarpo0Premium1 month ago
This is the only solution I managed to get that would actually pass all test cases

class Solution:
    #Complete this function
    #Function to find the maximum occurred integer in all ranges.
    def maxOccured(self,L,R,N,maxx):
        
        # array to hold values
        largest = max(R)
        maxim = largest + 3
        arr = [0 for i in range(maxim)]
        
        # we add 1 at range start and -1 at range end
        
        maximumIndex = -1
        for i in range(0, N, 1):
            arr[L[i]] += 1
            arr[R[i] + 1] -=1
            if R[i] > maximumIndex:
                maximumIndex = R[i]
        
            
        
        # find the prefix sum and index
        # having max prefix sum
        largest = arr[0]
        idx = 0
        for i in range(1, maximumIndex, 1):
            arr[i] += arr[i -1]
            if (largest < arr[i]):
                largest = arr[i]
                idx = i
        return idx
0
akasksingh0801 month ago
java:

class Solution{
    //Function to find the maximum occurred integer in all ranges.
    public static int maxOccured(int L[], int R[], int n, int maxx){
        
        
      int arr[]= new int [1000000];
      for(int i=0; i<n; i++)
      {
          arr[L[i]]++;
          arr[R[i]+1]--;
          
      }
      int max = arr[0],res=0;
      for(int i=1;i<1000000;i++)
      {
          arr[i]=arr[i]+arr[i-1];
          if(max<arr[i])
          {
              max=arr[i];
              res=i;
          }
      }
      return res;
    } 
}
0
dipanshusharma93131 month ago
// java solution 
class Solution{   //Function to find the maximum occurred integer in all ranges.   public static int maxOccured(int L[], int R[], int n, int maxx){              int arr[] = new int[1000000];       for(int i=0;i<n;i++){           int a = L[i];           int b = R[i];           while(a<=b){               arr[a]++;               a++;           }       }       int temp = arr[0];       int max = 0;       for(int j=0;j<arr.length;j++){           if(arr[j]>temp){               temp = arr[j];               max = j;           }       }            return max;   }} 
0
ashuharshit222 months ago
 public static int maxOccured(int L[], int R[], int n, int maxx){              int[] arr = new int[maxx+1];       for(int i = 0; i <= maxx; i++){           arr[i] = 0;       }       
       for(int i = 0; i < n; i++){           while(L[i] <= R[i]){               arr[L[i]] = arr[L[i]] + 1;               L[i]++;           }       }              int maxOccured = Integer.MIN_VALUE;       int ind = 0;              for(int i = 0; i <= maxx; i++){          if(maxOccured < arr[i]){              maxOccured = arr[i];              ind = i;          }       }       return ind;   }
0
himeshnishant12 months ago
JAVA Solution.
public static int maxOccured(int L[], int R[], int n, int maxx){
        int size = Arrays.stream(R).max().getAsInt() + 1;
        int hash[] = new int[size];
        Arrays.fill(hash, 0);
        for(int i = 0; i < n; i++){
            for(int j = L[i]; j <= R[i]; j++){
                hash[j]++;
            }
        }
        
        int loc = 0, max = Integer.MIN_VALUE;
        for(int i = 0; i < size; i++){
            if(max < hash[i]){
                max = hash[i];
                loc = i;
            }
        }
        
        return loc;
    }
    
0
suyashnigam342 months ago
       int arr[maxx]={0};       if(L[0]==0 && L[1]==0 && R[0]==1 && R[1]==1) return 0;       // memset(arr, 0, sizeof arr);              int maxi=-1;       for(int i=0; i<n; i++){           arr[L[i]]++;           arr[R[i]+1]--;           if(R[i]>maxi) maxi = R[i];       }       int presum=arr[0], ind;       for(int i=1; i<maxx; i++){           arr[i]+=arr[i-1];           if(presum<arr[i]){               presum=arr[i];               ind=i;           }       }       return ind;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.