0

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???

Rohan Sharma
  • 55
  • 1
  • 4
  • 10
  • 3
    Hint: you don't actually need to do any multiplication for this task. All you need to do is count the number of negative elements in the array (and immediately return `0` whenever you see a `0` in tje array). – Joachim Sauer Nov 25 '21 at 10:46
  • Exactly what Joachim says. For any two negative numbers applies that the product is positive. So only if you encounter an *odd* negative number, the product is negative. – MC Emperor Nov 25 '21 at 12:13

2 Answers2

3

For the solution of the problem, correctly stated by Joachim Sauer that just count the negative integers and based on that display the result.

To ans this : With datatype Double all testcases are passing, but why not with datatype Long???

This happens due to Overflow and Underflows. If it overflows, it goes back to the minimum value, and if it underflows, it goes to the maximum value. The ans to this is explained in this link for Integers and the same applies to all the dataypes in Java.

As the max value for Long is 9223372036854775807 which is approximately 9.2233E18, multiplying the last integer (-24 from your input set) creates the situation of underflow, causing it to start from Long.MAX_VALUE.

Urvashi Soni
  • 279
  • 1
  • 3
  • 13
1

Because of overflow, you can instead play on the idea of the sign only without product, I mean that if you have a container and it contains a -ve value and in the loop, you find another -ve value so, change the value of the container to be +ve without multiplying them, so when, you finish and check it, you will check a small number in comparison with the product.

public static int arraySign(int[] nums)
{
    int productSign=1;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i]==0) return 0;
        else if(nums[i]<0 && productSign<0)
            productSign = -nums[i];
        else if(nums[i]<0 && productSign>0)
            productSign = nums[i];
    }
    return signFunc(productSign);
}
public static int signFunc(int x)
{
    if (x > 0)
        return 1;
    else
        return -1;
}
Nour Amr
  • 11
  • 1