Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,587 views
in Online Assessments by Expert (34,270 points)
edited by | 1,587 views
0 0
class HelloWorld {
   
    static int mex(int A[],int start, int end){
        boolean bool[] = new boolean[A.length+1];
        for(int i=start;i<=end;i++){
            bool[A[i]] = true;
            // System.out.println("--"+bool[A[i]]);
        }
        
        for(int i=0;i<A.length+1;i++){
            if(!bool[i]){
                // System.out.println(i);
                return i;
            }
        }
        return -1;
    }
    static void checker(int start, int end, int arr[][], int mex,int index){
        if(arr.length==0){
            arr[0][0] = start;
            arr[0][1] = end;
            arr[0][2] = mex;
            return ;
        }
        int res = 0;
        int check = 0;
        for(int i=0;i<arr.length;i++){
            if((start>=arr[i][0]&&start<=arr[i][1])||end>=arr[i][0]&&end<=arr[i][1]){
                if(mex>arr[i][2]){
                    res = res+arr[i][2];
                    check = 1;
                }
            }
        }
        
        
           
          
        
        if(mex>res){
        for(int i=0;i<arr.length;i++){
            if((start>=arr[i][0]&&start<=arr[i][1])||end>=arr[i][0]&&end<=arr[i][1]){
                if(mex>arr[i][2]){
                   arr[i][0] = -1;
                   arr[i][1] = -1;
                   arr[i][2] =-1;
                }
            }
        }
        
         arr[index][0] = start;
         arr[index][1] = end;
         arr[index][2]= mex;
        
        }
        
        
    }
    static int result(int A[],int[][] offers){
        int res = 0;
        int added = 0;
        int prestart=-1,preend=-1,index = 0;
        int arr[][] = new int[offers.length][3];
        for(int i=0;i<offers.length;i++){
            int start = offers[i][0]-1;
            int end = offers[i][1]-1;
             int mex = mex(A, start, end);
            //  System.out.println(mex);
             if(mex!=-1){
                checker(start,end, arr, mex,index);
                index++;
             }}
        
        for(int i=0;i<index;i++){
            if(arr[i][0]!=-1){
                res = res+arr[i][2];
            }
        }
        
        return res;
        
    }
    public static void main(String[] args) {
        int offers[][] = {{1,2}};
        int A[] = {0,1};
        System.out.println(result(A,offers));
       
    }
}

1 Answer

0 like 0 dislike
by Expert (34,270 points)