Java Solution for Sign of the product of an array Problem
Intuition\n Describe your first thoughts on how to solve this problem. \n The intuition behind this approach is that the sign of the product of an array of numb
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n The intuition behind this approach is that the sign of the product of an array of numbers is determined by the number of negative elements in the array. If the number of negative elements is even, the product is positive, otherwise, it is negative. If there is a zero in the array, then the product is zero.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis implementation counts the number of negative integers in the input array. If the count is even, the product of all numbers is positive, and signFunc() returns 1. If the count is odd, the product of all numbers is negative, and signFunc() returns -1. If the input array contains a zero, the product is zero, and signFunc() returns 0. This approach works well even for large length of nums.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n), where n is the length of the input array.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe solution uses a constant amount of space to store the count of negative elements, so the space complexity is O(1)\n\n# Code\n```\nclass Solution {\n    public int arraySign(int[] nums) {\n       int countNegatives = 0;\n        for (int num : nums) \n        {\n            if (num == 0) \n            {\n                return 0;\n            } \n            else if (num < 0) \n            {\n                countNegatives++;\n            }\n        }\n        return (countNegatives % 2 == 0) ? 1 : -1;\n    }\n}\n```
['Java']