Skip to content
← All posts

Sign of the Product of an Array: Count Negatives

3 min read
leetcodeproblemeasyarraysmath

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.

[0][1][2][3][4][5][6]-1-2-3-4321----+++Negative count: 4 (even)sign = +1negativepositive
Array [-1, -2, -3, -4, 3, 2, 1] has 4 negatives (even count), so the product is positive and signFunc returns 1.

The approach

You can skip multiplying the numbers entirely. Two observations give you everything you need:

  1. If any element is zero, the entire product is zero. Return 0.
  2. 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:

  1. Initialize a counter neg_count to 0.
  2. Iterate through each number in the array.
  3. If you encounter a zero, return 0 immediately. The product must be zero.
  4. If the number is negative, increment neg_count.
  5. After the loop, check whether neg_count is 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

-1-2-3-4321
zero found = falsenegative count = 4 (even)
result: 1

No zeros. 4 negatives (even), so the product is positive. Return 1.

Example 2: Array contains a zero

1502-3
zero found = truenegative count = 1 (odd)
result: 0

A zero is present, so the product is 0. Return 0 immediately.

Example 3: Odd number of negatives

-11-11-1
zero found = falsenegative count = 3 (odd)
result: -1

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

ApproachTimeSpace
Count negatives and check for zerosO(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