import java.lang.*; import java.util.*; class Stock { public static int maxProfit(int[] prices) { int s = 0; int e = prices.length - 1; int low = Integer.MAX_VALUE; int high = 0; while (s < e) { if (prices[s] < low) low = prices[s]; if (prices[e] > high) high = prices[e]; s++; e--; } return high - low > 0 ? high - low : 0; } public static void main(String[] args) { int[] prices = {7, 1, 5, 3, 6, 4}; System.out.println(maxProfit(prices)); } }