package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/product-of-array-except-self/
 */
public class Problem238 {
   public int[] productExceptSelf(int[] nums) {
       int totalWithoutZero = 1;
       int total = 1;
       int nZero = 0;
       for (int num : nums) {
           if (num != 0) {
               totalWithoutZero *= num;
           } else {
               nZero++;
           }
           total *= num;
       }
       int[] output = new int[nums.length];
       for (int i = 0; i < nums.length; i++) {
           if (nZero > 1) {
               output[i] = 0;
           } else if (nZero == 1) {
               if (nums[i] == 0) {
                   output[i] = totalWithoutZero;
               } else {
                   output[i] = 0;
               }
           } else {
               output[i] = total / nums[i];
           }
       }
       return output;
   }
   
   public static void main(String[] args) {
       Problem238 prob = new Problem238();
       System.out.println(Arrays.toString(prob.productExceptSelf(new int[]{1, 2, 3, 4}))); // [24, 12, 8, 6]
       System.out.println(Arrays.toString(prob.productExceptSelf(new int[]{0, 0}))); // [0, 0]
       System.out.println(Arrays.toString(prob.productExceptSelf(new int[]{0, 2, 3}))); // [6, 0, 0]
   }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -161,273 +161,8 @@
 ) %7B%0A
-       int totalWithoutZero = 1;%0A       int total = 1;%0A       int nZero = 0;%0A       for (int num : nums) %7B%0A           if (num != 0) %7B%0A               totalWithoutZero *= num;%0A           %7D else %7B%0A               nZero++;%0A           %7D%0A           total *= num;%0A       %7D%0A
     
@@ -201,16 +201,52 @@
 ength%5D;%0A
+       int x = 1;%0A       // forward%0A
        f
@@ -299,29 +299,22 @@
     
-if (nZero %3E 1) %7B%0A    
+x *= nums%5Bi%5D;%0A
     
@@ -328,33 +328,33 @@
 output%5Bi%5D = 
-0
+x
 ;%0A      
      %7D else 
@@ -345,45 +345,32 @@
     
+ %7D%0A
      
-%7D else if (nZero
+  x
  =
-=
  1
-) %7B
+;
 %0A       
     
@@ -369,106 +369,98 @@
     
-        if (nums%5Bi%5D == 0) %7B%0A                   output%5Bi%5D = totalWithoutZero;%0A               %7D else
+int backward = 1;%0A       for (int i = nums.length-1; i %3E= 0; i--) %7B%0A           if (i != 0)
  %7B%0A 
@@ -465,35 +465,35 @@
 %0A               
-   
+x =
  output%5Bi%5D = 0;%0A
@@ -489,13 +489,22 @@
 ut%5Bi
-%5D = 0
+-1%5D * backward
 ;%0A  
@@ -520,518 +520,147 @@
     
-%7D%0A           %7D else %7B%0A               output%5Bi%5D = total / nums%5Bi%5D;%0A           %7D%0A       %7D%0A       return output;%0A
+backward *= nums%5Bi%5D;%0A           %7D else %7B%0A        
    
-%7D%0A
    
-%0A   public static void main(String%5B%5D args) %7B%0A       Problem238 prob = new Problem238();%0A       System.out.println(Arrays.toString(prob.productExceptSelf(new int%5B%5D%7B1, 2, 3, 4%7D))); // %5B24, 12, 8, 6%5D%0A       System.out.println(Arrays.toString(prob.productExceptSelf(new int%5B%5D%7B0, 0%7D))); // %5B0, 0%5D%0A       System.out.println(Arrays.toString(prob.productExceptSelf(new int%5B%5D%7B0, 2, 3%7D))); // %5B6, 0, 0%5D
+ x = backward;%0A           %7D%0A           output%5Bi%5D = x;%0A       %7D%0A       return output;
 %0A   

Solve problem 238 without using division