CJCoding With Joseph

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:

5

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
Topics:
Arrays
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท 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.