|
| 1 | +# Product of Array Except Self |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/product-of-array-except-self/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. |
| 8 | + |
| 9 | +The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. |
| 10 | + |
| 11 | +You must write an algorithm that runs in O(n) time and without using the division operation. |
| 12 | + |
| 13 | +### Constraints |
| 14 | + |
| 15 | +- 2 <= nums.length <= 105 |
| 16 | +- -30 <= nums[i] <= 30 |
| 17 | +- The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +``` |
| 22 | +Input: nums = [1,2,3,4] |
| 23 | +Output: [24,12,8,6] |
| 24 | +``` |
| 25 | + |
| 26 | +``` |
| 27 | +Input: nums = [-1,1,0,-3,3] |
| 28 | +Output: [0,0,9,0,0] |
| 29 | +``` |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +- Calculate the total product and store in variable |
| 34 | +- Iterate thru array a second time and subtrack product of current element |
| 35 | +- Can I store product left and right? |
| 36 | +- Will flipping or doing something in reverse work? |
| 37 | + |
| 38 | +### Initial Solution |
| 39 | + |
| 40 | +This solution has a time and space complexity of O(n). |
| 41 | + |
| 42 | +```javascript |
| 43 | +const productExceptSelf = function (nums) { |
| 44 | + // Calculate left and right products |
| 45 | + const left = []; |
| 46 | + for (let i = 0; i < nums.length; i++) { |
| 47 | + if (i > 0) { |
| 48 | + const product = nums[i - 1] * left[left.length - 1]; |
| 49 | + left.push(product); |
| 50 | + } else { |
| 51 | + left.push(1); |
| 52 | + } |
| 53 | + } |
| 54 | + const right = []; |
| 55 | + for (let i = nums.length - 1; i >= 0; i--) { |
| 56 | + if (i < nums.length - 1) { |
| 57 | + const product = nums[i + 1] * right[0]; |
| 58 | + right.unshift(product); |
| 59 | + } else { |
| 60 | + right.unshift(1); |
| 61 | + } |
| 62 | + } |
| 63 | + console.log(left, right); |
| 64 | + // Calculate result based on what is left and right of current number |
| 65 | + const answer = []; |
| 66 | + for (let i = 0; i < nums.length; i++) { |
| 67 | + answer.push(left[i] * right[i]); |
| 68 | + } |
| 69 | + return answer; |
| 70 | +}; |
| 71 | +``` |
| 72 | + |
| 73 | +### Optimized Solution |
| 74 | + |
| 75 | +This solution has O(n) time complexity and O(1) space complexity. You can see an explanation of this solution here: https://www.youtube.com/watch?v=bNvIQI2wAjk |
| 76 | + |
| 77 | +```javascript |
| 78 | +const productExceptSelf = function (nums) { |
| 79 | + // Create initial array and put initial value of 1 |
| 80 | + const output = Array(nums.length).fill(1); |
| 81 | + |
| 82 | + // Iterate thru nums and add prefixes to output |
| 83 | + let prefix = 1; |
| 84 | + for (let i = 0; i < nums.length; i++) { |
| 85 | + output[i] = prefix; |
| 86 | + prefix *= nums[i]; |
| 87 | + } |
| 88 | + |
| 89 | + // Iterate thru nums backwards and calculate total with pre and post fix. |
| 90 | + let postfix = 1; |
| 91 | + for (let i = nums.length - 1; i >= 0; i--) { |
| 92 | + output[i] *= postfix; |
| 93 | + postfix *= nums[i]; |
| 94 | + } |
| 95 | + |
| 96 | + return output; |
| 97 | +}; |
| 98 | +``` |
0 commit comments