Meta Interview Question

Solve this algorithm problem in an optimised way