Answer to Question #334908 in Java | JSP | JSF for mahesh

Question #334908

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


1
Expert's answer
2022-05-01T19:05:00-0400

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));

  }

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS