Happy Number (LeetCode 202) | Full solution with with easy explanation | Study Algorithms

Поділитися
Вставка
  • Опубліковано 11 жов 2024

КОМЕНТАРІ • 52

  • @RohitPal-lz1wf
    @RohitPal-lz1wf Місяць тому +3

    Bro you are really underrated I saw a lot of people having more subscribers but they simply waste time. You are spot on

  • @cse9003
    @cse9003 2 місяці тому +1

    Man, i really found the alogrithm treasure and its YOU, thank you bro

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

    Hey, great video. So the basic intuition is that the pattern repeats (i did not do enough iterations to understand that the pattern is repeating) and therefore we can use the same algorithm which we use to detect if a loop exists in a linked list. Rabbit-hare or floyd marshal algorithm. This way we need not use set to store the already encountered numbers.

    • @nikoo28
      @nikoo28  6 місяців тому +1

      Yes, exactly

  • @RishabhSharma-p9o
    @RishabhSharma-p9o 3 місяці тому

    sir you are really amazing , your teaching style is perfect

  • @rohitkatiyar2340
    @rohitkatiyar2340 2 роки тому +5

    Your explanations are very good.
    Keep up the good work!
    Can u please make a video on Leetcode problem 29( Divide Two Integers)

    • @nikoo28
      @nikoo28  2 роки тому +6

      You can try to solve that problem, but as per my experience it will not be helpful as that problem does not enhance or even improve your problem solving skills. You can skip it if you like.
      Look at the number of dislikes the problem has.
      As a rule of thumb, first try to cover the most liked questions.
      I can soon make a video on how to use LeetCode correctly.

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

      @@nikoo28 did you made that video ?? if yes please share the link

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

      @@JohnWickea How to use LeetCode and pick problems to solve effectively in #2023
      ua-cam.com/video/PcolzuB-fRc/v-deo.html

  • @abandubey
    @abandubey 6 місяців тому +3

    How is the space complexity O(1) if we have used an extra space by creating a HashSet? Can you explain?

    • @nikoo28
      @nikoo28  5 місяців тому

      You will never have the sum of digits that is more than 1000. That means no matter the input size, the hashset has a maximum size. Hence constant space

  • @sonalagrawal-up3bq
    @sonalagrawal-up3bq Місяць тому +1

    I did not understand, why is it guaranteed to have a cycle if not summing up to 1?

  • @RishiJain-yk9xr
    @RishiJain-yk9xr Місяць тому +2

    Thanks bhai

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

    but GFG is showing that it's time complexity is n*log(n). Because finding time complexity of finding the digit in any number is log(n). Can you please help in finding the time complexity .

  • @wish7479
    @wish7479 2 роки тому +1

    Sir,
    Can you make more videos on bit manipulations ?
    Like xor,And ,Or...
    How to use this efficiently and when to use?

    • @nikoo28
      @nikoo28  2 роки тому +2

      I will soon start a segment on bitwise operations.

  • @AadeshKulkarni
    @AadeshKulkarni 8 місяців тому

    Nicely explained, baba!

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

    thank you sir
    i understood clearly

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

    hi! I believe the Time complexity for the solution is O (log n ) not O(1), could you double check that?

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

      why do you think the time complexity will be O(log n)? we do not do any binary operations. O(1) actually means constant time. So we know that no matter what is the test case, it will always finish in a certain amount of time.

  • @iiju8212
    @iiju8212 Рік тому +3

    can anyone explain why is it guaranteed to be cycle? why cant it not continue till infinity ?

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

      because the sum's you can get are finite...no matter what you add up, you will get a sum between 2-99. If any of the number is present in the hashset, that means it will become a cycle. Try to think of a case when it happens infinite times?? can you come up with one?

  • @AtharvaKulkarni-lw4sv
    @AtharvaKulkarni-lw4sv Рік тому +1

    Can you please explain while(true) statement?

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

      that just means an infinite loop.

  • @wish7479
    @wish7479 2 роки тому

    Can you make a video on leetcode 120- triangle. ?

  • @BCAdudes
    @BCAdudes 7 місяців тому

    Actually I wanted to ask we don't have to use any header file in leetcode. I am trying to run the program but I am getting error everytime in leetcode but the same code is working in TURBO C.

    • @nikoo28
      @nikoo28  7 місяців тому

      leetcode already imports some libraries for you

  • @abhishek6575
    @abhishek6575 8 місяців тому

    Sir what i i do same problem using vector and using "find "prebuilt function in vector to solve this .what is best to do using hashset or vector

    • @nikoo28
      @nikoo28  7 місяців тому +1

      both are perfect to use

  • @mohammedilyas8824
    @mohammedilyas8824 2 роки тому +1

    Sir pls make interview based problems

    • @nikoo28
      @nikoo28  2 роки тому +2

      I try to cover interview based problems and most liked problems on LeetCode. If you want any specific questions, let me know :)

    • @mohammedilyas8824
      @mohammedilyas8824 2 роки тому +1

      @@nikoo28 sir pls cover linked list,stack,que,recursion quesns

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

    but here we are using inner while loop calculate sum of digits which takesO(K) then plz explain how timecomplexity will be O(1) sir.

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

      When you say O(K), K is the length of the input integer. Now think about it, the maximum number of digits are 9, as defined by the problem statement. So you know that it will not be greater than O(9), and that means a constant time. Hence O(1)

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

      @@nikoo28 yeah thanks you so much sir

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

    Great!

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

    Thanks

  • @amit9790singh
    @amit9790singh 11 місяців тому +1

    Thanku sir

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

    keep going but please the voice is not clear enough, try another microphone

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

      It is fixed in the newer videos. :)

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

      @@nikoo28 ok thankful I'll watch them

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

    why is while(n!=0) instead of while(n!=1)?

    • @Nishi-Tiwari
      @Nishi-Tiwari Рік тому

      Do you understood now? If yes, please tell me. Thanks.

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

      @@Nishi-Tiwari updated one
      class Solution {
      public boolean isHappy(int n) {
      //set to track the continuous condition
      HashSet set = new HashSet();
      //infinite loop until the conditions match
      while(true){
      int sum =0;
      while(n != 0){
      sum += Math.pow(n%10,2);
      n= n/10;
      }
      // given condition
      if(sum ==1){
      return true;
      }
      //so new n is the sum we have gotten after the squares
      n=sum;
      // else if there is a repetition then return false, which means it will run infinitely, otherwise add it to the set.
      if(set.contains(n)){
      return false;
      }
      else{
      set.add(n);
      }
      }
      }
      }

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

    we are using hashset here . doesn't it will be taking extra space???????

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

      it will be constant space. watch at 10:15

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

      @@nikoo28 thanks for answering

  • @Priya-le1li
    @Priya-le1li 2 роки тому

    Nice👍🏼

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

    class Solution {
    public boolean isHappy(int n) {
    boolean ans;
    while(true){
    int temp=n;
    int sum=0;
    while(temp>0){
    int r=temp%10;
    sum+=Math.pow(r,2);
    temp/=10;
    }
    n=sum;
    if(sum

  • @mdshayanurrahman78
    @mdshayanurrahman78 15 днів тому

    Sir please make video in hindi as well

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

    op explanation

  • @shivashukla7894
    @shivashukla7894 Місяць тому

    please use good mike