public int[] productExceptSelf(int[] nums){
  int n=nums.length;
  int left=1;
  int right=1;
  int[] res=new int[n];
  Arrays.fill(res,1);
  for (int i=0; i < nums.length; i++) {
    res[i]*=left;
    left*=nums[i];
    res[n - 1 - i]*=right;
    right*=nums[n - 1 - i];
  }
  return res;
}

public int [ ] productExceptSelf ( int [ ] nums ) { int n = nums . length ; int left = 1 ; int right = 1 ; int [ ] res = new int [ n ] ; Arrays . fill ( res , 1 ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { res [ i ] *= left ; left *= nums [ i ] ; res [ n - 1 - i ] *= right ; right *= nums [ n - 1 - i ] ; } return res ; }
the product is actually composed of two parts , the integers on the left , and integers on the right . for a naive o ( n ) time , o ( n ) space solution you can use two arrays to store products from the beginning and from the end . then multiply each position in the two arrays to generate result . if we want to reduce space usage to o ( 1 ) , we can just replace the two arrays with two integers .
