Counting Sort 3

  • + 0 comments

    why my code is not correct? when i use intellij to run, result is correct, but when i copy code and paste it on hackerank is not correct? This is Kotlin Solution :

    import java.util.*
    
    fun main(args: Array<String>) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. */
        val reader = Scanner(System.`in`)
        val n: Int = reader.nextInt()
        val arr2: Array<String> = Array(n) { " " }
        for (i in arr2.indices) {
            arr2[i] = readLine().toString()
        }
        countingSort(arr2)
    }
    fun countingSort(arr: Array<String>) {
        val numArr: Array<Int> = Array(arr.size) { 0 }
        for (i in arr.indices) {
            try {
                val numberOnly: String = arr[i].filter { it.isDigit() }
                numArr[i] = numberOnly.toInt()
            } catch (e: NumberFormatException) {
            }
        }
        val n = arr.size
        var max = Int.MIN_VALUE
        for (i in 0 until n) {
            max = maxOf(max, numArr[i])
        }
        val sum = Array(100) { 0 }
        val count: Array<Int> = Array(100) { 0 }
        for (i in 0 until n) {
            count[numArr[i]]++
    
        }
        var k = 1
        sum[0] = count[0]
        for (i in 1..99) {
            sum[k] = count[i] + sum[k - 1]
            k++
        }
        for (i in sum.indices) {
            print(sum[i].toString() + " ")
        }
    }