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
Returns (int) 1

Example 2

Input
nums (int[]) = [-1,0,5]
size (int) = 3
Returns (int) 0

Example 3

Input
nums (int[]) = [-1,2,3]
size (int) = 3
Returns (int) -1

Scan once, checking for zero and counting negative numbers.

Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.