CJCoding With Joseph
15free / daySign inCreate account

Best Buy and Sell Profit

The array {7, 1, 5, 3, 6, 4} holds daily prices in order. Find the largest value of arr[j] - arr[i] where the sell day j comes AFTER the buy day i, and print it (see Expected Output).

Buying at 1 and selling later at 6 gives the best profit of 5. If no later day is higher, the profit is 0. Solve it in one pass by tracking the smallest price seen so far.

Expected Output:

5

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Arrays
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.