No such element Exception(April Long Challenge)

CodeChef: Practical coding for everyone[my solution]
question

You should check the editorial editorial