id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

gaurav.gupta@mq.edu.au arrays > maxProfit
prev  |  next  |  chance

Consider an array containing a stock's value over a certain number of days. Define a function that when passed this array, returns the maximum profit that could be made. For example, if the array is {10, 20, 70, 90, 30, 80}, buying the stock on day 1 (10) and selling on day 4 (90), and then buying it again on day 5 (30) and selling it on day 6 (80) would result in an overall profit of 130.


maxProfit([10, 20, 70, 90, 30, 80]) → 130
maxProfit([100, 180, 260, 310, 40, 535, 695]) → 865
maxProfit([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) → 13

...Save, Compile, Run (ctrl-enter)

int maxProfit(int[] data) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 4 Post-solution available

Copyright Nick Parlante 2017 - privacy