Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

7. Product of Array Except Self

MediumDay 2 of 60Arrays & Hashing

Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i]. You must not use division, and the solution should run in O(n) time.

Example 1
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
For index 0 the product is 2 * 3 * 4 = 24, for index 1 it is 1 * 3 * 4 = 12, and so on.
Example 2
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Constraints
  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • Every prefix and suffix product fits in a 32-bit integer
Hints
Frequently asked at: Amazon, Meta, Microsoft, Apple, Atlassian
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min