import java.lang.*; import java.util.*; class ProductExceptSelf { public static int[] product(int[] nums) { int[] left = new int[nums.length]; int[] right = new int[nums.length]; left[0] = 1; right[nums.length - 1] = 1; int s = 1; int e = nums.length - 2; while (s <= nums.length && e >= 0) { left[s] = left[s - 1] * nums[s - 1]; right[e] = right[e + 1] * nums[e + 1]; s++; e--; } int[] res = new int[nums.length]; for (int i = 0; i < nums.length; i++) res[i] = left[i] * right[i]; return res; } public static void main(String[] args) { int[] nums = {-1, 1, 0, -3, 3}; System.out.println(Arrays.toString(product(nums))); } }