Meta interview question

Solve this algorithm problem in an optimised way