โ Back to Arrays
Question 394
Best Buy and Sell Profit
Best Buy and Sell Profit
The array{7, 1, 5, 3, 6, 4}holds daily prices in order. Find the largest value ofarr[j] - arr[i]where the sell dayjcomes AFTER the buy dayi, and print it: 5 Buying at1and selling later at6gives the best profit of5. If no later day is higher, the profit is0. 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.