C3 AI interview question

Problem formulaiton reduce time complexity