FastPrepDistinct Products After Removing One Element
Problem · Array

Distinct Products After Removing One Element

Learn this problem
EasyHSBC logoHSBCNEW GRADOA

Problem statement

You are given an array A of positive integers. Remove exactly one element and compute the product of all remaining elements.

If removing different positions produces the same numerical product, count that product only once. When one element remains, its value is the product.

Return the number of distinct products obtainable by removing exactly one element.

The answer must be computed without constructing the potentially enormous products.

Function

solution(A: int[]) → int

Examples

Example 1

A = [9,16,4]return = 3

Removing 9, 16, or 4 gives products 64, 36, and 144, so there are 3 distinct products.

Example 2

A = [3,4,2,3,1]return = 4

The two occurrences of 3 produce the same remaining product. Removing each of the other distinct values produces a different product, for 4 possibilities.

Example 3

A = [1000000000,1000000000]return = 1

Either removal leaves the same single value, so only one product is obtainable.

Constraints

  • 2 <= A.length <= 100000
  • 1 <= A[i] <= 1000000000

More HSBC problems

drafts saved locally
public int solution(int[] A) {
    // Write your code here.
}
A[9,16,4]
expected3
checking account