Nokia interview question

Coding problem and time optimality