Question:
Sellers buy items in Walmart during sale events and sell them on Amazon to make money. You have to help a seller make maximum profit for their purchase.
You are given two lists, one for Walmart price and another for Amazon price of an item over a period of time. The ith entry in both lists is the price of the item at time i in Walmart and Amazon.
Example:
Walmart: 4, 7, 5, 8
Amazon: 6, 8, 7, 8
Answer: 4
Seller can buy the item at 0th time index from Walmart at $4 and sell at Amazon on 1st time index at $8, making a profit of $4.
This problem asks for the maximum profit from buying at a price in the Walmart list and selling later at a price in the Amazon list. The key idea is to track the best buying opportunity seen so far and compare it with each later Amazon price to maximize the difference. In the example, buying at 4 and selling at 8 gives a profit of 4. A linear scan is enough, making the solution efficient and simple.