I have been trying out a problem on leetcode 1822. Sign Of the Product of an array
There is a function signFunc(x) that returns:
1 if x is positive.
-1 if x is negative.
0 if x is equal to 0.
You are given a strong text and integer array nums. Let the product be the product of all values in the array nums.
Return signFunc(product).
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
And this is my approach to the problem
class Solution {
public int arraySign(int[] nums) {
double product=1;
for(int i=0;i<nums.length;i++){
product=product*nums[i];
}
if(product>0) return 1;
else if (product <0) return -1;
else return 0;
}
}
The question is whenever I'm taking the product as datatype double all my test cases are passing but when I'm taking it as long my test cases are failing.
**Input:**[9,72,34,29,-49,-22,-77,-17,-66,-75,-44,-30,-24]
**Output:** 1
**Expected:** -1
Can anyone tell me why is this happening???