Watch 10 video solutions for Sign of the Product of an Array, a easy level problem involving Array, Math. This walkthrough by NeetCodeIO has 8,020 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Implement a function signFunc(x) that returns:
1 if x is positive.-1 if x is negative.0 if x is equal to 0.You are given an integer array nums. Let product be the product of all values in the array nums.
Return signFunc(product).
Example 1:
Input: nums = [-1,-2,-3,-4,3,2,1] Output: 1 Explanation: The product of all values in the array is 144, and signFunc(144) = 1
Example 2:
Input: nums = [1,5,0,2,-3] Output: 0 Explanation: The product of all values in the array is 0, and signFunc(0) = 0
Example 3:
Input: nums = [-1,1,-1,1,-1] Output: -1 Explanation: The product of all values in the array is -1, and signFunc(-1) = -1
Constraints:
1 <= nums.length <= 1000-100 <= nums[i] <= 100Problem Overview: You receive an integer array nums and must return the sign of the product of all elements. The result is 1 if the product is positive, -1 if negative, and 0 if any element makes the product zero. Computing the full product can overflow quickly, so the task is really about reasoning about signs rather than multiplication.
Approach 1: Zero and Negative Count Check (O(n) time, O(1) space)
Instead of multiplying every element, iterate through the array once and track two conditions: whether a zero exists and how many negative numbers appear. If any element equals 0, the product is immediately 0. Otherwise, the sign depends entirely on the parity of the negative count. An even number of negatives produces a positive product, while an odd number produces a negative product. This works because sign multiplication follows simple parity rules and avoids overflow entirely. The solution only requires a single pass through the array and constant extra memory.
Approach 2: Multiplicative Identity and Sign Evaluation (O(n) time, O(1) space)
Another method keeps a running sign variable initialized to 1, which represents the multiplicative identity. Traverse the array and update the sign as you go. If you encounter 0, return 0 immediately since the product becomes zero. For each negative number, flip the current sign by multiplying by -1. Positive numbers leave the sign unchanged. This approach mimics multiplication behavior but only tracks sign transitions instead of actual values. It is a clean implementation grounded in basic math properties and still requires just a single linear scan.
Recommended for interviews: The zero and negative count method is typically what interviewers expect. It clearly demonstrates your understanding that only the count of negative values and the presence of zero determine the final sign. Mentioning the overflow issue with direct multiplication shows practical engineering awareness. The running-sign variant is equally optimal and slightly more elegant, but both approaches demonstrate the same O(n) reasoning over the array.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Zero and Negative Count Check | O(n) | O(1) | Best general solution; clearly explains the role of zero detection and negative parity. |
| Multiplicative Identity and Sign Evaluation | O(n) | O(1) | Useful when modeling sign behavior directly without counting negatives. |