PIXELBANKv9.1.0
Menu

Product of Array Except Self

Given an array nums, return an array where each element is the product of all elements except itself. Do not use division.

Output space-separated.

Example:

Input:
1,2,3,4
Output:
24 12 8 6
Reasoning:
  • First, we calculate the total product of all elements: 1â‹…2â‹…3â‹…4=241 \cdot 2 \cdot 3 \cdot 4 = 24
  • Then, for each element, we find the product of all other elements by using the total product and the current element:
    • For the first element (1), the product is 2â‹…3â‹…4=242 \cdot 3 \cdot 4 = 24
    • For the second element (2), the product is 1â‹…3â‹…4=121 \cdot 3 \cdot 4 = 12
    • For the third element (3), the product is 1â‹…2â‹…4=81 \cdot 2 \cdot 4 = 8
    • For the fourth element (4), the product is 1â‹…2â‹…3=61 \cdot 2 \cdot 3 = 6
  • The final output is the array of these products: 24 12 8 6

Constraints:

  • 2 <= len(nums) <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix fits in a 32-bit integer
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.