There is a function
-
1 ifx is positive. -
-1 ifx is negative. -
0 ifx is equal to 0.
You are given an integer array
Return
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0
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
Contents
Solution 1 - Count total number of negatives, return -1 if there are odd number of negatives, 1 otherwise, and 0 if there is atleast one 0
In this approach, instead of calculating the product of array, we will simply count the number of negative integers, if there are are odd number of
negative interegs, then the product is going to be a negative value, so we will return
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1)
Above implementations source code can be found at