Goldman Sachs Interview Question

Solve this problem: hackerrank tests