---
# System prepended metadata

title: 0238. Product of Array Except Self
tags: [Leetcode, FaceBook, Medium]

---

# 0238. Product of Array Except Self
###### tags: `Leetcode` `FaceBook` `Medium`
Link: https://leetcode.com/problems/product-of-array-except-self/
## Code
```java=
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] ans = new int[nums.length];
        Arrays.fill(ans,1);
        int leftRes = 1, rightRes = 1;
        for(int i = 0;i < nums.length-1;i++){
            leftRes*=nums[i];
            ans[i+1]*=leftRes;
        }
        for(int j = nums.length-1; j>0;j--){
            rightRes*=nums[j];
            ans[j-1]*=rightRes;
        }
        return ans;
    }
}
```