FastPrepProduct Of Array Except Self
Problem · Array

Product Of Array Except Self

Learn this problem
Mediuminfosys logoinfosysNEW GRADONSITE INTERVIEW

Problem statement

You are given an integer array nums of length n.

Return an array answer of the same length where answer[i] is the product of every value in nums except nums[i].

The product of any prefix or suffix of nums fits in a 32-bit integer. Do not use the division operator.

Function

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

Examples

Example 1

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

The products excluding each index are 2*3*4, 1*3*4, 1*2*4, and 1*2*3.

Example 2

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

A single zero makes every other product zero. The product excluding that zero is -1*1*-3*3 = 9.

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.
  • Every output value answer[i] also fits in a signed 32-bit integer.

More infosys 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