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.
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.
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
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 .
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.
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?
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.
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)
@@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); } } } }
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
Bro you are really underrated I saw a lot of people having more subscribers but they simply waste time. You are spot on
Man, i really found the alogrithm treasure and its YOU, thank you bro
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.
Yes, exactly
sir you are really amazing , your teaching style is perfect
Your explanations are very good.
Keep up the good work!
Can u please make a video on Leetcode problem 29( Divide Two Integers)
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.
@@nikoo28 did you made that video ?? if yes please share the link
@@JohnWickea How to use LeetCode and pick problems to solve effectively in #2023
ua-cam.com/video/PcolzuB-fRc/v-deo.html
How is the space complexity O(1) if we have used an extra space by creating a HashSet? Can you explain?
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
I did not understand, why is it guaranteed to have a cycle if not summing up to 1?
Thanks bhai
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 .
Sir,
Can you make more videos on bit manipulations ?
Like xor,And ,Or...
How to use this efficiently and when to use?
I will soon start a segment on bitwise operations.
Nicely explained, baba!
thank you sir
i understood clearly
hi! I believe the Time complexity for the solution is O (log n ) not O(1), could you double check that?
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.
can anyone explain why is it guaranteed to be cycle? why cant it not continue till infinity ?
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?
Can you please explain while(true) statement?
that just means an infinite loop.
Can you make a video on leetcode 120- triangle. ?
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.
leetcode already imports some libraries for you
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
both are perfect to use
Sir pls make interview based problems
I try to cover interview based problems and most liked problems on LeetCode. If you want any specific questions, let me know :)
@@nikoo28 sir pls cover linked list,stack,que,recursion quesns
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.
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)
@@nikoo28 yeah thanks you so much sir
Great!
Thanks
Thanku sir
keep going but please the voice is not clear enough, try another microphone
It is fixed in the newer videos. :)
@@nikoo28 ok thankful I'll watch them
why is while(n!=0) instead of while(n!=1)?
Do you understood now? If yes, please tell me. Thanks.
@@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);
}
}
}
}
we are using hashset here . doesn't it will be taking extra space???????
it will be constant space. watch at 10:15
@@nikoo28 thanks for answering
Nice👍🏼
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
Sir please make video in hindi as well
op explanation
please use good mike