McKinsey & Company Interview Question

DSA Medium Question to get Optimal Amount from an array