Sign In
You are coding as a Guest. Sign in with your RoleNest account to permanently track your streak, earn XP, and climb the Campus Leaderboard!
Sign In with RoleNest
🔥Product of Array Except SelfMedium
MediumArrays & Hashing•Acceptance: 65.4%

Product of Array Except Self

Real-World Engineering Context
Critical in portfolio risk analytics and stock volatility recalculation where removing one asset's variance must be calculated without recomputing full matrix products.
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 algorithm must run in O(n) time without using the division operation.

Sample Test Cases

Input: [[1,2,3,4]]
Expected: [24,12,8,6]
Input: [[-1,1,0,-3,3]]
Expected: [0,0,9,0]
Input: [[2,3]]
Expected: [3,2]

Constraints

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums fits in a 32-bit integer.