Infosys interview question

Coding question it was tricky