O12

Product of Array Except Self

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: ARRAY

Problem Statement & Objective

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]. You must write an algorithm that runs in O(n) time and WITHOUT using the division operation.

Function Signature (Python)

product_except_self(nums: list[int]) -> list[int]

Input & Output Contract

Output Rule:Array of products of all other elements.

System Constraints

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

Example Execution Walkthrough

nums = [1, 2, 3, 4]
result: [24, 12, 8, 6]

Example Test Scenarios (3)

Example 1
Input:[[1,2,3,4]]
Expected Output:[24,12,8,6]
Example 2: Tek Sıfır
Input:[[-1,1,0,-3,3]]
Expected Output:[0,0,9,0,0]
Example 3: İki Eleman
Input:[[2,3]]
Expected Output:[3,2]
Loading Python Editor...
Input Parameters
[
  [
    1,
    2,
    3,
    4
  ]
]
Expected Output
[
  24,
  12,
  8,
  6
]