Difficulty: Easy | Acceptance: 64.80% | Paid: No Topics: Array, Math
There is 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).
- Examples
- Constraints
- Approach 1: Direct Multiplication
- Approach 2: Counting Negatives
Examples
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 1: Direct Multiplication
Intuition The most straightforward way to solve this problem is to calculate the product of all numbers in the array and then check its sign. This directly follows the problem statement.
Steps
- Initialize a variable
productto 1. - Iterate through the array
nums, multiplying each element intoproduct. - After the loop, check if
productis greater than 0 (return 1), less than 0 (return -1), or equal to 0 (return 0).
python
class Solution:
def arraySign(self, nums: list[int]) -> int:
product = 1
for num in nums:
product *= num
if product > 0:
return 1
elif product < 0:
return -1
else:
return 0Complexity
- Time: O(n) - We iterate through the array once.
- Space: O(1) - We only use a single variable to store the product (ignoring the space for the BigInt object itself in languages like Java/JS).
- Notes: In languages with fixed-size integers (C++, Java with
int), this approach risks overflow because the product can grow up to 100¹⁰⁰⁰, which exceeds the limit of a 64-bit integer (2⁶³-1). To make it runnable in Java and JavaScript, we use arbitrary-precision integers (BigIntegerandBigInt). In C++,long longwill overflow for large inputs, making this approach technically incorrect for the upper constraint limits without a big-integer library.
Approach 2: Counting Negatives
Intuition We can determine the sign of a product without calculating the product itself. The sign is positive if the number of negative integers is even, and negative if the count is odd. If any number is zero, the product is immediately zero.
Steps
- Initialize a counter
negCountto 0. - Iterate through the array
nums:- If the current number is 0, return 0 immediately.
- If the current number is negative, increment
negCount.
- After the loop, if
negCountis even, return 1. Otherwise, return -1.
python
class Solution:
def arraySign(self, nums: list[int]) -> int:
neg_count = 0
for num in nums:
if num == 0:
return 0
if num < 0:
neg_count += 1
return -1 if neg_count % 2 else 1Complexity
- Time: O(n) - We traverse the array once.
- Space: O(1) - We only use a single integer counter.
- Notes: This is the optimal approach as it avoids any potential overflow issues and uses minimal memory.