LeetCode – Product of Array Except Self (Java)

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].

Java Solution 1

public int[] productExceptSelf(int[] nums) {
    int[] result = new int[nums.length];
    int[] t1 = new int[nums.length];
    int[] t2 = new int[nums.length];
    //scan from left to right
    for(int i=0; i<nums.length-1; i++){
        t1[i+1] = nums[i] * t1[i];
    //scan from right to left
    for(int i=nums.length-1; i>0; i--){
        t2[i-1] = t2[i] * nums[i];
    for(int i=0; i<nums.length; i++){
        result[i] = t1[i] * t2[i];
    return result;

Java Solution 2

We can directly put the product values into the final result array. This saves the extra space to store the 2 intermediate arrays in Solution 1.

public int[] productExceptSelf(int[] nums) {
    int[] result = new int[nums.length];
    for(int i=nums.length-2; i>=0; i--){
    int left=1;
    for(int i=0; i<nums.length; i++){
        left = left*nums[i];
    return result;
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  • class Solution {
    vector productExceptSelf(vector& nums) {
    vector ans(nums.size(), 1);
    /* ans[i] will be the prefix multiplication nums[0] * nums[1] * .... * nums[i - 1] */
    for(int i = 0; i 0 ? ans[i - 1] * nums[i - 1] : 1);
    /* suffixMul will be the suffix multiplication at any index i */
    int suffixMul = 1;
    for(int i = nums.size() - 1; i >= 0; i--) {
    ans[i] *= suffixMul;
    suffixMul *= nums[i];
    return ans;

  • Blue Mormon

    (then God speaking).. Practice makes you mortals perfect!!

  • sd karthik

    How the fuck in this world will i know that i have to do this. How can anyone even think of this intuitively. Who ever is are GODS.

  • Hooman

    still you are creating `n` new objects: int[] result = new int[nums.length];

  • Rishabh Jain

    Space required is the input and output space. In this case that is the result and the nums array. Unlike the first solution where we were taking O(n) space for right and left arrays, we are declaring only constants in the second solution.

  • Jangku

    Why space O(1) for 2nd solution. It should be O(n) for result


    Second solution is wrong. The second loop in the second program should start from i=1 and not i=0. result[0] is already determined.

  • Shreyas Hirday

    it says to do it without division

  • srj_michael

    Simple solution without using extra space

    import java.util.*;
    import java.io.*;
    public class Product_of_Array_except_Self {
    public static void main(String args[])throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pw = new PrintWriter(System.out, true);
    int arr[]=new int[5];
    pw.println(“Enter 5 element of array”);
    for(int i=0;i<5;i++)
    int product=1;
    for(int i=0;i<5;i++)

    for(int i=0;i<5;i++)
    int temp=arr[i];