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].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
To solve this problem, we need to compute the product of all elements except the current element without using the division operation. This can be done by using two passes over the array:
We can use two arrays to store the prefix and suffix products, then multiply them to get the final result.
function productExceptSelf(nums: number[]): number[] { const n = nums.length; const prefixProducts = new Array(n).fill(1); const suffixProducts = new Array(n).fill(1); const result = new Array(n).fill(1); // Compute prefix products for (let i = 1; i < n; i++) { prefixProducts[i] = prefixProducts[i - 1] * nums[i - 1]; } // Compute suffix products for (let i = n - 2; i >= 0; i--) { suffixProducts[i] = suffixProducts[i + 1] * nums[i + 1]; } // Compute the result by multiplying prefix and suffix products for (let i = 0; i < n; i++) { result[i] = prefixProducts[i] * suffixProducts[i]; } return result; }
The basic solution works well but uses extra space for storing prefix and suffix products.
We can optimize the solution to use O(1) extra space by using the output array to store prefix products first and then modify it in-place to include the suffix products.
function productExceptSelfOptimized(nums: number[]): number[] { const n = nums.length; const result = new Array(n).fill(1); // Compute prefix products in the result array for (let i = 1; i < n; i++) { result[i] = result[i - 1] * nums[i - 1]; } // Compute suffix products and multiply with the prefix products let suffixProduct = 1; for (let i = n - 1; i >= 0; i--) { result[i] = result[i] * suffixProduct; suffixProduct *= nums[i]; } return result; } <h3> Time Complexity Analysis: </h3> <ul> <li> <strong>Time Complexity:</strong> O(n), where n is the length of the array. We iterate through the array twice.</li> <li> <strong>Space Complexity:</strong> O(1), as we are using the output array to store intermediate results and not using any additional space.</li> </ul> <h3> Improvements Over Basic Solution: </h3> <ul> <li>The optimized solution reduces space complexity to O(1) by using the output array for intermediate results.</li> </ul> <h2> Edge Cases and Testing: </h2> <h3> Edge Cases: </h3> <ol> <li>The array contains zero(s).</li> <li>The array contains negative numbers.</li> <li>The array length is the minimum (2) or maximum (10^5) limit.</li> </ol> <h3> Test Cases: </h3> <pre class="brush:php;toolbar:false">console.log(productExceptSelf([1,2,3,4])); // [24,12,8,6] console.log(productExceptSelf([-1,1,0,-3,3])); // [0,0,9,0,0] console.log(productExceptSelf([2,2,2,2])); // [8,8,8,8] console.log(productExceptSelf([0,0])); // [0,0] console.log(productExceptSelf([5])); // This should not be a valid input as the minimum length is 2 console.log(productExceptSelf([1,2])); // [2, 1] console.log(productExceptSelfOptimized([1,2,3,4])); // [24,12,8,6] console.log(productExceptSelfOptimized([-1,1,0,-3,3])); // [0,0,9,0,0] console.log(productExceptSelfOptimized([2,2,2,2])); // [8,8,8,8] console.log(productExceptSelfOptimized([0,0])); // [0,0] console.log(productExceptSelfOptimized([5])); // This should not be a valid input as the minimum length is 2 console.log(productExceptSelfOptimized([1,2])); // [2, 1]
Prefix Sum Array:
In-Place Algorithms:
Array Manipulation:
By practicing such problems and strategies, you can improve your problem-solving skills and be better prepared for various coding challenges.
The above is the detailed content of Typescript Coding Chronicles: Product of Array Except Self. For more information, please follow other related articles on the PHP Chinese website!