Sign of the Product of an Array: Count Negatives
LeetCode 1822. Sign of the Product of an Array asks you to determine whether the product of every element in an integer array is positive, negative, or zero, and return 1, -1, or 0 accordingly. The catch: you do not need to compute the actual product at all.
Example 1: nums = [-1, -2, -3, -4, 3, 2, 1] returns 1. The product is 144, which is positive.
Example 2: nums = [1, 5, 0, 2, -3] returns 0. The product is 0 because the array contains a zero.
Example 3: nums = [-1, 1, -1, 1, -1] returns -1. The product is -1 because there is an odd number of negatives.
The approach
You can skip multiplying the numbers entirely. Two observations give you everything you need:
- If any element is zero, the entire product is zero. Return 0.
- If no element is zero, the sign depends only on how many negatives are in the array. An even count of negatives means the product is positive (return 1). An odd count means the product is negative (return -1).
This works because multiplying two negative numbers produces a positive number. Each pair of negatives cancels out. If there is one left over, the product stays negative.
Never compute the actual product. For large arrays the product can overflow even 64-bit integers. Counting negatives and checking for zeros gives you the sign in O(n) time with zero overflow risk.
The code
def arraySign(nums):
neg_count = 0
for num in nums:
if num == 0:
return 0
if num < 0:
neg_count += 1
return -1 if neg_count % 2 == 1 else 1
Here is what happens step by step:
- Initialize a counter
neg_countto 0. - Iterate through each number in the array.
- If you encounter a zero, return 0 immediately. The product must be zero.
- If the number is negative, increment
neg_count. - After the loop, check whether
neg_countis odd or even. Odd means the product is negative (-1), even means positive (1).
Visual walkthrough
Let's trace through all three examples to see how counting negatives and checking for zeros determines the answer without any multiplication.
Example 1: Even number of negatives
No zeros. 4 negatives (even), so the product is positive. Return 1.
Example 2: Array contains a zero
A zero is present, so the product is 0. Return 0 immediately.
Example 3: Odd number of negatives
No zeros. 3 negatives (odd), so the product is negative. Return -1.
In every case, a single pass through the array is enough. The zero check provides an early exit, and the parity of the negative count decides the rest.
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Count negatives and check for zeros | O(n) | O(1) |
You visit each element exactly once and use only a single integer counter. There is no additional data structure needed, so space stays constant regardless of input size.
Building blocks
Counting elements by property
Scanning an array and tallying elements that satisfy a condition (here, being negative) is one of the most common array patterns. Once you recognize that the answer depends on a count rather than the values themselves, many problems simplify dramatically.
Sign tracking without multiplication
Instead of computing a potentially enormous product, you track just the sign. This idea appears in other contexts too, like determining the sign of a determinant or deciding whether a permutation is even or odd. The core insight is that signs compose simply: negative times negative is positive.
Edge cases
Single element: An array with one element returns the sign of that element directly. The loop runs once and the logic still holds.
All zeros: If every element is zero, the function returns 0 on the very first iteration. The negative count never matters.
All positive: With zero negatives, neg_count stays at 0 (even), so the function returns 1.
Large arrays with mixed signs: Even with millions of elements, the single-pass approach handles it in O(n) time. No overflow risk because you never multiply.
From understanding to recall
This problem tests whether you can avoid the obvious trap of computing the product directly. The building blocks, counting elements by property and reasoning about sign parity, show up in many array and math problems. Practicing this pattern with spaced repetition helps you recognize when a problem asks for a property of a result rather than the result itself. The next time you see a problem that seems to require a huge computation, ask yourself: can I track just the property I need?
Related posts
- Single Number - Counting properties across an array without extra space
- Missing Number - Math-based array deduction
- Move Zeroes - Handling zeros in arrays