Maximum Occured Integer

PHOTO EMBED

Mon Feb 07 2022 15:07:44 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #lecture #arrays #prefixsum #maximumoccuredinteger

import java.util.*;
import java.io.*;

class GFG 
{ 
    static int maxOcc(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]-=1; // arr[R[i] + 1]--;
	    	}

	    	int maxm = arr[0], res = 0;

	    	for(int i = 1; i < maxx; i++) // maxx = 1000000
	    	{
	    		arr[i] += arr[i - 1];

	    		if(maxm < arr[i])
	    		{
	    			maxm = arr[i];

	    			res = i;
	    		}
	    	}

	    	return res;
    }
    public static void main(String args[]) 
    { 
       int L[] = {1, 2, 3}, R[] = {3, 5, 7}, n = 3;

      System.out.println(maxOcc(L, R, n)); 
    } 
}
content_copyCOPY

Maximum occured integer -------------------------------- Given n ranges, find the maximum appearing element in their ranges. For small values, 0 <= L[i], R[i] < 10^6 Input : ------------------ L[] = {1, 2, 5, 15} R[] = {5, 8, 7, 18} Output: ----------------- 5

https://www.geeksforgeeks.org/maximum-occurred-integer-n-ranges/