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].

Function Signature:

from typing import List

def product_except_self(nums: List[int]) -> List[int]:
    """
    Return an array where each element is the product of all the elements in the input array except itself.

    Parameters:
    - nums: List of integers.

    Returns:
    - List[int]: Array of products.
    """
    # Your code here

Example:

# Input: nums = [1, 2, 3, 4]
# Output: [24, 12, 8, 6]
# Explanation: The output array is [2*3*4, 1*3*4, 1*2*4, 1*2*3].

# Input: nums = [-1, 1, 0, -3, 3]
# Output: [0, 0, 9, 0, 0]
# Explanation: The output array is [1*1*0*3*3, -1*-1*0*3*3, -1*1*0*-3*3, -1*1*0*3*-3, -1*1*0*3*3].

# Input: nums = [0, 0]
# Output: [0, 0]
# Explanation: The output array is simply [0, 0]. 
# As there are only two elements, no multiplication is needed.

# Input: nums = [10, 0]
# Output: [0, 10]
# Explanation: The output array is simply [0, 10], 
# As there are only two elements, no multiplication is needed.

Note:

  • Please solve it without division and in O(n) time complexity.

Instructions:

  • Write the product_except_self function to solve the problem.
  • Implement your solution in Python.
  • Provide clear comments in your code.
  • Discuss the time and space complexity of your solution.

As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊