You are given a record of the historical prices of an investment asset from the last N days. Analyze the record in order to calculate what could have been your maximum income. Assume you started with one asset of this type and could hold at most one at a time. You could choose to sell the asset whenever you held one. If you did not hold an asset at some moment, you could always afford to buy an asset(assume you had infinite money available). What is the maximum income you could make?

Write a function in Java 8 or 11
public int solution (int[] A);
that, given an array A of length N representing a record of prices over the last N days, returns the maximum income you could make.

Example 1 - Given A= [4,1,2,3], the function should return 6. You could sell the product on the first day (for 4), buy it on the second (for 1) and sell it again on the last day (for 3). The income would be equal to 4-1+3 = 6 codility solution

Q&A Education