Fast pointer should start at index one . because in the while loop we are checking . run the while loop until slow and fast pointer are not equal (while (slow !== fast)) so if both start at the same place i.e 0 . the while loop will not execute
Thank you for the amazing explanation sir. I just changed the sequence of code for the HappyNumber problem in the getNext method and the runtime decreased to 0ms. public int getNext(int n){ int totalsum=0; while(n>0){ int d=n%10; totalsum= totalsum+ d*d; n=n/10; }return totalsum; } earlier as you explained , n/=10; was before totalsum calculation still the runtime was 1ms.
Hello, amazing video. Can you explain why the time complexity of cycle in linked list would be O(n). We know that if there's a cycle they will meet at some point, but that could take a lot of time in some cases, no? Thank you.
In the context of a two-pointer problem like the Happy Number problem, the key is to understand when to exit the sequence creation and move to the two-pointer check. The idea behind the two-pointer technique is to use two different pointers (usually iterating at different speeds) to detect a cycle in a sequence. In the Happy Number problem, you're generating a sequence of numbers by replacing a number with the sum of the squares of its digits. The process repeats with the new number. The two-pointer technique is particularly useful here for detecting if this sequence enters a cycle, which would mean the number is not a happy number. Here's how you would apply this technique: Fast and Slow Pointers: Start with two pointers, slow and fast. Initially, both point to the original number. Then, move slow by one step (i.e., calculate the sum of the squares of its digits once) and fast by two steps (i.e., do this process twice). Cycle Detection: If at any point, the fast pointer equals the slow pointer, a cycle has been detected. There are two possibilities here: If they meet at 1, then the number is a happy number. If they meet at any other number, the number is not a happy number. Exit Condition: The loop should continue as long as slow does not equal fast. As soon as they meet, you can exit the loop and determine whether the number is happy based on the value where they meet. This method is efficient for detecting cycles in sequences and is a popular approach in problems that involve repeated operations leading to potential loops.
Infinite search is never always good idea, not sustainable solution I guess. But I have to see the question to really understand what are you talking about. I don't remember the question TBH
Hey! Great work! I hope the grind has paid off for you.
Yes, it keeps on giving. But grind never stops.
Your videos always make me smile, thank you for brightening my day!
Your comment made my day as well. 🥹
i have a question fast and slow pointer both should start from index 0 or fast pointer should be at index 1 at starting?
Both should start at the same time
Fast pointer should start at index one . because in the while loop we are checking . run the while loop until slow and fast pointer are not equal (while (slow !== fast)) so if both start at the same place i.e 0 . the while loop will not execute
@@mrmadhan8557 thanks now I got it
Nice video understood all concepts. Subbed :)
Glad to hear it! Let me know if there is anything you feel I can improve upon.
Thank you for the amazing explanation sir. I just changed the sequence of code for the HappyNumber problem in the getNext method and the runtime decreased to 0ms.
public int getNext(int n){
int totalsum=0;
while(n>0){
int d=n%10;
totalsum= totalsum+ d*d;
n=n/10;
}return totalsum;
} earlier as you explained , n/=10; was before totalsum calculation still the runtime was 1ms.
Nice work!
Hello, amazing video. Can you explain why the time complexity of cycle in linked list would be O(n). We know that if there's a cycle they will meet at some point, but that could take a lot of time in some cases, no? Thank you.
Still it would be directly related to number of inputs in a linear relationship. Hence TC would be O(N)
Great work
Thank you! Cheers!
In the last two pointer problem(happy number), how do we know when to exit the sequence creation and get to the two pointer check for loop?
In the context of a two-pointer problem like the Happy Number problem, the key is to understand when to exit the sequence creation and move to the two-pointer check. The idea behind the two-pointer technique is to use two different pointers (usually iterating at different speeds) to detect a cycle in a sequence.
In the Happy Number problem, you're generating a sequence of numbers by replacing a number with the sum of the squares of its digits. The process repeats with the new number. The two-pointer technique is particularly useful here for detecting if this sequence enters a cycle, which would mean the number is not a happy number.
Here's how you would apply this technique:
Fast and Slow Pointers: Start with two pointers, slow and fast. Initially, both point to the original number. Then, move slow by one step (i.e., calculate the sum of the squares of its digits once) and fast by two steps (i.e., do this process twice).
Cycle Detection: If at any point, the fast pointer equals the slow pointer, a cycle has been detected. There are two possibilities here:
If they meet at 1, then the number is a happy number.
If they meet at any other number, the number is not a happy number.
Exit Condition: The loop should continue as long as slow does not equal fast. As soon as they meet, you can exit the loop and determine whether the number is happy based on the value where they meet.
This method is efficient for detecting cycles in sequences and is a popular approach in problems that involve repeated operations leading to potential loops.
Thank you so much sir ❤ subbed❤
Thanks.
i have one question in happy number what if we run in infinite search where fastRunner != slowRunner
Infinite search is never always good idea, not sustainable solution I guess. But I have to see the question to really understand what are you talking about. I don't remember the question TBH
Happy number is Supposed to be medium problem
agreed
Thank you
You’re welcome
The number of subs u have isnt fair 😭
Hopefully they will increase. Till then I will keep on trying. Thanks
The flash🤣
🤣🤣