Modular Product Exclude Current Element
Arrays & HashingAuto-graded · 20 test casesLast updated
Compute the modular product of all elements in an array except the current element for each index without division.
Given an array of integers nums and an integer modulus mod, return an array result of equal length where each element result[i] is the product of all elements in nums except nums[i], evaluated modulo mod.
You must compute this without using division operations.
Constraints
2 <= len(nums) <= 10^51 <= nums[i] <= 10^91 <= mod <= 10^9- Time Complexity: $\mathcal{O}(n)$, where $n$ is the length of
nums. - Auxiliary Space Complexity: $\mathcal{O}(1)$ excluding the space required for the output array.
Examples
Example 1
Input: product_except_self([1, 2, 3, 4], 1000)
Output: [24, 12, 8, 6]
Example 2
Input: product_except_self([5, 2, 0], 100)
Output: [0, 0, 10]
Hints
Hint 1
Think about how to split the product at index i into two parts: elements to the left of i and elements to the right of i.
Hint 2
Can you compute the running product of elements from left to right, storing intermediate values in the result array?
Hint 3
How can you combine a second pass from right to left using a single running product variable?
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To compute the product of all array entries except the element at index $i$, we can observe that the target product decomposes into two separate components:
- The prefix product: the product of all elements strictly to the left of index $i$ ($\prod_{j=0}^{i-1} \text{nums}[j]$).
- The suffix product: the product of all elements strictly to the right of index $i$ ($\prod_{j=i+1}^{n-1} \text{nums}[j]$).
Since division is disallowed and modular division requires modular inverses (which may not exist for general $mod$), we cannot simply take the product of the whole array and divide by nums[i].
Instead, we perform two linear passes over the array:
-
Left-to-Right Pass (Prefixes): We iterate through the array from left to right. We use a variable
prefix(initialized to1) to keep track of the running product of elements seen so far. At index $i$, we setresult[i] = prefix, then updateprefix = (prefix * nums[i]) % mod. -
Right-to-Left Pass (Suffixes): We iterate backward from right to left. We maintain a variable
suffix(initialized to1) tracking the product of elements to the right. At index $i$, we updateresult[i] = (result[i] * suffix) % modand updatesuffix = (suffix * nums[i]) % mod.
Applying % mod after every multiplication keeps intermediate numbers small and prevents overflow or performance degradation.
Complexity
- Time Complexity: $\mathcal{O}(n)$ because we perform two simple sweeps over the array of size $n$.
- Auxiliary Space Complexity: $\mathcal{O}(1)$ extra space beyond the array created for the return value, using only two integer scalars for prefix and suffix accumulators.
Python Reference Solution
from typing import List
def product_except_self(nums: List[int], mod: int) -> List[int]:
n = len(nums)
result = [1] * n
# First pass: accumulate prefix products
prefix = 1
for i in range(n):
result[i] = prefix
prefix = (prefix * nums[i]) % mod
# Second pass: accumulate suffix products into result
suffix = 1
for i in range(n - 1, -1, -1):
result[i] = (result[i] * suffix) % mod
suffix = (suffix * nums[i]) % mod
return result