Best single trade
Return the largest nonnegative profit from buying then selling once at later indices. Empty or decreasing prices return zero.
earlier_low, price = 1, 6
print(price-earlier_low)Reason about the state
Track the lowest earlier price and the best profit so far. Every price is visited once with constant extra space. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the largest nonnegative profit from buying then selling once at later indices. Empty or decreasing prices return zero.
The explanation opens here once every check passes.