Hackerearth had it's version of long challenge which ended yesterday. It had this problem. I wonder if hackerearth or may be any site hosting competitive programming test the limits for questions for most languages. This question seems to be written in such a way (not intentionally) that it can be only solved using c++/c only. For java, the memory limits seem to be insufficient. I would like to see a solution written in java.