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].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:
Input: nums = [1,2,3,4] Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]
Constraints:
2 <= nums.length <= 105-30 <= nums[i] <= 30answer[i] is guaranteed to fit in a 32-bit integer.Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a row of items, and for every single item, you need to calculate the combined value of all the other items around it combined together. The brute force approach achieves this by looking at every item one by one and manually multiplying all the other items from scratch.
Here's how the algorithm would work step-by-step:
def product_except_self_brute_force(input_numbers):
# We need a container to store the calculated product for each item
result_collection = []
# Iterate through every single item to compute its exclusive product
for target_index in range(len(input_numbers)):
current_product = 1
# Gather and multiply all other items except the current target
for other_index in range(len(input_numbers)):
if target_index != other_index:
current_product *= input_numbers[other_index]
# Append the final combined product for the current item
result_collection.append(current_product)
return result_collectionTo find the product of all items except the current one without using division, we can break the task into two passes. The strategy involves looking at everything to the left and everything to the right of each item, then combining those two perspectives.
Here's how the algorithm would work step-by-step:
def product_except_self(input_numbers):
array_length = len(input_numbers)
output_result = [1] * array_length
# We accumulate left products first so each position knows what precedes it.
running_left_product = 1
for index_value in range(array_length):
output_result[index_value] = running_left_product
running_left_product *= input_numbers[index_value]
# Multiplying by right products combines both sides without using division.
running_right_product = 1
for index_value in range(array_length - 1, -1, -1):
output_result[index_value] *= running_right_product
running_right_product *= input_numbers[index_value]
# The final array now contains the complete product of all elements except self.
return output_result| Case | How to Handle |
|---|---|
| Array containing a single zero | The prefix and suffix multiplication passes naturally compute the correct product of one for that single element. |
| Array containing multiple zeros | Once there are two or more zeros in the array, all products in the resulting array will correctly evaluate to zero. |
| Array containing a single negative number or all negative numbers | The multiplication logic accumulates signs correctly through the prefix and suffix passes to yield the proper positive or negative products. |
| Minimum valid input array of size two | The prefix pass will be empty for the first element and the suffix pass will be empty for the second, resulting in the correct swapped pair. |
| Maximum constraint array size up to one hundred thousand elements | The linear time O(n) approach ensures the solution executes well within standard interview time limits without hitting timeout errors. |
| Large integers causing integer overflow during multiplication | Since constraints typically guarantee fits within standard integer ranges or the problem implies standard 32-bit integers, standard multiplication is assumed sufficient unless language-specific wrapping occurs. |
| Array with all identical non-zero values | The prefix and suffix products calculate powers of that value, resulting in each position holding the value raised to the power of n minus one. |
| Array containing a single zero at the beginning or end | The zero will propagate its effect to all other indices while the zero index itself receives the full product of all non-zero elements. |