Java Challenge - Maximize Stock Trading Profit

This community-built FAQ covers the “Maximize Stock Trading Profit” code challenge in Java. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Java challenge Maximize Stock Trading Profit

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Anyone can help me?

I’m trying to resolve the challenge and I get the correct result, but my code only pass 1/5 test, what i’m doing wrong?

import java.util.*;

public class MaxProfitDays {
  public static void main(String[] args) {
    int[] stockPrices = {170, 11, 60, 25, 150, 75, 31, 120};
    int[] maxProfitDays = (maxProfitDays(stockPrices));
    System.out.println(maxProfitDays[0]);
    System.out.println(maxProfitDays[1]);
  }

  public static int[] maxProfitDays(int[] stockPrices) {
    int maximo = stockPrices[0];
    int minimo = stockPrices[0];
    int[] maxProfitDays = new int[2];
    for(int i = 0; i < stockPrices.length; i++){
      int stockPrice = stockPrices[i];
      if (stockPrice < minimo){
        minimo = stockPrice;
        int indexMin = i;
        maxProfitDays[0] = indexMin;
      }
    }
    for(int i = 0; i < stockPrices.length; i++){
      int stockPrice = stockPrices[i];
      if (stockPrice > maximo){
        maximo = stockPrice;
        int indexMax = i;
        maxProfitDays[1] = indexMax;;
      }
    }
    return maxProfitDays;
    }
  }

You have to make sure that the sell price comes before the buy price. For example, here is a set of stock prices over the course of a week: [5, 10, 12, 3, 6, 14, 2]. There is a greater difference between 2 and 14, but you cannot purchase the stock for $2 after the $14 value has already passed. In this case, the answer is (3, 14).

1 Like

Here’s my go at it.
variable “i” in the outer for loop is the purchase date
variable “j” in the inner for loop is the sell date (always starts as i + 1 so the sell date is always after the purchase date)
There’s definitely better ways to do it but was trying to make the number of checks required less as the for loop progressed (e.g. there are less sell dates to check as you progress in the purchase date)

import java.util.*;

public class MaxProfitDays {
  public static void main(String[] args) {
    int[] stockPrices = {17, 11, 60, 25, 150, 75, 31, 120};
    int[] maxProfitDays = (maxProfitDays(stockPrices));
    System.out.println(maxProfitDays[0]);
    System.out.println(maxProfitDays[1]);
  }

  public static int[] maxProfitDays(int[] stockPrices) {
    int bestProfit = 0;
    int[] sellAndBuyDays = {0, 0};
    for(int i = 0; i < stockPrices.length; i++){
      for(int j = i + 1; j < stockPrices.length; j++){
        int profit = stockPrices[j] - stockPrices[i];
        if(profit > bestProfit){
          bestProfit = profit;
          sellAndBuyDays[0] = i;
          sellAndBuyDays[1] = j;
        }
      }
    }
    return sellAndBuyDays;
  }
}