diff --git a/Sample.java b/Sample.java index f10cd8d1..f895824c 100644 --- a/Sample.java +++ b/Sample.java @@ -1,6 +1,33 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Three line explanation of solution in plain english +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Three line explanation of solution in plain english: +// We maintain a running product +// Frist do the left to right pass where we multiple numbers +// Then do a right to left pass -// Your code here along with comments explaining your approach \ No newline at end of file +// Your code here along with comments explaining your approach + +class Solution { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] result = new int[n]; + + int rp = 1; + result[0] = 1; + + for (int i = 1; i < n; i++) { + rp = rp * nums[i-1]; + result[i] = rp; + } + + rp = 1; + + for (int i = n-2; i >=0; i--) { + rp = rp * nums[i+1]; + result[i] = result[i] * rp; + } + + return result; + } +} \ No newline at end of file