|
| 1 | +# Product of Array Except Self (Medium) |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | + |
| 5 | +- [Problem Statement](#problem-statement) |
| 6 | +- [Examples](#examples) |
| 7 | +- [Constraints](#constraints) |
| 8 | +- [Solutions](#solutions) |
| 9 | + - [Optimal Solution](#optimal-solution) |
| 10 | + - [Alternative Solution](#alternative-solution) |
| 11 | +- [Code Explanation](#code-explanation) |
| 12 | +- [Complexity Analysis](#complexity-analysis) |
| 13 | +- [Additional Resources](#additional-resources) |
| 14 | + |
| 15 | +## Problem Statement |
| 16 | + |
| 17 | +[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/) |
| 18 | + |
| 19 | +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]`. |
| 20 | + |
| 21 | +The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer. |
| 22 | + |
| 23 | +You must write an algorithm that runs in O(n) time and without using the division operation. |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +### Example 1: |
| 28 | + |
| 29 | +``` |
| 30 | +Input: nums = [1,2,3,4] |
| 31 | +Output: [24,12,8,6] |
| 32 | +``` |
| 33 | + |
| 34 | +### Example 2: |
| 35 | + |
| 36 | +``` |
| 37 | +Input: nums = [-1,1,0,-3,3] |
| 38 | +Output: [0,0,9,0,0] |
| 39 | +``` |
| 40 | + |
| 41 | +## Constraints |
| 42 | + |
| 43 | +- 2 <= nums.length <= 10^5 |
| 44 | +- -30 <= nums[i] <= 30 |
| 45 | +- The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. |
| 46 | + |
| 47 | +## Solutions |
| 48 | + |
| 49 | +### Optimal Solution |
| 50 | + |
| 51 | +```python |
| 52 | +class Solution(object): |
| 53 | + def productExceptSelf(self, nums): |
| 54 | + """ |
| 55 | + :type nums: List[int] |
| 56 | + :rtype: List[int] |
| 57 | + """ |
| 58 | + answers = [] |
| 59 | + leftptr = 1 |
| 60 | + for i in range(len(nums)): |
| 61 | + answers.append(leftptr) |
| 62 | + leftptr *= nums[i] |
| 63 | + |
| 64 | + rightptr = 1 |
| 65 | + for i in range(len(nums) - 1, -1, -1): |
| 66 | + answers[i] *= rightptr |
| 67 | + rightptr *= nums[i] |
| 68 | + return answers |
| 69 | +``` |
| 70 | + |
| 71 | +### Alternative Solution |
| 72 | + |
| 73 | +```python |
| 74 | +class Solution(object): |
| 75 | + def productExceptSelf(self, nums): |
| 76 | + """ |
| 77 | + :type nums: List[int] |
| 78 | + :rtype: List[int] |
| 79 | + """ |
| 80 | + bef = [1] * len(nums) |
| 81 | + aft = [1] * len(nums) |
| 82 | + for i in range(1, len(nums)): |
| 83 | + bef[i] = bef[i - 1] * nums[i - 1] |
| 84 | + |
| 85 | + for i in range(len(nums) - 2, -1, -1): |
| 86 | + aft[i] = aft[i + 1] * nums[i + 1] |
| 87 | + return [bef[i] * aft[i] for i in range(len(nums))] |
| 88 | +``` |
| 89 | + |
| 90 | +## Code Explanation |
| 91 | + |
| 92 | +### Optimal Solution |
| 93 | + |
| 94 | +The optimal solution uses a two-pass approach to calculate the product of all elements except self: |
| 95 | + |
| 96 | +1. First pass (left to right): |
| 97 | + - Initialize an `answers` list to store the results. |
| 98 | + - Use `leftptr` to keep track of the cumulative product from the left. |
| 99 | + - For each element, append the current `leftptr` to `answers` and update `leftptr` by multiplying it with the current element. |
| 100 | + |
| 101 | +2. Second pass (right to left): |
| 102 | + - Use `rightptr` to keep track of the cumulative product from the right. |
| 103 | + - Iterate from right to left, multiplying each element in `answers` by `rightptr` and updating `rightptr`. |
| 104 | + |
| 105 | +This approach calculates the product of all elements to the left and right of each element without using division. |
| 106 | + |
| 107 | +### Alternative Solution |
| 108 | + |
| 109 | +The alternative solution uses two separate arrays to store the products: |
| 110 | + |
| 111 | +1. `bef` array: Stores the product of all elements to the left of each element. |
| 112 | +2. `aft` array: Stores the product of all elements to the right of each element. |
| 113 | + |
| 114 | +The final result is obtained by multiplying corresponding elements from `bef` and `aft`. |
| 115 | + |
| 116 | +## Complexity Analysis |
| 117 | + |
| 118 | +For the optimal solution: |
| 119 | +- Time Complexity: O(N), where N is the length of the input array. |
| 120 | +- Space Complexity: O(1), excluding the output array. |
| 121 | + |
| 122 | +## Additional Resources |
| 123 | + |
| 124 | +- [YouTube Explanation](https://youtu.be/yKZFurr4GQA?si=-wykJZfdRSw7M8UN) |
| 125 | +- [GitHub Solution](https://github.com/gahogg/Leetcode-Solutions/tree/main/Product%20of%20Array%20Except%20Self%20-%20Leetcode%20238) |
| 126 | +- [LeetCode Solution Explanation](https://leetcode.com/problems/product-of-array-except-self/solutions/5757573/solution) |
| 127 | +- [Personal Submission Details](https://leetcode.com/submissions/detail/1383555855/) |
| 128 | + |
| 129 | +> [!NOTE] |
| 130 | +> This problem is part of a larger collection following the roadmap on [algomap.io](https://algomap.io/). For more details and related problems, please refer to the AlgoMap website. |
0 commit comments