238. Product of Array Except Self

 

Efficiently Solving "Product of Array Except Self" on LeetCode

Introduction

LeetCode is a popular platform for sharpening coding skills and tackling challenging problems. Today, I want to share an elegant solution to one such problem: "Product of Array Except Self." This problem requires us to compute the product of all elements in an array except for the one at the current index, and it needs to be done without using division and in linear time.

Problem Statement

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Intuition

To solve this problem efficiently, we can use two auxiliary arrays to keep track of the product of elements to the left and right of each index. By doing this, we can avoid using division and maintain an O(n) time complexity.

Approach

  1. Initialize Prefix and Postfix Arrays:

    • Create two arrays, prifixMulti and postfixMulti, both of size n and initialize all elements to 1.

  2. Calculate Prefix Products:

    • Iterate through the array from left to right.

    • For each element, store the product of all previous elements in the prifixMulti array.

  3. Calculate Postfix Products:

    • Iterate through the array from right to left.

    • For each element, store the product of all subsequent elements in the postfixMulti array.

  4. Calculate Result:

    • Iterate through the array.

    • Multiply the corresponding elements of prifixMulti and postfixMulti to get the product of all elements except the one at the current index.

Code

Here is the JavaScript code implementing this approach:

var productExceptSelf = function(nums) {
    let prifixMulti = Array(nums.length).fill(1);
    let postfixMulti = Array(nums.length).fill(1);
    for(let i = 1; i < nums.length; i++) {
        prifixMulti[i] = prifixMulti[i - 1] * nums[i - 1];
    }
    for(let i = nums.length - 2; i >= 0; i--){
        postfixMulti[i] = postfixMulti[i + 1] * nums[i + 1];
    }
    for(let i = 0; i < nums.length; i++) {
        nums[i] = prifixMulti[i] * postfixMulti[i];
    }
    return nums;
};
Complexity Analysis
  • Time Complexity: O(n)

    • Calculating prefix products takes O(n) time.

    • Calculating postfix products takes O(n) time.

    • Multiplying prefix and postfix products to get the result takes O(n) time.

  • Space Complexity: O(n)

    • The space required for the prifixMulti and postfixMulti arrays is O(n).

Conclusion

This solution efficiently solves the problem with linear time and space complexity, making it optimal for large input sizes. By using auxiliary arrays to store prefix and postfix products, we can avoid the pitfalls of division and maintain a clean, understandable solution.

For more details and to see the solution in action, check out my LeetCode submission: Product of Array Except Self - LeetCode.

Happy coding! If you have any questions or suggestions, feel free to leave a comment below. 😊

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

LeetCode Problem: Implement pow(x, n) leetcode:- 50