you want to buy a particular stock at its lowest price and sell it later at its highest price,since the stock market is unpredictable,you steal the price plans of a company for this stock for the next N days.
Find the best price you can get to buy this stock to achieve maximum profit.
Note:The initial price of the stock is 0.
Input1:Nnumber of days.
Input2:Array representing changing stock price for the day.
Output: Your function must return the best price to buy the stock at.
Example:
Input1:5
Input2:{-39957,-17136,35466,21820,-26711}
Output:-57093
public class TestMain {
public static void main(String[] args){
int numberDays = 5;
int[] array = {-39957, -17136, 35466, 21820, -26711};
int[] arrayNew = new int[4];
for (int i = 0; i < numberDays - 1; i++) {
arrayNew[i] = array[i] + array[i + 1];
}
System.out.println(IntStream.of(arrayNew).min().orElse(0));
}
}
Comments
Leave a comment