Problem · Array

Product Except Self With Zeros

Learn this problem
MediumAppleONSITE INTERVIEW

Problem statement

You are given an integer array nums. Return an array result where result[i] is the product of all elements in nums except nums[i].

The source solution explicitly handled three zero cases:

  • If there are no zeroes, each position receives the product of all numbers divided by the current value.
  • If there is exactly one zero, only the zero position receives the product of all non-zero values.
  • If there is more than one zero, every output value is 0.

Function

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

Examples

Example 1

nums = [1,2,0,4]return = [0,0,8,0]

The source shared the rule but did not include this exact sample. FastPrep added this small example so the behavior can be checked directly.

Constraints

  • The source expects integer input.
  • The source solution uses integer division in the no-zero case.

More Apple problems

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