Infosys interview question

Leetcode DSA question as it is