DEV Community

Cover image for Typescript Coding Chronicles: Product of Array Except Self
Zamora
Zamora

Posted on

Typescript Coding Chronicles: Product of Array Except Self

Problem Statement:

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.

Example 1:

  • Input: nums = [1,2,3,4]
  • Output: [24,12,8,6]

Example 2:

  • Input: nums = [-1,1,0,-3,3]
  • Output: [0,0,9,0,0]

Constraints:

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

Follow-up:

Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

Initial Thought Process:

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:

  1. Compute the prefix products for each element.
  2. Compute the suffix products for each element and multiply with the prefix products.

Basic Solution:

We can use two arrays to store the prefix and suffix products, then multiply them to get the final result.

Code:

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;
}
Enter fullscreen mode Exit fullscreen mode

Time Complexity Analysis:

  • Time Complexity: O(n), where n is the length of the array. We iterate through the array three times.
  • Space Complexity: O(n), for storing the prefix and suffix products.

Limitations:

The basic solution works well but uses extra space for storing prefix and suffix products.

Optimized Solution:

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.

Code:

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;
}
Enter fullscreen mode Exit fullscreen mode

Time Complexity Analysis:

  • Time Complexity: O(n), where n is the length of the array. We iterate through the array twice.
  • Space Complexity: O(1), as we are using the output array to store intermediate results and not using any additional space.

Improvements Over Basic Solution:

  • The optimized solution reduces space complexity to O(1) by using the output array for intermediate results.

Edge Cases and Testing:

Edge Cases:

  1. The array contains zero(s).
  2. The array contains negative numbers.
  3. The array length is the minimum (2) or maximum (10^5) limit.

Test Cases:

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]
Enter fullscreen mode Exit fullscreen mode

General Problem-Solving Strategies:

  1. Understand the Problem: Carefully read the problem statement to understand the requirements and constraints.
  2. Identify Key Operations: Determine the key operations needed, such as computing prefix and suffix products.
  3. Optimize for Readability: Use clear and concise logic to ensure the code is easy to follow.
  4. Test Thoroughly: Test the solution with various cases, including edge cases, to ensure correctness.

Identifying Similar Problems:

  1. Prefix Sum Array:

    • Problems where you need to compute prefix sums for range queries.
    • Example: Range Sum Query.
  2. In-Place Algorithms:

    • Problems where operations need to be performed in place with limited extra space.
    • Example: Rotate an array to the right by k steps.
  3. Array Manipulation:

    • Problems where you need to modify arrays based on specific conditions.
    • Example: Move zeros to the end of an array.

Conclusion:

  • The problem of computing the product of an array except self can be efficiently solved using both a basic approach with extra space and an optimized in-place approach.
  • Understanding the problem and breaking it down into manageable parts is crucial.
  • Using clear logic and optimizing for readability ensures the solution is easy to follow.
  • Testing with various edge cases ensures robustness.
  • Recognizing patterns in problems can help apply similar solutions to other challenges.

By practicing such problems and strategies, you can improve your problem-solving skills and be better prepared for various coding challenges.

Top comments (0)