Skip to content

Instantly share code, notes, and snippets.

@rohanjai777
Created September 15, 2023 18:49
Show Gist options
  • Save rohanjai777/a34049d524d7ccab14dea84513be8265 to your computer and use it in GitHub Desktop.
Save rohanjai777/a34049d524d7ccab14dea84513be8265 to your computer and use it in GitHub Desktop.
class Solution {
public int[] productExceptSelf(int[] nums) {
int ans[] = new int[nums.length];
int prod = 1;
for(int i=0;i<nums.length;i++){ //get product from left to right without using the index element
ans[i] = prod;
prod *= nums[i];
}
int revprod = 1;
for(int i=nums.length-1;i>=0;i--){ //get product from right to left without using the index element
ans[i] *= revprod;
revprod *= nums[i];
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment