Learner Level

Implement the method productSign that returns the sign of the product of an integer array.

Return 1 for a positive product, -1 for a negative product, and 0 if the product is zero.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Do not multiply all values directly because large products can overflow.
  • Return 0 as soon as a zero is found.
  • An odd count of negative values means the product is negative.
Example 1
Input:
nums (int[]) = [-1,-2,3]
size (int) = 3
Return:
(int) 1
Example 2
Input:
nums (int[]) = [-1,0,5]
size (int) = 3
Return:
(int) 0
Example 3
Input:
nums (int[]) = [-1,2,3]
size (int) = 3
Return:
(int) -1

Scan once, checking for zero and counting negative numbers.

Run your code to see the result.