There is a function signFunc(x) that returns:

You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

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
Input: nums = [1,5,0,2,-3]
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0
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:

Contents

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 -1, if there are even number of negative integers then we wil return 1, and if there is atleast one 0 then we will return 0.

public class SignOfArray { static int arraySign(int[] nums) { int totalNegatives = 0; for(int i=0; i<nums.length; i++) { if(nums[i] == 0) { return 0; } if(nums[i] <0) { totalNegatives++; } } return totalNegatives %2 ==0 ? 1 : -1; } 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})); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of nums array.
Space complexity: O(1)

Above implementations source code can be found at GitHub link for Java code