Walmart interview question

Approach used to solve the problem