Hexaware Technologies interview question

Asked to solve a program in hacker rank