Created
September 15, 2023 18:49
-
-
Save rohanjai777/a34049d524d7ccab14dea84513be8265 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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