Go from left to right and calculate max profit for each index (i). Go from right to left and calculate max profit for (i). Add max right profit for (i) and max left profit for (i-1) and check if it's max profit.

```
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) return 0;
int lenght = prices.length;
int[] leftProfit = new int[lenght];
int leftMaxProfit = 0;
int leftMin = prices[0];
for (int i=0; i<lenght; i++) {
if (prices[i] < leftMin) leftMin = prices[i];
if (prices[i] - leftMin > leftMaxProfit) leftMaxProfit = prices[i]-leftMin;
leftProfit[i] = leftMaxProfit;
}
int maxProfit = 0;
int rightMaxProfit = 0;
int rightMax = prices[lenght-1];
for (int i=lenght-1; i>=0; i--) {
if (prices[i] > rightMax) rightMax = prices[i];
if (rightMax - prices[i] > rightMaxProfit) rightMaxProfit = rightMax - prices[i];
int currentProfit = rightMaxProfit + (i>0 ? leftProfit[i-1] : 0);
if (currentProfit > maxProfit) {
maxProfit = currentProfit;
}
}
return maxProfit;
}
```