import java.util.*; public class Stock { public int maxProfit(int[] prices, int n) { // write code here int min1 = prices[0]; int max = 0; for (int i = 1; i < prices.length; i++) { min1 = Math.min(prices[i], min1); int a = prices[i] - min1; if (i + 1 < prices.length) { int min2 = prices[i + 1]; for (int j = i + 2; j < prices.length; j++) { min2 = Math.min(prices[j], min2); int b = prices[j] - min2; max = Math.max(max, a + b); } } } return max; } }