Sign of the Product of an Array - Solution & Explanation
Problem Statement
Implement a function signFunc(x) that returns:
1ifxis positive.-1ifxis negative.0ifxis equal to0.
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] <= 100
Approach Overview
Problem 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 1: Zero and Negative Count Check
This approach involves scanning through the array once to check for any zeros and counting the number of negative numbers. If there's a zero, the product is zero. Otherwise, if the number of negative numbers is odd, the product is negative; if even, the product is positive.
The C solution uses a loop to iterate over the array. If a zero is found immediately return zero, otherwise count the number of negative integers. If the count is even, return 1. If odd, return -1.
Complexity
Time Complexity: O(n), where n is the number of elements in the array. We only need to pass over the array once.
Space Complexity: O(1), as no additional space beyond the given variables is used.
Approach 2: Multiplicative Identity and Sign Evaluation
This approach uses a multiplicative identity set as variable 'sign', initialized to 1. Then iterates through each element of the array, multiplying 'sign' with -1 for each negative number and returning zero if a zero is found.
This C code sets a 'sign' variable to 1 and iterates over the input. It flips the sign by multiplying it by -1 for each negative number found, returning 0 immediately upon encountering a zero.
Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Approach 3: Direct Traversal
The problem requires us to return the sign of the product of the array elements, i.e., return 1 for positive numbers, -1 for negative numbers, and 0 if it equals 0.
We can define an answer variable ans, initially set to 1.
Then we traverse each element v in the array. If v is a negative number, we multiply ans by -1. If v is 0, we return 0 in advance.
After the traversal is over, we return ans.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
Complexity Comparison
| Approach | Complexity |
|---|---|
| Zero and Negative Count Check | Time Complexity: O(n), where n is the number of elements in the array. We only need to pass over the array once. |
| Multiplicative Identity and Sign Evaluation | Time Complexity: O(n) |
| Direct Traversal | — |
Detailed Complexity Analysis
| 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. |
Video Solution
Sign of An Array - Leetcode 1822 - Python • NeetCodeIO • 8,136 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Sign of the Product of an Array easy or hard?
Sign of the Product of an Array Python/Java solution
How to solve Sign of the Product of an Array in O(n)?
What is the best approach for Sign of the Product of an Array?
Is Sign of the Product of an Array asked at Google/Amazon/Meta?
What data structure is used in Sign of the Product of an Array?
What is the time complexity of Sign of the Product of an Array?
Ready to solve this problem?
Practice Sign of the Product of an Array with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor