Infosys interview question

to solve the coding question