Hexaware Technologies interview question

Similar to the Leetcode problem.