← 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 (see Expected Output). 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= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
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.