McKinsey & Company interview question

DSA Medium Question to get Optimal Amount from an array