Sign of the Product of an Array (LeetCode 1822): Solution and Explanation
Given an integer array nums, return 1 if the product of all its values is positive, -1 if it is negative and 0 if it is zero. The trick is to answer without ever computing the product.
Intuition
Multiplying everything together is the obvious move, but 1000 values of size up to 100 give a product with up to 2000 digits. In most languages that overflows a 64-bit integer long before the end, and even where big integers exist it is wasted work, because the question only asks for the sign.
The sign of a product depends on just two facts. If any factor is 0, the product is 0. Otherwise each negative factor flips the sign and each positive factor leaves it alone, so the product is negative exactly when the number of negatives is odd.
That turns the problem into a scan with one variable. Start with sign = 1, flip it on each negative, and return 0 the moment a zero appears, since nothing after a zero can change the answer.
Approach: Track the sign, not the product
- Set
sign = 1, the sign of an empty product. - For each
numinnums, return 0 immediately ifnum == 0. - If
num < 0, setsign = -sign. Positive numbers do nothing. - After the loop, return
sign.
Step-by-step example
nums = [-1,-2,-3,-4,3,2,1]
- Start with
sign = 1. - -1 is negative, so
signflips to -1. - -2 flips it back to 1, -3 flips it to -1, and -4 flips it to 1.
- 3, 2 and 1 are positive and leave
signat 1. - No zero was found and there were 4 negatives, an even count, so the function returns 1. The real product is 144.
Code: Python, Java, C++
1class Solution:
2 def arraySign(self, nums: List[int]) -> int:
3 sign = 1
4 for num in nums:
5 if num == 0:
6 return 0
7 if num < 0:
8 sign = -sign
9 return sign1class Solution {
2 public int arraySign(int[] nums) {
3 int sign = 1;
4 for (int num : nums) {
5 if (num == 0) return 0;
6 if (num < 0) sign = -sign;
7 }
8 return sign;
9 }
10}1class Solution {
2public:
3 int arraySign(vector<int>& nums) {
4 int sign = 1;
5 for (int num : nums) {
6 if (num == 0) return 0;
7 if (num < 0) sign = -sign;
8 }
9 return sign;
10 }
11};Time and space complexity
O(n)
Each element is examined once, and the scan can stop early at the first zero.
O(1)
Only the sign variable is stored.
Other approaches
Count negatives (time O(n), space O(1))
Count the negative numbers and check for any zero, then return 0, 1 or -1 from the zero flag and the parity of the count. It is the same idea with a counter instead of a flipping sign.
Multiply directly (time O(n * d), space O(d))
Compute the actual product and take its sign. It works in Python thanks to arbitrary precision integers, but it builds a number with thousands of digits and overflows in Java, C++, C#, Go and JavaScript.
Common mistakes
- Computing the product in a fixed-width integer. It overflows and the sign of the overflowed value is meaningless.
- Multiplying by
sign(num)but forgetting that a zero must make the answer 0 for good. - Continuing the loop after a zero. It still gives the right answer if the zero case is handled, but returning immediately is simpler and faster.
Frequently asked questions
Why not just multiply the numbers?
With up to 1000 factors of magnitude up to 100, the product can have about 2000 digits, which overflows every fixed-size integer type. Tracking the sign avoids the problem completely.
Why does the parity of negative numbers decide the sign?
Each negative factor multiplies the running product by a negative number, which flips its sign. An even number of flips returns to positive, an odd number ends negative.
Can I stop at the first zero?
Yes. Zero times anything is zero, so the answer is 0 regardless of the remaining elements.
What are the time and space complexity?
O(n) time and O(1) extra space.