Problem · Array

Product of Array Except Self

Learn this problem
MediumSquare Point logoSquare PointFULLTIMEONSITE INTERVIEW

Problem statement

Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i].

Build the result without using division and in linear time.

Function

productExceptSelf(nums: int[]) → int[]

Examples

Example 1

nums = [1,2,3,4]return = [24,12,8,6]

Each position contains the product of all values except the one at that position.

Example 2

nums = [-1,1,0,-3,3]return = [0,0,9,0,0]

Only the index containing 0 receives the product of all nonzero values.

Constraints

  • 2 <= nums.length <= 100000
  • -30 <= nums[i] <= 30
  • Every prefix product and suffix product fits in a signed 32-bit integer.
  • Do not use division.
  • Your algorithm must run in O(n) time.

More Square Point problems

drafts saved locally
public int[] productExceptSelf(int[] nums) {
  // write your code here
}
nums[1,2,3,4]
expected[24,12,8,6]
checking account