Dashboard
LC #238

Product of Array Except Self

Pattern · ?AmazonMetaGoogleAppleMicrosoftLinkedIn

Problem

Given integer array nums, return an array answer where answer[i] is the product of all elements of nums except nums[i]. You must solve it in O(n) without using division.

Constraints

  • 2 ≤ nums.length ≤ 10⁵
  • -30 ≤ nums[i] ≤ 30
  • No division allowed. O(n) time, O(1) extra space

Example

Input
nums = [1, 2, 3, 4]
Output
[24, 12, 8, 6]
Why
output[0]=2×3×4=24, output[1]=1×3×4=12, ...

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗