Distinct Products After Removing One Element
Learn this problemProblem 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[]) → intExamples
Example 1
A = [9,16,4]return = 3Removing 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 = 4The 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 = 1Either removal leaves the same single value, so only one product is obtainable.
Constraints
2 <= A.length <= 1000001 <= A[i] <= 1000000000