Here is one question from hackerrank, I have a solution but there is some testcase failed. I don\'t know the better solution for it enter image description here