Shopee interview question

How can we optimize this operation