• + 0 comments

    function minimumBribes(q) { let totalBribes = 0;

    for (let i = 0; i < q.length; i++) {
        let current = q[i];
        // Check if the person has moved more than 2 places ahead
        if (current - (i + 1) > 2) {
            console.log("Too chaotic");
            return;
        }
    
        // Count how many times the current person has been overtaken
        // Only check up to two positions behind their original position
        for (let j = Math.max(0, current - 2); j < i; j++) {
            if (q[j] > current) {
                totalBribes++;
            }
        }
    }
    
    console.log(totalBribes);
    

    }

    function main() { const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout });

    let inputLines = [];
    
    rl.on('line', (line) => {
        inputLines.push(line.trim());
    });
    
    rl.on('close', () => {
        let index = 0;
        const t = parseInt(inputLines[index++], 10);
    
        for (let i = 0; i < t; i++) {
            const n = parseInt(inputLines[index++], 10);
            const q = inputLines[index++].split(' ').map(Number); getting error
            minimumBribes(q);
        }
    });
    

    }