bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 238. Product of Array Except Self

238. Product of Array Except Self

Leetcode Solutions

Approach: Left and Right product lists

  1. Initialize two arrays L and R of the same length as the input array nums.
  2. Set L[0] to 1 because there are no elements to the left of the first element.
  3. Populate the L array by setting L[i] = L[i - 1] * nums[i - 1] for i from 1 to n - 1.
  4. Set R[n - 1] to 1 because there are no elements to the right of the last element.
  5. Populate the R array by setting R[i] = R[i + 1] * nums[i + 1] for i from n - 2 down to 0.
  6. Initialize the answer array of the same length as the input array nums.
  7. Populate the answer array by setting answer[i] = L[i] * R[i] for each i.
  8. Return the answer array.
UML Thumbnail

Approach: O() space approach (Optimized Space)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...