Simple Java || C++ || JavaScript Code ☠️
java []\nclass Solution {\n    public int arraySign(int[] nums) {\n        int n=nums.length;\n        int nc=0;\n        for(int i=0;i<n;i++){\n            if(
```java []\nclass Solution {\n    public int arraySign(int[] nums) {\n        int n=nums.length;\n        int nc=0;\n        for(int i=0;i<n;i++){\n            if(nums[i]==0){\n                return 0;\n            }\n            if(nums[i]<0){\n                nc++;\n            }\n        }\n        if(nc % 2 == 0){\n            return 1;\n        }\n        else{\n            return -1;\n        }\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int arraySign(vector<int>& nums) {\n        int n=nums.size();\n        int nc=0;\n        for(int i=0;i<n;i++){\n            if(nums[i]==0){\n                return 0;\n            }\n            if(nums[i]<0){\n                nc++;\n            }\n        }\n        if(nc % 2 == 0){\n            return 1;\n        }\n        else{\n            return -1;\n        }\n    }\n};\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar arraySign = function(nums) {\n    let n=nums.length;\n    let nc=0;\n    for(let i=0;i<n;i++){\n        if(nums[i]==0){\n            return 0;\n        }\n        if(nums[i]<0){\n            nc++;\n        }\n    }\n    if(nc % 2 == 0){\n        return 1;\n    }\n    else{\n        return -1;\n    }\n};\n```\n\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe `arraySign` method is designed to determine the sign of the product of all the elements in an integer array `nums` without explicitly computing the product itself. This approach is both time-efficient and avoids the potential pitfalls of integer overflow that might arise from multiplying many elements together. The method relies on the following key observations:\n\n1. **Zero Check**:\n   - If any element in the array is `0`, the product of the entire array will necessarily be `0`. Thus, the method can immediately return `0` upon encountering a zero element.\n\n2. **Negative Count**:\n   - The sign of a product is influenced by the number of negative numbers in the array:\n     - An even number of negative numbers results in a positive product.\n     - An odd number of negative numbers results in a negative product.\n   - By counting the number of negative elements (denoted as `nc`), the method can determine the sign of the product without performing the actual multiplication.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe `arraySign` method aims to determine the sign of the product of an array of integers without directly calculating the product, thereby avoiding overflow and unnecessary computation. The approach consists of the following steps:\n\n1. **Initialization**:\n   - **Length of Array**: `int n = nums.length;` determines the number of elements in the array.\n   - **Negative Count**: `int nc = 0;` initializes a counter `nc` to track the number of negative elements in the array.\n\n2. **Iterating Through the Array**:\n   - The method uses a `for` loop to iterate over each element in the array:\n     ```java\n     for(int i = 0; i < n; i++) {\n     ```\n   - **Zero Check**: Within the loop, the method checks if the current element is zero:\n     ```java\n     if(nums[i] == 0) {\n         return 0;\n     }\n     ```\n     - If any element is zero, the product of the entire array will be zero. Hence, the method returns `0` immediately.\n   \n   - **Negative Count**: If the current element is negative, the method increments the negative count `nc`:\n     ```java\n     if(nums[i] < 0) {\n         nc++;\n     }\n     ```\n     - This keeps track of how many negative numbers are present in the array.\n\n3. **Determine the Sign of the Product**:\n   - After the loop completes, the method determines the sign of the product based on the count of negative numbers:\n     ```java\n     if(nc % 2 == 0) {\n         return 1;\n     } else {\n         return -1;\n     }\n     ```\n   - **Even Count of Negatives**: If `nc` is even, the product of the numbers in the array is positive, so the method returns `1`.\n   - **Odd Count of Negatives**: If `nc` is odd, the product of the numbers in the array is negative, so the method returns `-1`.\n\nCertainly! Let\'s walk through an example to illustrate how the `arraySign` method works step-by-step.\n\n### Example\n\nConsider the input array: `nums = [-1, 2, -3, 4, -5]`.\n\n### Step-by-Step Execution\n\n1. **Initialization**:\n   - `int n = nums.length;` determines the length of the array, which is `5`.\n   - `int nc = 0;` initializes the negative count `nc` to `0`.\n\n2. **Iterating Through the Array**:\n   - **First Element (`nums[0] = -1`)**:\n     - The method checks if `nums[0] == 0`. It is not zero.\n     - The method checks if `nums[0] < 0`. It is negative, so `nc` is incremented to `1`.\n   \n   - **Second Element (`nums[1] = 2`)**:\n     - The method checks if `nums[1] == 0`. It is not zero.\n     - The method checks if `nums[1] < 0`. It is not negative, so `nc` remains `1`.\n   \n   - **Third Element (`nums[2] = -3`)**:\n     - The method checks if `nums[2] == 0`. It is not zero.\n     - The method checks if `nums[2] < 0`. It is negative, so `nc` is incremented to `2`.\n\n   - **Fourth Element (`nums[3] = 4`)**:\n     - The method checks if `nums[3] == 0`. It is not zero.\n     - The method checks if `nums[3] < 0`. It is not negative, so `nc` remains `2`.\n\n   - **Fifth Element (`nums[4] = -5`)**:\n     - The method checks if `nums[4] == 0`. It is not zero.\n     - The method checks if `nums[4] < 0`. It is negative, so `nc` is incremented to `3`.\n\n3. **Determine the Sign of the Product**:\n   - After iterating through the entire array, `nc` (negative count) is `3`.\n   - The method checks if `nc % 2 == 0` to determine if the count of negative numbers is even. In this case, `3 % 2 != 0`, so the count is odd.\n   - Since the count of negative numbers is odd, the product of the array\'s elements is negative. Therefore, the method returns `-1`.\n\n\n\n# Complexity\n\n1. **Time Complexity**: The method iterates through the array exactly once, resulting in a time complexity of `(O(n))`, where \\(n\\) is the length of the array.\n2. **Space Complexity**: The method uses a constant amount of extra space (for variables `n` and `nc`), leading to a space complexity of `(O(1))`.\n\n
['Array', 'Math', 'C++', 'Java', 'JavaScript']