package com.fishercoder.solutions;

/**
 * Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

 Solve it without division and in O(n).

 For example, given [1,2,3,4], return [24,12,8,6].

 Follow up:
 Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
 */
public class _238 {

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        result[0] = 1;
        for (int i = 1; i < n; i++) {
            result[i] = result[i-1]*nums[i-1];
        }
        int right = 1;
        for (int i = n-1; i >= 0; i--) {
            result[i] *= right;
            right *= nums[i];
        }
        return result;
    }
}

@@ -33,16 +33,56 @@
 s;%0A%0A/**%0A
+ * 238. Product of Array Except Self%0A *%0A
  * Given
@@ -123,16 +123,19 @@
 1, nums,
+%0A *
  return 
@@ -238,17 +238,18 @@
 ums%5Bi%5D.%0A
-%0A
+ *
  Solve i
@@ -280,17 +280,18 @@
 n O(n).%0A
-%0A
+ *
  For exa
@@ -397,16 +397,17 @@
 plexity?
+%0A
  (Note: 
@@ -504,16 +504,17 @@
 s.)%0A */%0A
+%0A
 public c
@@ -526,16 +526,384 @@
 _238 %7B%0A%0A
+    /**Very straightforward idea: iterate through the array twice:%0A     * first time: get res%5Bi%5D = res%5Bi-1%5D*nums%5Bi-1%5D%0A     * second time: have a variable called right, which means all the numbers product to its right, then do%0A     * res%5Bi%5D *= right;%0A     * right *= nums%5Bi%5D;%0A     * that's it.%0A%0A     * This could be very well illustrated with this example: %5B1,2,3,4%5D*/%0A
     publ
@@ -1100,27 +1100,33 @@
 result%5Bi
--1%5D*
+ - 1%5D * 
 nums%5Bi
--
+ - 
 1%5D;%0A    
@@ -1176,17 +1176,19 @@
 nt i = n
--
+ - 
 1; i %3E= 

edit 238