Given an array of numbers, return array of products of all other numbers no division
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Given an array of numbers, returning an array of products of all other numbers without using division is a classic problem that tests both algorithmic reasoning and array manipulation skills. Below, we will explore a comprehensive approach to solving this problem, along with technical insights and examples.
Problem Explanation
The problem requires constructing an output array such that each element at index i is the product of all the numbers in the input array except the one at i. Crucially, you must not use division in your solution.
Example
Consider the input array: [1, 2, 3, 4]. The task is to produce an output array [24, 12, 8, 6], where:
24is the product of2,3, and4.12is the product of1,3, and4.8is the product of1,2, and4.6is the product of1,2, and3.
Technical Approach
To solve this issue without using division, we can employ a method involving prefix and suffix products. This method efficiently calculates the desired products in two main passes through the array.
- Prefix Products: First, we compute an array that contains the product of all elements preceding the current element. This operation is done in a single left-to-right traversal.
- Suffix Products: Next, we compute an array for the products of all elements following the current element in a right-to-left traversal.
- Combine Products: Finally, the product of all other numbers for a given index is simply the product of the corresponding prefix and suffix products.
Algorithm Steps
- Initialize arrays
prefixandsuffixto 1, with the same length as the input array. - Populate the
prefixarray:- Start with
prefix[0] = 1. - Iterate through the array from
1ton-1, settingprefix[i] = prefix[i-1] * array[i-1].
- Populate the
suffixarray:- Start with
suffix[n-1] = 1. - Iterate from
n-2down to0, settingsuffix[i] = suffix[i+1] * array[i+1].
- The result at each index
iisprefix[i] * suffix[i].
Example Calculation
Let's walk through the calculation for arr = [1, 2, 3, 4]:
- Prefix Calculation:
prefix[0] = 1prefix[1] = prefix[0] * array[0] = 1 * 1 = 1prefix[2] = prefix[1] * array[1] = 1 * 2 = 2prefix[3] = prefix[2] * array[2] = 2 * 3 = 6ResultingprefixArray:[1, 1, 2, 6]
- Suffix Calculation:
suffix[3] = 1suffix[2] = suffix[3] * array[3] = 1 * 4 = 4suffix[1] = suffix[2] * array[2] = 4 * 3 = 12suffix[0] = suffix[1] * array[1] = 12 * 2 = 24ResultingsuffixArray:[24, 12, 4, 1]
- Final Product Calculation:
output[0] = prefix[0] * suffix[0] = 1 * 24 = 24output[1] = prefix[1] * suffix[1] = 1 * 12 = 12output[2] = prefix[2] * suffix[2] = 2 * 4 = 8output[3] = prefix[3] * suffix[3] = 6 * 1 = 6Resulting Output Array:[24, 12, 8, 6]
Complexity Analysis
- Time Complexity: The algorithm runs in time, where
nis the length of the input array. This is because each of the three major steps (building prefix, building suffix, computing the final result) involves a single pass through the array. - Space Complexity: The algorithm uses additional space for the
prefixandsuffixarrays. The space for the output array does not count towards extra space because it is returned as part of the solution.
Key Points Summary
| Step | Description |
| 1 | Calculate prefix products in a left-to-right pass. |
| 2 | Calculate suffix products in a right-to-left pass. |
| 3 | Combine prefix and suffix products to get the result. |
Considerations and Alternative Approaches
- Edge Cases: Consider handling arrays containing one element or zeros, which might require additional checks.
- Improving Space Complexity: You can optimize space usage to by directly modifying the output array for prefix values, then iterating backward for suffix computations.
This approach ensures that we efficiently compute the required products without any reliance on division, making the solution applicable to scenarios where division is not suitable due to potential division by zero concerns or when optimizing for integer arithmetic.

