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 = 0; i--) { suffixProducts[i] = suffixProducts[i 1] * nums[i 1]; } // Compute the result by multiplying prefix and suffix products for (let i = 0; iTime Complexity Analysis:
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 = 0; i--) { result[i] = result[i] * suffixProduct; suffixProduct *= nums[i]; } return result; }
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3