well i came up with this solution. i had also used kind of the same approach. class Solution { public int arraySign(int[] nums) { int res = 1; for(int n: nums) { if(n == 0) return 0; if(n
my approach would be why dont we loop a function through each element and determine its sign that calculates number of negatives found in the array the we can deducethe sign of the array...there will a lot of function calls but if we could keep it short i think thatsbetter then multiplying 6:10
class Solution { public int arraySign(int[] nums) { int sign = 1; // Initialize the sign to positive for(int num : nums) { if(num == 0) { return 0; // If the element is 0, the product is 0 } else if(num < 0) { sign ^= 1; // Flip the sign bit if the element is negative } } return sign * 2 - 1; // Convert the sign bit to a sign (-1 or 1) } }
class Solution { public int arraySign(int[] nums) { int negativeCount = 0; // Initialize the count of negative numbers to 0 for(int num : nums) { if(num == 0) { return 0; // If the element is 0, the product is 0 } else if(num < 0) { negativeCount++; // Increment the count if the element is negative } } if(negativeCount % 2 == 0) { return 1; // Return 1 if the number of negative numbers is even } else { return -1; // Return -1 if the number of negative numbers is odd } } }
You just can set initial value to 1 and flip it's sign every time you meet negative and just return it at the end without conditions
n=1
for i in nums:
if i==0:
return 0
if i
literally the exact code I came up with. Finally feel like I am making progress. #feelsgoodman
Nice job 👏
same, i came up with the solution on my own 🤯🤯🥲🥲
well i came up with this solution. i had also used kind of the same approach.
class Solution {
public int arraySign(int[] nums) {
int res = 1;
for(int n: nums) {
if(n == 0) return 0;
if(n
The channel we've needed.
my approach would be why dont we loop a function through each element and determine its sign that calculates number of negatives found in the array the we can deducethe sign of the array...there will a lot of function calls but if we could keep it short i think thatsbetter then multiplying 6:10
Just
class Solution:
def arraySign(self, nums):
result = 1
for num in nums:
if num == 0:
return 0
if num < 0:
result *= -1
return result
works fine -
var arraySign = function(nums) {
let product = 1;
for(let i = 0; i < nums.length; i++){
product *= nums[i] / 2;
if(product == 0) return 0;
}
return (product > 0) ? 1 : -1
};
isnt the problem saying to create a function, signFunc()?
U could also do a true or false for neg 6:36. So if we encounter a negative number we can negate it.
yep, that's what i did too!
leetcode is bugged rn, everyone is getting a 5% solution lol
Oh wow and here I was thinking it was due to my slow internet conmection
my code:
class Solution:
def arraySign(self, nums: List[int]) -> int:
pro=1
for i in nums:
if i==0:
return i
elif i
Hey! Thanks for amazing content. Any idea when hiring will start at FAANG?
Thanks a lot.
class Solution {
public int arraySign(int[] nums) {
int sign = 1; // Initialize the sign to positive
for(int num : nums) {
if(num == 0) {
return 0; // If the element is 0, the product is 0
} else if(num < 0) {
sign ^= 1; // Flip the sign bit if the element is negative
}
}
return sign * 2 - 1; // Convert the sign bit to a sign (-1 or 1)
}
}
SIR please upload daily streak question of medium and hard level .You explain concepts in top noch manner .Please help students like us😢
How is may 1 related to question difficulty
1st question of the month they usually don’t start off crazy hard to scare you off lol
i think leetcode is having some sort of seizure rn.
if you copy and paste one of the top percent solutions it still gives ~5% runtime.
Legend
Amazing
class Solution {
public int arraySign(int[] nums) {
int negativeCount = 0; // Initialize the count of negative numbers to 0
for(int num : nums) {
if(num == 0) {
return 0; // If the element is 0, the product is 0
} else if(num < 0) {
negativeCount++; // Increment the count if the element is negative
}
}
if(negativeCount % 2 == 0) {
return 1; // Return 1 if the number of negative numbers is even
} else {
return -1; // Return -1 if the number of negative numbers is odd
}
}
}
One of the easiest problems
The leetcode link is for another problem though.