New Year Chaos

  • + 0 comments

    public static void minimumBribes(List q) { // Write your code here

        int arr[] = new int[q.size()];
        int j=0;
        int minBribe = 0;
        for(Integer i : q){
            arr[j++] = i;
        }
    
        for(int i=0; i < arr.length-1;i++){
            if(arr[i] > i+1){
                // System.out.println(arr[i]+"here");
                int diff = arr[i] - (i +1 );
                if(diff <= 2 ){
                    minBribe += diff;
                }else{
                    System.out.println("Too chaotic");
                    return;
                }
            }
            else if(arr[i] > arr[i+1] ){
                // System.out.println(arr[i]+"*");
                minBribe += 1;  
            }
    
    
        }
        System.out.println(minBribe);
    }
    

    Can anyone help me here. and explain where I am going wrong here. in what case my solution is failing.