New Year Chaos

  • + 0 comments

    public static void minimumBribes(List q) { int bribes = 0;

    for (int i = q.size() - 1; i >= 0; i--) {
        // Check if the person at position i has moved more than 2 positions forward
        if (q.get(i) - (i + 1) > 2) {
            System.out.println("Too chaotic");
            return;
        }
    
        // Count how many people have overtaken the person in position i
        for (int j = Math.max(0, q.get(i) - 2); j < i; j++) {
            if (q.get(j) > q.get(i)) {
                bribes++;
            }
        }
    }
    System.out.println(bribes);
    

    }