World war preparation (code wars 1.0)

why is the following approach getting a runtime error in java but an ac in c++
java code
c++ code

otherwise if anyone can show me the approach, then it will be helpful as well