Before watching this video: I wasted first 30 minutes trying to solve it using loop =>RESULT - Got TLE I wasted next 15 minutes trying to solve it using IBH method learned in previous videos =>RESULT - Got TLE again in the same test case, but i was proud of myself that i was able to code it using IBH method so easily. Then i watched this video till 9:32 and then paused and then kept trying for next 45 minutes trying to observe any pattern and code it using IBH. =>RESULT - Accepted The level of understanding this man has given me in recursion is just exceptional. Thanks to one of my college senior bhaiya who recommended me this channel. He just told me that there is no other youtube channel that has taught recursion so much easily as taught by aditya verma. The only thing missing from your videos is time complexity and space complexity of your code. It's very difficult for me to find time complexity of recursive solutions.
@svastiishrivastava92915 күн бұрын
Have you find any video for time and space complexity
@LightYagami-ib6fb4 жыл бұрын
I like your style of asking people to stop and think in vital positions, helping them to think more deeply and not just mug learn something. Thank you for the efforts!
@prativadas47943 жыл бұрын
you are a blessing for many people like me. thank you for explaining everything in such a smooth way.
@mriit3025 Жыл бұрын
bro! it's simple and to the point explaination, first because of length i was going to leave, but the line you mentioned " Your observation is tested" ignited me to continue!..
@akashrajput37314 жыл бұрын
Thanks aditya for providing us such a great content on Recursion
@yuktikumari60424 жыл бұрын
I wonder how did you study these topics so nicely ?
@ananysharma92904 жыл бұрын
Yess Sir these are Amazing but we Seriously need graphs
@rick.exe12 күн бұрын
Bro how did you even come up with this solution.Mad respect only !.
@AP-bk8vq4 жыл бұрын
Great stuff! I'm following this series daily...Please try to upload GRAPHS next..there isn't much good content on Graphs on the internet and your videos would surely help us!
@abhaygupta87544 жыл бұрын
please make graphs simple for us. i know you can do that
@yasharora68182 жыл бұрын
Follow striver's graph series, it is brilliant.
@ajinkyakale4391 Жыл бұрын
Le Striver: Let me introduce myself
@shreya-tf5go8 ай бұрын
I solved this solution on my own and i was so happy .It happened all because of your teaching .Thanks bhaiya Hats off.
@Liftsandeats3 жыл бұрын
At 8th minute mark, I paused the video took me 30 minutes to code up a solution involving only recursion and a couple of if statements in Python. The previous videos build up this confidence in me to solve it before watching the video itself. Now I am gonna watch the video and see your approach as well!!
@thinkingmad16853 жыл бұрын
Did you code in paper or ide?
@Liftsandeats3 жыл бұрын
@@thinkingmad1685 paper for logic building and then write the code in vscode
@waka913 жыл бұрын
@@Liftsandeats can you share your soultion if it is different than aditya's?
@DroidHolicOfficial2 жыл бұрын
What an interesting problem. All about observation as you said! You explained the problem so well and also the observations, that the solution became so damn simple! When you explained the problem statement, i did notice that first half of next row was same as previous but didn't notice that second half is just complement of previous row. RECURSION IS LIKE MAGIC INDEED!! const kthGrammar = function(n, k) { //Base Condition if(n == 1 && k == 1){ return 0; } //Find the mid of length of the nth row let mid = Math.pow(2, n - 2); if(k
@the_humble_lazy3 жыл бұрын
thanks internet for making us meet with aditya's content where we watch for out betterment and thanks us also....recursion and dp god he is for sure
@VishalSharma-hq5ti4 жыл бұрын
In base condition, we can have if(N == 1 || K == 1) since, K == 1 for any N, ans will be 0 only so it might save a few function calls also in this case.
@nrbtreasure62402 жыл бұрын
Great!
@DroidHolicOfficial2 жыл бұрын
Or we can just ignore this K == 1 condition and just say if (N == 1) return 0;
@harishsn48662 жыл бұрын
This is by far the best explanation I found for this problem. Thanks.
@himanshuarora539 Жыл бұрын
When you said this question is only to test our observation skills, i gave this one more try and was able to solve on my own. Thanks
@saitrinathdubba2 жыл бұрын
Great Stuff !! Thank you !! People here is the alternative python solution, this relies on the if k is even then we will compliment the output other wise we continue, every time we reduce the value of k by half depending on if k is even or odd. Once again thank you for the great content !! class Solution: def kthGrammar(self, n: int, k: int) -> int: output = self.sub_kthgrammar(n, k) if output: return 1 return 0 def sub_kthgrammar(self, n:int, k: int): if n == 1 and k == 1: return 0 if k%2 == 0: # if k is even return not self.sub_kthgrammar(n-1, k/2) else: # if k is not even return self.sub_kthgrammar(n-1, (k+1)/2)
@mihirsharma23382 жыл бұрын
Or we can just typecast the return value with int()... Here's my code with little bit of change.... //// def kthGrammar(self, n: int, k: int) -> int: if(n==1 and k==1): return int(0) mid = (2**(n-1))//2 if(k
@ankittjindal2 жыл бұрын
if(n==0 || n==1) return 0; int mid = pow(2,n-1)/2; if(k
@himanshidixit64064 жыл бұрын
honestly sir you're a great teacher and your explanations are wow..never stop uploading these videos..they are very helpful..
@gauravraj26043 жыл бұрын
Hey @Aditya Verma, thanks a lot for this awesome collections of yours. Your every video is not just a video of any problem, it teaches us so many concepts in many ways that help us later when we revisit such topics again. You are a true gem.
@paragroy53593 жыл бұрын
Thanks for the playlist sir.....your channel is extremely helpful for the placement preparation
@kapilchoudhary29224 жыл бұрын
East or west Aditya boss is the best!!
@TheAdityaVerma4 жыл бұрын
❤️✌️
@amayatre35582 жыл бұрын
baapre kitna jyada easy bana diya question ko , hats off to you sir
@BECSAQUIB2 жыл бұрын
Great thanks from the core of my heart ❤️
@sanchitagarwal9651 Жыл бұрын
today i got the real essence of recursion. ...thanks bhai a lot!!!!!!!
@SDE_FACT2 жыл бұрын
I have no words for your explanation. Bass itna kahunga ... Aap ho to DSA me aag laga denge bhaiya❤️🥺
@namekuldeep4 жыл бұрын
very good .. Other way also mentioned below ...apart of recursion ... if (N == 1) return 0; if (K
@nikitajaiswal91122 жыл бұрын
Amazing 😍thank you so much for providing great content free of cost
@rajatgupta-ld1wg4 жыл бұрын
I did this question around 15 days before watching your video but wasn't clear about the basics.. Now it seems i can do this question anytime as you have cleared the basic concept. Thanks bro!. You are awesum as always.. :D
@studyacc12345 ай бұрын
if I got a penny every time Aditya said the word 'cool', I would have been a billionaire by now. hehe jokes aside - you are a bomb teacher!
@codertan41092 жыл бұрын
So wonderful explanation!!! Thank You so much for this Bhaiya!!!
@tusharagarwal34754 жыл бұрын
Bhai dil se bhot bhot dhanyavaad kya explanation thi bro.
@AngadSingh974 жыл бұрын
Kya sikha rahe ho bhai, thank you so much.
@tsupreetsingh4 жыл бұрын
Bhai Graphs par bhi bano do ek playlist , bohot help hoga !! 🙏🙏
@sayalikulkarni47174 жыл бұрын
It will be better if you mention the time complexity of every problem along with the solution. Thanks!!
@Vickerdeer Жыл бұрын
o(n) complexity for Time & space
@radium990Ай бұрын
++
@suraj_cricclub36302 жыл бұрын
Aditya sir your are legends ❤️
@044_karan94 жыл бұрын
Best playlist for recursion👍
@fenilbhavsar78924 жыл бұрын
God bless you brother. You are a pure soul.
@Lucifer_movies2 жыл бұрын
Nice and crisp explanation
@Anonymous-gl7gs6 ай бұрын
best explanation
@svworld014 жыл бұрын
Amazing explanation 💕🙏
@paritoshdadhich29543 жыл бұрын
Thank you for such a clear and simple solution
@neharikakhera64114 жыл бұрын
Thanks for such a great explanation.
@bharanidharan.m424511 ай бұрын
how to find the method for a problem that uses IBH method or ip-op method .
@pratheeksha6164 жыл бұрын
Great content, A playlist on GRAPHS please
@riteshgangwar7192 Жыл бұрын
Awesome sir, best playlist for recursion on KZbin,,plz plz , make playlists on trees
@sejalzambare18963 жыл бұрын
you made it very easy! Thankyou! great work!
@vedantgolash60844 жыл бұрын
waah yr ..maja agya aditya bhaiya
@AmanKumar-qz4jz Жыл бұрын
congratz sir for 2 lakh subscriber
@anmolsingh90844 жыл бұрын
Sir how to compliment the function in solve(n-1, k-mid) as m not able to do so. Basically asking how to NOT the 2nd half compared to the n-1 1st half.
@ujjwal8244 жыл бұрын
Simply use this: if(kthGrammar(N-1,K-mid)==0) return 1; else return 0;
@DragoonBlod4 жыл бұрын
Take the xor of the return value with 1
@rounakbajaj12733 жыл бұрын
@@ujjwal824 thanx bro..
@pawanlok17762 жыл бұрын
** Thank you , Observation is quite important to solve this question
@mutyaluamballa3 жыл бұрын
man this is so cool, the moment you asked me to pause and asked us to observe. I got a different answer. int kthGrammar(int n, int k) { if(n==1) return 0; int k_ = ceil(k/2.0); // its the previous index of current k, when n-1 int prev = kthGrammar(n-1, k_); if(k_ == k/2) return !prev; return prev; }
@endlesslearning263 жыл бұрын
Bro same here But I just complicated it a bit intially Induction step:- Val=find(n-1,ceil(k/2)) If ((Val+k)%2==0){return 1} Else{return 0}
@thinkingmad16853 жыл бұрын
Can u elaborate what you did
@shreya-tf5go8 ай бұрын
can anyone please suggest good resources to learn 2 pointers?
@bambu-mq9zg2 жыл бұрын
my approach def kthGrammar(self, n: int, k: int) -> int: def rajeev(n,k): if n==1: return 0^((k+1)%2) return ((k+1)%2)^rajeev(n-1,(k+1)//2) return rajeev(n,k)
@SadhanaSharma Жыл бұрын
explicit explanation .. Thankyou sir!
@037_abhinavkumar3 Жыл бұрын
What's the induction condition in this question?
@rohitnandagawali15896 ай бұрын
int kthGrammar(int n, int k) { if(n==1 and k==1) return 0; //this is given in question int mid=pow(2,n-1)/2; //pow(2,n-1) gives length and '/2' helps to find mid if(k
@ganeshjaggineni40977 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@souravkumardas89052 жыл бұрын
best explanation ever.
@shibanidas70183 жыл бұрын
Bro I could solve this before watching the video by following your series, Not only u solve these problems but teach us HOW TO THINK U r god !!!!
@abbas.muzammil233 жыл бұрын
Time complexity: O(n) right?
@RonitSagar Жыл бұрын
Can anyone tell me ....which value is returned to the calling function in recursion. base condition or other part(hypothesis/induction)
@MalobikaNandy26 күн бұрын
really good video.. thanks a lot
@Akash99D2 жыл бұрын
What's the induction step here? could anyone point out.
@nidhigalmale1123 жыл бұрын
Python Solution: class Solution: def kthGrammar(self, n: int, k: int) -> int: if k==1 and n==1: return 0 mid = 2**(n-1)/2 if kmid: return (self.kthGrammar(n-1,k-mid)^1)
@karanbisht63592 жыл бұрын
thnks ..... i got little confused on complement part !!!
@piyushmishra8892 жыл бұрын
sahi ho bhai tum, naam suna tha , dekha to acha laga
@shikharjoshi29986 ай бұрын
btw what was the induction step in this code?
@maniish-sgh4 жыл бұрын
Great work buddy🥂
@arjunankathattichandrashek19973 жыл бұрын
very beautifully explained!!
@akankshasaini37793 жыл бұрын
please end me time complexity bhi btaya kro, recursive functions me dikkt hoti hai
@kailashjangir15303 жыл бұрын
bhaishab magic tha ye to bilkul magic
@asishcodes2 жыл бұрын
Can someone please tell me which part of the code is hypothesis and induction
@ElectronToInterface2 жыл бұрын
def kthGrammar(n: int, k: int) -> int: if n == 1 and k == 1: return 0 mid = (2 ** (n-1)) // 2 if k
@cse077mukulrana Жыл бұрын
We can also done this using XOR of string and add to the end And after building find k element simple Time complexity would be O(max(N, K)) Space complexity would be O(N) And the second method is what you told here I guess
@rohitjoshi63354 жыл бұрын
This question was great!
@satyamagarwal03023 жыл бұрын
what will be the time complexity of this code?
@SalmanKhan-vu4bk4 жыл бұрын
Please upload video on graph 💓💓
@adityapandey52644 жыл бұрын
Excellent explanation.
@neo95302 жыл бұрын
I understood how it is working but I am not getting why it is working? If I take an example say(3,3) and back trace it from the base condition to first function call. It is giving me right ans bhut why it is giving correct ans? That i am not getting. Plz can someone explain.
@aspiring_Sr.dev_Om5 ай бұрын
we can also find mid by :-> mid = pow(2,N-2); , jst simple maths
@shubhamjha57382 жыл бұрын
can anyone explain time complexity of above soln.
@biswajitsahoo14563 жыл бұрын
how can u compliment by just putting a ! sign in code plz can u explain it or share a link that will help me
@faheemuddinnaseem3972 жыл бұрын
perfectly explained i love it
@subhajitdutta52162 жыл бұрын
Can't we use DP in this problem?
@ashutoshshukla53982 жыл бұрын
The time and space both would be O(N) for this. Right?
@pinkkitty6553 Жыл бұрын
The java bitwise not operator (~) might fuck up your code logic, as it also changes the most significant bit which denotes the sign of the number Use ternary operator instead Return (solve (n-1,k-mid)) == 0 ? 1: 0:
@atharvsakhala94693 жыл бұрын
Can you prove by induction that the relationship you found actually holds between next rows?
@bijendersingh81413 жыл бұрын
int kthSymbol(int n, int k) { if (n == 0 || k == 0) { return 0; } int symbol = kthSymbol(n - 1, k / 2); return ((k / 2) * 2 == k) ? symbol : !symbol; } call => kSymbol(n-1, k-1) works for larger inputs.
@bijendersingh81413 жыл бұрын
final return statement can be changed to return (k&1) ? !symbol : symbol;
@vedantshinde37172 жыл бұрын
What change should be done in the code to print this grammar instead of getting a single bit ?
@AnuragSingh-jq6jn11 ай бұрын
I wonder how did you study these things so well on your own because during your Btech youtube barely had any resources.
@yashshimpi30932 жыл бұрын
k/2 bhi use kar sakte hai na 🤔🤔
@sandipan_c3 жыл бұрын
def solve(n,k): if n==1 and k==1: return 0 else: if k%2==0: return 1-(solve(n-1,k//2)) else: return solve(n-1,(k+1)//2) print(solve(4,8)) this is a different approach.
@angadrajsingh43114 жыл бұрын
Amazing brother!!!
@susmitjaiswal1362 жыл бұрын
faster than 100% submissions...aditya verma i loved your series int kthGrammar(int n, int k) { if(n==1||k==1) return 0; else{ int a=kthGrammar(n-1,(k+1)/2); if((k+1)%2==0) return a; else return 1-a; } }
@sohamnandi7526 Жыл бұрын
can you please explain this solution.
@chandanbharti5207 Жыл бұрын
@@sohamnandi7526 The given code implements a recursive function called `kthGrammar` that returns the value at position `k` in the `n`th row of a grammar sequence. The grammar sequence is defined as follows: - The first row is always "0". - For each subsequent row, each element is created by taking the element at the corresponding position in the previous row and replacing it with two elements based on the following rules: - "0" is replaced by "01". - "1" is replaced by "10". The function uses recursion to calculate the value at position `k` in the `n`th row by dividing the problem into smaller subproblems. Here's a breakdown of how the function works: 1. Base Case: If `n` is 1 or `k` is 1, it means we are at the first row or the first position in any row, so the value is always 0. Hence, the function returns 0. 2. Recursive Case: a. The function recursively calls itself with `n-1` (moving to the previous row) and `(k+1)/2` (finding the corresponding position in the previous row). b. The result of the recursive call is stored in variable `a`. c. If `(k+1)%2` is 0, it means `k` is even. In this case, the function returns the value of `a` as it is. d. If `(k+1)%2` is 1, it means `k` is odd. In this case, the function returns the negation of `a` (1-a). By using recursion and dividing the problem into smaller subproblems, the function effectively calculates the value at position `k` in the `n`th row of the grammar sequence. Please note that the code assumes `n` and `k` are positive integers.
class Solution: def kthGrammar(self, n: int, k: int) -> int: # base case if n==1 and k ==1 : return 0 length = 2 ** (n-1) mid = length / 2 if k mid: return 0 if (self.kthGrammar(n-1,k-mid)) else 1