Applied Intuition interview question

Try to solve a program from leet code