Sign of An Array - Leetcode 1822 - Python

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 28

  • @DeathSugar
    @DeathSugar Рік тому +22

    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

  • @abisheknair3545
    @abisheknair3545 Рік тому +2

    n=1
    for i in nums:
    if i==0:
    return 0
    if i

  • @steeve1
    @steeve1 Рік тому +7

    literally the exact code I came up with. Finally feel like I am making progress. #feelsgoodman

  • @youknownothing_
    @youknownothing_ Рік тому +1

    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

  • @d.h.y
    @d.h.y Рік тому

    The channel we've needed.

  • @BonerPauler
    @BonerPauler Рік тому

    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

  • @flatmapper
    @flatmapper Рік тому

    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

  • @sabu4539
    @sabu4539 9 місяців тому

    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
    };

  • @shrit1401
    @shrit1401 Рік тому

    isnt the problem saying to create a function, signFunc()?

  • @Sibearian_
    @Sibearian_ Рік тому

    U could also do a true or false for neg 6:36. So if we encounter a negative number we can negate it.

    • @ericnl
      @ericnl Рік тому

      yep, that's what i did too!

  • @theOPindian
    @theOPindian Рік тому +5

    leetcode is bugged rn, everyone is getting a 5% solution lol

    • @jayjanodia9445
      @jayjanodia9445 Рік тому +1

      Oh wow and here I was thinking it was due to my slow internet conmection

  • @BurhanAijaz
    @BurhanAijaz Рік тому +1

    my code:
    class Solution:
    def arraySign(self, nums: List[int]) -> int:
    pro=1
    for i in nums:
    if i==0:
    return i
    elif i

  • @arshalakhan567
    @arshalakhan567 Рік тому

    Hey! Thanks for amazing content. Any idea when hiring will start at FAANG?

  • @animeshdubey941
    @animeshdubey941 2 місяці тому

    Thanks a lot.

  • @novascotia2015
    @novascotia2015 Рік тому

    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)
    }
    }

  • @SAIGOVINDSATISH
    @SAIGOVINDSATISH Рік тому

    SIR please upload daily streak question of medium and hard level .You explain concepts in top noch manner .Please help students like us😢

  • @infinitygod5379
    @infinitygod5379 Рік тому

    How is may 1 related to question difficulty

    • @def__init
      @def__init Рік тому +1

      1st question of the month they usually don’t start off crazy hard to scare you off lol

  • @vixguy
    @vixguy Рік тому +1

    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.

  • @Rob-147
    @Rob-147 Рік тому

    Legend

  • @kirillzlobin7135
    @kirillzlobin7135 6 місяців тому

    Amazing

  • @novascotia2015
    @novascotia2015 Рік тому

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

  • @flatmapper
    @flatmapper Рік тому

    One of the easiest problems

  • @krishnachoudhary4924
    @krishnachoudhary4924 Рік тому +1

    The leetcode link is for another problem though.