Sign of An Array
Tell whether the product of an array's numbers is positive, negative or zero, without multiplying them.
Take an array of integers nums
Example 1
Output: 1
Explanation: the product is 144, which is positive.
Example 2
Output: 0
Explanation: the product is 0.
Example 3
Output: -1
Explanation: the product is -1, which is negative.
The Idea
You learned the sign rules for multiplication at school: a minus times a minus gives a plus, and a minus times a plus gives a minus. Anything times zero is zero. This problem asks us to apply those rules to a whole array at once.
The obvious way is to multiply everything and look at the result. For Example 1 that works: -1 times -2 times -3 times -4 times 3 times 2 times 1 is 144, so the answer is 1. But the array can hold 1000 numbers of size 100, and their product can be 100 multiplied by itself 1000 times. That is a 1 followed by 2000 zeros, far too big for an intlong
We can, because the size of each number never changes the sign. Only two things do: whether a number is negative, and whether it is zero. Look at the picture below.
In the top row, read Example 1 from left to right and keep only the sign so far. The first number, -1, makes it -1. Then -2 flips it back to +1, -3 flips it to -1, and -4 flips it to +1 again. The positive numbers 3, 2 and 1 leave it alone, so the answer is 1.
In the bottom row, Example 2 starts the same way, with 1 and 5 keeping the sign at +1. Then comes 0. Whatever follows, the product is now 0, so there is no need to look at 2 or -3.
So the answer depends only on whether the array has a zero and, if not, on how many negative numbers it has. An even count gives a positive product, and an odd count gives a negative one.
Solution Approach
Turning that idea into code means settling two things: how to keep track of the sign, and what to do when a zero turns up.
Keeping track of the sign
How do we remember the sign without the product? Keep a single variable signsign = -sign
Counting the negative numbers and checking at the end whether the count is even would work just as well. Flipping sign
Stopping at a zero
What should happen at a zero? The product is 0 from then on, whatever numbers come later, so the method can return 0 at once. That also saves reading the rest of the array.
Java Solution
The method arraySign
- First, set
to 1.sign - Go through the array with one loop, taking each number as
.x - If
is 0, return 0 right away.x - If
is negative, flipx withsign .sign = -sign - When the loop ends without meeting a zero, return
.sign
The mainarraySignnaiveSignint
import java.util.Arrays;
public class SignOfAnArray {
public static int arraySign(int[] nums) {
int sign = 1;
for (int x : nums) {
if (x == 0) {
return 0; // one zero makes the product 0
}
if (x < 0) {
sign = -sign; // each negative number flips the sign
}
}
return sign;
}
// The tempting way: multiply everything, then look at the result.
static int naiveSign(int[] nums) {
int product = 1;
for (int x : nums) {
product *= x;
}
return Integer.signum(product);
}
public static void main(String[] args) {
System.out.println(arraySign(new int[] {-1, -2, -3, -4, 3, 2, 1}));
System.out.println(arraySign(new int[] {1, 5, 0, 2, -3}));
System.out.println(arraySign(new int[] {-1, 1, -1, 1, -1}));
System.out.println(arraySign(new int[] {-7}));
System.out.println(arraySign(new int[] {-5, -5, -5, 0}));
int[] sixNegatives = new int[6];
Arrays.fill(sixNegatives, -100);
int[] fortyTwos = new int[40];
Arrays.fill(fortyTwos, 2);
System.out.println("six -100s: arraySign " + arraySign(sixNegatives)
+ ", naive " + naiveSign(sixNegatives));
System.out.println("forty 2s: arraySign " + arraySign(fortyTwos)
+ ", naive " + naiveSign(fortyTwos));
}
}
Running it prints:
1
0
-1
-1
0
six -100s: arraySign 1, naive -1
forty 2s: arraySign 1, naive 0
The first three lines, 1, 0 and -1, match the three examples. Next, [-7][-5, -5, -5, 0]
The last two lines show why the method never multiplies. Six -100s multiply to 1,000,000,000,000, which is positive, but an intintnaiveSignnaiveSignarraySign
The original implementation,
Complexity
The work is one pass over the array, with two checks per number.
- Time: O(n), because the loop looks at each of the n numbers in the array at most once, and stops early at the first zero.
- Space: O(1). Only
and the current number are kept, however long the array is.sign
Keeping track of what negative numbers do to a product also matters in