leetcode 1822 solution in java

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 an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

The problem description is taken from :- Leetcode Problem

Solution:-

Flow Diagram:-

Code Block:-

public static int arraySign(int [] nums) {
    int sign = 1; 
    for (int n=0; n< nums.length; n++ ) {
    	
        if (nums[n] == 0) {
        	System.out.println("The value of n " + n);
            return 0; 
            
        } else if (nums[n] < 0) {
        	//System.out.println("The value of n222 " + nums [n]);
            sign = -sign; 
        }
    }
    return sign; 
}

For more problem and solution please visit:- Algorithms and Datastructure