Problem: 2050D - Digital string maximization
Language: Node.js


Submission (verdict: Wrong answer on test 1):
const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

let countInput = 0
let numberTest = 0
let testCase = 0

let inputContainer = []
let outputs = []

const inputProcess = (input) => {
    testCase++
    inputContainer.push(input)
}

const compareNumbers = (a, b) => {
    return b-a
}

const arraysEqual = (arr1, arr2) => {
    if (arr1.length !== arr2.length) return false

    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] !== arr2[i]) return false
    }

    return true
}

const sortDescendingArray = (array) => {
    return array.sort(compareNumbers)
}


const computeOutput = () => {    
    
    for (const stringNumber of inputContainer) {
        
        // Il me faut une boucle while qui va continuer cette boucle for
        // Tant que l'on a pas une string qui est décroissante dans les chiffres
        let holder = []
        for (let i = 0; i < stringNumber.length; i++) {
            holder.push(parseInt(stringNumber[i]))
        }
        // Create a copy of the holder array
        let unsortedArray = [...holder]
        // Descending order, we're making a copy of holder because holder will be affected otherwise
        let sortedArray = sortDescendingArray([...holder])
        // console.log(`Unsorted : ${unsortedArray}`)
        // console.log(`Sorted : ${sortedArray}`)
        // console.log(`Base array : ${holder}`)

        let previousHolderInt = [parseInt(stringNumber)]

        do {
            let previousDigit = 0
            
            for (let i = 0; i < holder.length; i++) {
                if (i > 0) {
                    if ( (holder[i] > 1) && (holder[i] > previousDigit)) {
                        holder[i-1] = holder[i] - 1
                        holder[i] = previousDigit
                        // console.log(`On ${i} number : ${holder[i]}, on ${i-1} : ${holder[i-1]}`)
                    }
                }
                previousDigit = holder[i]

                unsortedArray = [...holder]
                sortedArray = sortDescendingArray([...holder])
            }

            holderString = ""
            for (const element of holder) {
                holderString = holderString.concat(element.toString())
            }
            previousHolderInt.push(parseInt(holderString))

            // console.log(`After changing holder : ${holder}`)
        } while (!arraysEqual(unsortedArray, sortedArray) && 
            (previousHolderInt[previousHolderInt.length - 1] >= previousHolderInt[previousHolderInt.length - 2]))

        // console.log(`${previousHolderInt[previousHolderInt.length - 1]} < ${previousHolderInt[previousHolderInt.length - 2]} ?`)

        if (previousHolderInt[previousHolderInt.length - 1] < previousHolderInt[previousHolderInt.length - 2]) {
            holderString = previousHolderInt[previousHolderInt.length - 2].toString()
        } else {
            holderString = previousHolderInt[previousHolderInt.length - 1].toString()
        }

        outputs.push(holderString)
    }
}


const printOutput = () => {
    for (const output of outputs) {
        console.log(output)
    }
}




rl.on('line', (input) => {
    if (countInput === 0) {
        testNumber = parseInt(input)
        countInput++
        return
    }
    inputProcess(input)
    if (testCase === testNumber) {
        computeOutput()
        printOutput()
        
        rl.close()
    }
    // console.log(inputContainer)

})