Nokia Interview Question

Coding problem and time optimality