Goldman Sachs interview question

Solve this problem: hackerrank tests