Why am I getting runtime error on this simple question and everything is running fine!

This is the question - CM164364
All we have to do in this question is to find the number of unique elements and do some simple calculation. I tried using Frequency Map, Set, and Sorting to find the unique elements but every time I am getting runtime error even though the sample test case is running perfectly. No Memory or Time Limit is exceeding too.
Solution using set - CodeChef: Practical coding for everyone
Solution using frequency map - CodeChef: Practical coding for everyone
Solution using Sorting and Looping - CodeChef: Practical coding for everyone
I also tried using Long instead of Int even though the constraints does not require Long.

Can anyone please look into it and help, I know the language is Kotlin and whenever I say Kotlin no one seems to help :cry:, The code is pretty simple, self explanatory and not much different than any modern language. Kotlin also compiles to java bytecode so I hope any java user could help too.

Try to add trim() before split(' ') when you are reading input. Your current solution fails with a runtime error if the input data contains an extra space at the end of the line.

For example, if you change “50 50 50 100 100” to "50 50 50 100 100 " in the sample input, then your solution fails with the following error in the “Compile & Run” mode:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:678)
	at java.base/java.lang.Integer.parseInt(Integer.java:786)
	at ProgKt.sol(prog.kt:7)
	at ProgKt.main(prog.kt:18)
2 Likes

Thanks a lot!! I have never faced this problem on CC before because the testcases are generated by some algorithm and this seems a manual mistake somewhere.
But thanks again!! I’ll look out for this in future too.

You are welcome, I’m glad that my comment helped. As for how such testcases could have been generated, I think that most likely somebody just used C++ code without bothering not to print an extra space character at the end of each line. Something like this:

#include <iostream>
#include <vector>

int main() {
  std::vector<int> a = {1, 2, 3, 4, 5};
  for (auto x : a)
    std::cout << x << " ";
  std::cout << std::endl;
}

This code prints "1 2 3 4 5 ". And your original input handling Kotlin code would parse it as an array of strings ["1", "2", "3", "4", "5", ""] with an extra empty string at the end. And then attempt to convert these strings to numbers. The conversion of an empty string fails and that’s how we get a runtime error.

For comparison, a testcase generator written in Kotlin (or in Python which is more likely) wouldn’t have any problems with an extra space character:

fun main() {
    val a = arrayOf(1, 2, 3, 4, 5)
    println(a.joinToString(" "))
}

To sum it up. It’s better to just safeguard against this and always trim lines when parsing input in Kotlin. Because appealing after a ranked contest and trying to convince contest admins that you got a malformed input won’t be a pleasant experience.

I’ll keep that in mind and use trim every time I use readLine. Thanks for the guidance.