The provided code implements a solution to the "Product of...
The provided code implements a solution to the "Product of Array Except Self" problem. Here’s a detailed explanation of what it does:
Problem Description
The problem is to return an array such that each element at index i
of the output array res
is the product of all elements of the input array nums
except the one at nums[i]
. The main constraint is that the solution must run in (O(n)) time complexity and must not use division.
Code Explanation
Input:
nums
: A vector of integers.
Output:
- A vector
res
, whereres[i]
is the product of all elements innums
exceptnums[i]
.
Detailed Steps
-
Initialize the Output Array (
res
):std::vector<int> res(nums.size(), 1);
The
res
array is initialized with1
s and will eventually store the desired product values. -
Build the Prefix Product:
for (size_t i = 1; i < nums.size(); ++i) { res[i] = res[i-1] * nums[i-1]; }
- This loop calculates the prefix product for each index and stores it in
res
. - The prefix product at index
i
is the product of all elements to the left ofnums[i]
. - After this loop:
res[i]
contains the product of all numbers beforenums[i]
.
- This loop calculates the prefix product for each index and stores it in
-
Build the Suffix Product and Combine:
int r = 1; // r keeps track of the suffix product for (int i = nums.size() - 1; i > -1; --i) { res[i] *= r; // Multiply current `res[i]` (prefix product) by the suffix product r *= nums[i]; // Update the suffix product }
- Here, the loop traverses the array from right to left:
r
accumulates the suffix product (product of all numbers afternums[i]
).- Each
res[i]
(which already contains the prefix product) is multiplied byr
, resulting in the product of all elements exceptnums[i]
.
- Here, the loop traverses the array from right to left:
-
Return the Result:
return res;
The
res
array now contains the desired result.
Example Walkthrough
Input:
nums = [1, 2, 3, 4]
Execution:
-
Prefix Product (First Loop):
- Start with
res = [1, 1, 1, 1]
. - Loop calculates:
i = 1 -> res[1] = res[0] * nums[0] = 1 * 1 = 1 => res = [1, 1, 1, 1] i = 2 -> res[2] = res[1] * nums[1] = 1 * 2 = 2 => res = [1, 1, 2, 1] i = 3 -> res[3] = res[2] * nums[2] = 2 * 3 = 6 => res = [1, 1, 2, 6]
- Start with
-
Suffix Product (Second Loop):
- Initialize
r = 1
. - Loop calculates:
i = 3 -> res[3] = res[3] * r = 6 * 1 = 6 r = r * nums[3] = 1 * 4 = 4 => res = [1, 1, 2, 6] i = 2 -> res[2] = res[2] * r = 2 * 4 = 8 r = r * nums[2] = 4 * 3 = 12 => res = [1, 1, 8, 6] i = 1 -> res[1] = res[1] * r = 1 * 12 = 12 r = r * nums[1] = 12 * 2 = 24 => res = [1, 12, 8, 6] i = 0 -> res[0] = res[0] * r = 1 * 24 = 24 r = r * nums[0] = 24 * 1 = 24 => res = [24, 12, 8, 6]
- Initialize
-
Final Output:
res = [24, 12, 8, 6]
Key Points
- Time Complexity: (O(n)) because we iterate over the array twice.
- Space Complexity: (O(1)) (excluding the input and output arrays), because we use only the result array and one auxiliary variable
r
.
Summary
The code calculates, for each index, the product of all elements in the array except the element at that index, without using division.