a prefix sum problem - store prefix sum for each index and then check for left i and right n-i elements difference.
public static void main(String[] args){
MinNetPrice minNetPrice = new MinNetPrice();
System.out.println(minNetPrice.minNetPrice(new int[]{1,3,2,3}));
}
public int minNetPrice(int[] stockPrice){
int sum = 0;
for(int i : stockPrice){
sum+=i;
}
int minNetPrice = Integer.MAX_VALUE, month=-1, n = stockPrice.length;
int leftSum=0,rightSum = sum;
for(int i=1;i<n;i++){
leftSum+=stockPrice[i-1];
rightSum-=stockPrice[i-1];
int currNet = Math.abs((leftSum/i)-(rightSum/(n-i)));
if(minNetPrice>currNet){
minNetPrice = currNet;
month = i;
if(minNetPrice==0) break;
}
}
return month;
}