The provided code implements a solution to the "Product of...

August 23, 2025 at 06:18 AM

class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { std::vector<int> res(nums.size(), 1); for (size_t i=1; i<nums.size(); ++i) { res[i] = res[i-1] * nums[i-1]; } int r = 1; for (int i=nums.size()-1; i > -1; --i) { res[i] *= r; r *= nums[i]; } return res; } };

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, where res[i] is the product of all elements in nums except nums[i].

Detailed Steps

  1. Initialize the Output Array (res):

    std::vector<int> res(nums.size(), 1);
    

    The res array is initialized with 1s and will eventually store the desired product values.

  2. 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 of nums[i].
    • After this loop:
      • res[i] contains the product of all numbers before nums[i].
  3. 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 after nums[i]).
      • Each res[i] (which already contains the prefix product) is multiplied by r, resulting in the product of all elements except nums[i].
  4. Return the Result:

    return res;
    

    The res array now contains the desired result.


Example Walkthrough

Input:

nums = [1, 2, 3, 4]

Execution:

  1. 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]
      
  2. 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]
      
  3. 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.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node