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

Previous submission (verdict: Time limit exceeded on test 3):
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 isFinalNumber = (array) => {
    // Criterias : 
    // The difference between the previous digit and the current one starting from the 2nd one
    // has to be either < 0, = 0, = 1
    for (let i = 1; i < array.length; i++) {
        const difference = array[i] - array[i - 1];
        if (!(difference < 0 || difference === 0 || difference === 1)) {
            return false
        }
    }
    return true
}


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

        while (!isFinalNumber(holder)) {
            let previousDigit = 0
            
            for (let i = 0; i < holder.length; i++) {
                if (i > 0) {
                    if ( (holder[i] > 1) && (holder[i] - previousDigit > 1)) {
                        holder[i-1] = holder[i] - 1
                        holder[i] = previousDigit
                    }
                }
                previousDigit = holder[i]

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

        holderString = ""
        for (const element of holder) {
            holderString = holderString.concat(element.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)

})


Submission (verdict: Accepted):
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 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 modified = true
        while (modified) {
            let previousDigit = 0
            modified = false
            
            for (let i = 0; i < holder.length; i++) {
                if (i > 0) {
                    if ( (holder[i] > 1) && (holder[i] - previousDigit > 1)) {
                        holder[i-1] = holder[i] - 1
                        holder[i] = previousDigit
                        modified = true
                    }
                }
                previousDigit = holder[i]
            }
        }

        holderString = ""
        for (const element of holder) {
            holderString = holderString.concat(element.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)

})