This video demonstrates effective strategies for solving competitive programming problems, including binary search for optimization, sparse tables for efficient range queries, and rolling hash for subarray identification. The presenter emphasizes that consistent practice, starting with basic problems (Codeforces 800 level), and learning advanced data structures like sparse tables through hands-on implementation are essential for improving competitive programming skills.
Approfondir
Prérequis
- Pas de données disponibles.
Prochaines étapes
- Pas de données disponibles.
Approfondir
Leetcode live problem solving | GuardianAjouté :
All right.
So, what should we do?
Hi Rohit.
So should we start with the virtual contest?
Everything's good. How about you?
I guess should I start with SQL first or DSA because I'm going to do both.
You was waiting. Okay, then let's just start with the virtual contest.
every pair of adjacent digits is at most two otherwise return false.
I mean just loop from 1 to n let's call it zero and uh right so max is going to be let's change the time maxi is going to be max of maxi comma absolute difference between s of i - s of i - 1.
There we go. Um what if at most two? Okay.
So if max C is less than three then return true. Else return false.
Right.
So the second problem we are given three inteious lr k integer y said to be a perfect k power if there exist an integer x such that x to the power k is by x to the power k is given what is lr so number of inteious y in the range that are perfect kith power.
Okay.
Can you move your head? Yeah. Uh sure.
Question might not be visible. Is this okay?
Yeah. So basically the question is in this range we have to find how many number of uh let's look at this thing. Oh so the constraints are pretty big. So we cannot iterate from L to R.
Okay but K is small.
So we have to find integer y such that there exist an integer x.
So x to the some power k h we have k right?
If k is 1 then it is just so if k is 1 right then it's just like y = to x there exist an integer x then in this case the answer is going to be r - l + 1 Okay.
There exist an integer x.
If this is two then so log by is basically we are saying that it is a into log x perfect cubes. What is the expected time?
Huh?
We have to do this faster.
X can be anything.
A equals to 2 then what is going to happen to the range? So there should be some integer disa cube should exist there.
So there are going to be like some range of x that is valid. Okay.
So there will be all right.
For example, here we have to look for cubes. So valid range is going to be 2 to the 4 to 8. It's probably going to be three, right? So we will check 1 2 3.
But we could have like very large l and r. l could be zero.
Then we could have two.
So the answer is going to 10 ^ 8² is 10 ^ 16.
Nope.
Perfect squares.
It's like 8 to 30.
Six is going to be upper bound.
Right? By the way, what is a lower bound? If we know lower bound and upper bound, we can find how many numbers are in between.
So you know this question five wrong answers. No worries. We are going to decipher the question properly.
Okay. See there's going to be some range of x.
So if we know if we know the lower bound and upper bound okay like the lowest point then we can then we can do just upper bound minus lower bound probably + one. So we just have to find like the maximum number that is going to give some power k under this variable. But how are we going to do this? We can binary search, right? H pretty good.
There will be one more.
I'm absolutely not interesting interested.
Not excited at all.
I think this is going to be the answer.
Just find lower bound and upper bound.
There we go.
And uh you got no choice. Oh my god. Okay, we'll see about that.
We'll see about that.
So should we just try to implement this?
How will I bind research? I can binary search.
So see x is not going to be greater than it is 10 ^ 9. I thought it's 10 ^ 19.
Now we can technically iterate over all the possible things because see if k is 1 then just return this r - l + 1. Okay.
If k is 2 then you will have to check till what is under root of 10 ^ 9. That is it.
Okay. So this is very easy.
So if k = to 1 return + 1 else what we are going to do.
But how do I um how do I treat um probably zero maybe we have one so Yeah.
How do we this?
So we will do this until this number this is this is a function right until this number i to the power k is less than or equal to r. Okay.
Is this is this fine?
I think.
Yep. If Wait a minute. Do I really have to iterate?
Do I really have to iterate? Maybe not.
I can also find lower bound, right?
If num is greater than equals to L and N is less than equals to R.
Okay. So we are getting right answer but I'm wondering like see if we're really worried about time complexity we can use uh binary exponentiation here that is also something but we don't have to iterate we just have to find a lower bound and upper bound to optimize this.
Should I try to submit?
It works. Why you guys are getting wrong answers? I don't get it.
Right.
Maybe I'll get T if I don't use this.
Okay. Because if k is greater than one then the range is going to be at max 10 ^ 4 5 something.
Were you guys able to solve this problem? Rohit and sharif.
It took me some time because I thought this is 10 ^ 19.
You know I got T when I tried to use this solution. Exactly. See I thought about this like first if this is the first thing that I thought if K is one then every single number is going to be the case because then you will iterate over all the possible 10 ^ 9 values which will give you TLE apart from one you won't get TLE because anything two or greater than that the range is going to be at max roo<unk> 10 and roo<unk> 10 is like around 10 ^ 4 to 10 ^ 5 in Okay.
So, you're okay with that.
Okay. Let's move to the third one.
All right. You are given n into mat integer matrix non- negative okay positive integers nonzero cell and I summit with lower and upper bound in context.
So you did like binary search with the lower and upper bound that is going to be like a more efficient solution.
We could have limits like 10 ^ 19 and 10 ^ 19 then still like we can do it with binary search I believe.
Non-zero cell checks the cell near it follows.
Non-zero cell checks the cell near. Let's feed it.
Consider every cell within X rows and X column.
H. So, this is going to give me a range.
Okay.
But how about this radius?
Ignore the cells that are outside the matrix. Of course, ignore the cells with both the row distance and column distance are exactly X.
Distance and column distance are exactly X.
Okay, this two and two, right? So this cell has to be ignored.
So we have like some sort of a box you know h the cell is a local maximum this is a local maximum. If it is non zero and also they can be 02 and and no considered cell has a value greater than x.
Okay. So it should not be zero. And in all this range the maximum value of all this range should either be equal to two or less than two. Equal to two of course. Yeah.
So basically maximum should be um two. So how do we do it? This looks like some sort of a window prefix suffix kind of a thing.
For every cell I have to maintain this window like both horizontal window and vertical window probably use some prefix suffix here.
So how do we get okay let's think about it. So if we are at this particular node two how do we get maximum can we use like what is the expected time it's n² and cube Maybe wait. Uh, so this x number is also dynamic. It's not fixed.
Okay.
This is also limited but like how do I get this thing this whole thing and I have to do this like efficiently Okay.
This question you tried brute force which gives T. Of course it will give TL if you want focus on optimizing the solution.
I mean your solution the I mean it's very easy to brute force it right you just if you're here you just go here go here every possible direction but I'm thinking of optimizing it how can we efficiently do it Uh uh uh uh h this question is a little bit different values are also limited.
So what happened?
What's the fourth one?
Given an integer array nums minimum length of sub that is not identical to any subarray. Return an integer denoming the minimum possible length of such a subarray of such a sub.
Summary is a cont sequence of elements and the same elements.
How was about prefix maximum?
I'll think about it. But like prefix max.
This prefix max work prefix max is not going to work because we need a range, right?
We need a range.
So we have to find the the smallest length subarray.
that is not identical to any other subs if they have the same length and same elements. Okay, identical means same length and same element. Okay basically I mean same.
So here, yeah, two can't be the answer because already have it.
2 one one is the answer because we don't have one.
But how do I find like oh nums are also limited.
So this tells me we can use like frequency thing.
How can we make sure?
How can we make sure that particular array is not identical?
We can always start with like a whole array, right? That can always be the answer.
1,700 people solved it.
That's crazy.
same length and same elements.
Press fix max work.
I mean I can use segment.
Should I use segment?
Solution will be like um n into n into n login. It is going to pass or maybe not.
Uh, well, you read wrong. Not 1700, but it's 1,700 + 1.
Wait, what?
We can do like prefix and suffix but we can't get for a range.
But this cannot be a segment problem.
Now this is a medium.
cannot be a segment problem.
If I was in a live contest, I probably would have implemented it.
Couldn't learn it.
I mean, segment is not optimal, right?
What could be the possible solution I mean you need you need maximum in a range in this problem you just need it it's a sparse okay I know sparse tables too so you can do this like in big of one with this sparse tables but I don't expect to use a sparse table in third problem that is my argument And apart from using sparse table, I would just use segmentry then because login will also pass.
Yeah.
Will it will pass?
Let's think about this one.
So on Yt everyone using sparse table it makes sense you need like maximum minimum you don't need like updates. So just go ahead and use sparse tables.
It's a good idea.
The horror horror tubs to learn RMQ. Yeah, maybe like you need more time I would say because like sparse table is what? First of all you like so what you do you store values in range of like 2 to the power.
So you would have something like n into log n size of your DP and then for max you can you can find like a from L2 you need to find some power of two that will give you maximum power of two that will give you like range less than or equal to you know right then like vice versa for R and why this works in big of one because we don't care overlapping thing right so if we have some array here can explain it a little bit let's say you want to find maximum from here to here right so we don't care about like overlapping things so you can like start let's add one more element so you can start from here right and we are storing like range in 2 to the power right so we will get 2 ^2 is four so we will get something like from here to here we will get maximum whatever is max then from this r we can get something like this maximum in this range right and the maximum of both is ultimately going to be the maximum of whole range but in terms of addition this is not going to work you say guide to get good at this phone.
You can definitely go ahead.
I would definitely recommend you.
I believe I should also have like a template for sparse table.
I can use segment though.
Should I implement like sparse table? It is going to take some time. Should I do it?
This is very similar to binary lifting entries.
So my base case will be 2 ^0 which is going to be the element itself.
Then for every single element I will first go to the element. Okay. I mean same as binary lifting. Yeah it's crazy. I also think about it. I mean I know about sparse tables. I learned about it probably in October previous year.
So it took me around five months, five to six months to learn these advanced data structures and also to be able to understand them.
Now I'm feeling very lazy to solve this problem because we know the solution already.
I will think about this one for now.
This one is interesting.
I would honestly in like a third problem that is which is medium I won't think of using like segment or sparse table but this is what you need here.
College professor nothing bro you learn everything by yourself I mean I did you can do that too it's all like YouTube CP algorithms even when I learn like something else my core subjects or whatever else it's all like YouTube and some other websites driver sheet.
It is going to take some time. Yeah, do CP31 sheet. It's it's pretty cool.
The tips are just the best tip I can give you is just it is going to take some time. Okay, it's going to take some time. Just don't leave it, right? Just be consistent. Make sure you are solving at least a problem a day.
Doesn't matter which platform. It's not like you have to do do only lead code and code whatever you want like wherever you want to solve you can do it.
Also like for these kind of a things for example if you're learning sparse table make sure you implement it yourself so that you get a very good intuitive idea and you can solve problems on CSCS problem set I did around like 250 problems there probably 200 probably less than that but around like 200 the only thing I don't know are like few advanced techniques like convex hull uh maybe DNC I know a little bit of DNC and HLD maybe light decomposition you should not you should not be looking at my growth okay everyone is different focus on what you can Minimum length of a submar that is not identical to any other subarray in numbers.
This is a very unique problem actually.
What could be the possible thing?
And I have to do this in Um, I can probably use some sort of a map.
I can take the hole and then I can try to reduce it.
But then maybe I can start with a hole. Then I can try to reduce. If two elements are different then we are pretty good.
Yeah.
No.
I'm still in college. I'm in third year right now. Um I have been selected for like a interview in Infosys L3 offer but I'm still yet I'm waiting for the official email for the interview date.
I think it is going to take some time.
It it might be next month.
I think currently they are interviewing people for like DSC, L1, L2 roles. I believe it's a very unique problem.
Same elements in corresponding positions. Same position as well.
Can I do binary search on length?
So I know like for sure this is going to be unique or anything like the whole size upon two + 1 is also going to be unique.
After that there might be issues, there might be duplicates if I have to generate like all arrays.
So let's say generate all all array of some length K.
But I cannot do that.
It will numbs are also limited.
So probably won't really It will take to go of N.
And then finally search on K 2027 batch. Yes.
You did not give me life.
What do you mean by that?
Can we do that?
We have to BS from one to that.
Oh. Uh this will not really this is okay.
We're going to have something like this.
Then this is going to be okay.
Maybe maybe maybe let's see r equals to right.
Is this correct?
I think I can do.
So, we're going to check for this mid.
If this is true, if if there exist something unique of this length, so we only need like one index that is unique.
Okay, we can use map for that. Okay. So if this is a case then else make L make + one. Let's initialize answer as well.
H n is fine.
Okay. Let's implement this check function.
It's going to take a variable x and a vector by reference.
Now I will use a set.
This set will contain some map.
Let's use a map.
We can't do this.
We will have to use like actual map.
We can use a string. Okay, we can use string maybe.
Okay, this is going to it's going to count frequency of each one of them.
Let's quickly form a string.
zero I is uh less than X that is going to be the len let's do push back this X nums of I plus zero okay good so we have one first subarray Right.
Wait. This is going to be bad.
Is there any Is it possible?
I will push one element. I will pop one element.
It's a string. Fine. Should I use something else?
Something else like a DQ might be helpful, but storing GPU is going to be expensive.
Let's use DQ for this.
Um, so just to the string, it's going to have some character.
It's front, right?
Okay, we have pushed everything to it.
How do we take everything in our DQ?
Can I check the unique popping operation is big that is not helpful.
I mean, let's just try something I can use uh probably Okay.
Frequency of this dq ++. Now let's iterate it starting from x to a whole.
So what do we do? What is going to be L?
We can just pop it.
Let's pop back. Okay.
And DQ dot push front.
lums of pi + 0 and frequency of dq ++ for um first we're going to have a dq we don't care about it we just care about value right so if plus= to one.
Return true.
return for this.
Okay.
Where is R? R equals to nums of size line 30.
Oh, everything is undeclared, bro.
Oh, we're getting three. We're getting one.
We're getting two. Okay, the answer is correct. But is this optimal? This is what we have to think about. Is this optimal? Is check. There should be a better way to implement it. No.
So it's like n log of login that is going to work.
But can I optimize it? Because T of course because of this.
Let's focus on optimizing it. Okay.
Pretty close. We're pretty close.
Oh, this is actually taking a lot of time.
A lot of time by the way, what is going to be the side? It's going to be K into N at max.
So, K into N, we can do it.
Okay, n² not possible. But what the But if it is n then we are only going to have a single one of them.
Maybe something is possible.
How to not use this dq? Can we use a vector something else?
character.
Wait a minute. What? Num suffice.
This is not of character but like Now we get memory limit. Okay, this should be optimal way to do this.
This binary search is fine.
No issue with that.
We have to wrap this up in Okay.
How do we do this?
So when we are moving to the next position, we're just including a element and removing one.
What is a possibility that Should be some optimization here.
But how will I remove element from guys? You can give me some hints if you have solved it already.
I just have to find a unique I mean this is also incorrect. No, wait. There could be a lot of them.
We'll have to make changes here.
Can you arch?
One, two, three. Okay. So there can only be n possible nus for a beginner. How to start?
You can start like from very basics.
You can start solving code forces 800 level problems.
Just make sure you are comfortable with the language.
Right. First learn any single language.
Learn how to use loops.
Learn like some STL templates and uh yeah can use code forces lead code. You can solve sheets. You can solve this driver sheet.
But like those sheets. It's not like if you will solve it then you will be able to solve problems. That's up to you if you're able to pick up concepts.
But sheets are going to be helpful if you're a beginner.
Just have to do this in big of a pen.
I'm not able to do it.
We just need these two loops. That's what I know.
Wait. Something here.
Something here.
But how do I check this thing?
We can't use set.
We can't use set.
We can use set.
Probably not.
Where did you learn coding? I mean I do I solved a lot of problem only port courses have also made a lot of pro projects there's something I'm missing like single point.
Check unique summary of some length.
Why does it Why does it because of DQ?
This is not going to work right. It requires hashing. So I think like integer integer or probably a string is going to be fine.
So, I should ask GPT.
How can we find whether there is a unique subarray of length x in like log in like big of I'm still in college. I'm in third year right now.
Should I try more or should I just ask GPT?
That's the question now.
same elements in a in corresponding positions.
There should be a way to check. No, if the length is two, then what are the possible? This one this one can only be equal if right these previous are equal try more probably let's think more maybe there is something I'm missing and I can optimally do Now this can be equal to this only.
If this element is equal to this Let's increase the size.
Let's say this is the length we are checking. Now this can only be equal to this if the first element is equal at at least like first and last element is equal.
But we have a lot more, right? we have a lot more.
So this starting with one can only be equal if there is some position that also starts with one following by consist of same elements.
But like for this to be equal, this is the second element now, right?
This is third element now. before it was fourth.
Wait a minute.
Maybe I don't have to check in this window now, right? Because it is only possible if this this is equal and like corresponding elements are equal, right?
Okay. So before this position was like first position, second position, third position, fourth position.
After I have shifted them by one. Now they have become something like this.
Okay. So I need to check this is not equal. First thing I need to check this is not equal to the next element.
Wait because this is not equal to the supplement.
If any one of them is false, I can start new window from this position.
That is going to be very efficient, right?
Maybe I can do it much faster.
What level of coding is this?
If you're just starting, then this is going to be pretty hard. This is a hard problem and like a very unique problem.
If you're just starting, you will not be able to solve this.
Wait, this should be equal to this.
This will be two. Wait a minute. There is a pattern. There is a pattern. This should be equal to this.
This should be equal to this.
So in which case we are going to have something like that. Let's think about it.
Is this the only case?
What if we have two here?
But like this is unique now. So you can possibly start Please give me a washroom break.
I'll be back.
Okay.
Which language? This is C++.
I think I should probably GPD.
Probably GPD because I have no idea how I can do this.
What is going to be like space?
So if we have two total are going to be like n minus k + 1 number of arrays n minus uh k + 1 into k and k can go till n.
If k is one then we have like n If k is like n then we will have 1 into n. So why the why I'm getting like memory exceeded error here?
It's probably because of dq.
I just have to think about fixing this thing here.
What if k is k by 2 nx2?
We will have n - n /x2 which is again nx2 + 1 into n /x2.
So it's like 2 + n /x 2.
Let's remove one. We are going to have n² by 4.
- n /2 is n / 2.
This might be a problem.
This is n² by 4.
Count of elements. Check how many duplicates exist.
Does there exist a sub of size X where all element distinct? This is not a problem.
Um, repeated sub. Yep.
Raven car. But this is going to be an square.
This is not big of n hashing is going to be bad.
Collision safe hashing double hashing pure STL version.
Does this actually require hashing?
Oops. Oops.
Really?
I do not expected to use hashing, but like I was spot on with binary search.
Somebody did it. Wait suffix tree.
Everybody did rolling hash a string hash plus binary search.
Um I mean that is people that is peop.
So it is hashing.
If I'll do like something like string, this is going to work, right?
What if I don't iterate here?
for the test case.
H let's try to convert this into unordered map.
But how are we going to hash a string now?
Hashing an integer is easy, but like string hashing.
Let's do this one.
Uh we don't need updates.
We don't need like half of these things.
Let's do one thing. Let's implement um the sparse tables by ourselves.
So what is going to be the value of n?
This is going to be log of whatever is the maximum value in this whole matrix.
I can take it like around 200 Is this a correct function?
What is m?
Give me five.
64 to the how much?
Still C++ log.
Uh so this is a natural log. Log two is fine.
But like we need less than this. What is 2 ^ 8?
That is going to exceed. So we are good.
Let's create a vector.
We're going to store integers in it.
Let's call it table.
And the size is going to be this is a square matrix.
I'll be storing this columns So sto size is going to be m and what size of this is going to be n right initial values I will have to iterate So, wait, what?
Oh, it's going to be a 3D back there.
So this vector will contain the index of a row where I will implement this thing.
Okay.
So we're going to have like n M and N. I guess that's enough. Let's iterate over this table.
So let's go to a particular row and uh let's go to all the columns and let's initialize base case for this nth row m column and to the 2 to the^0 that is nothing but matrix of n and Okay, this is the base case.
Let's make the table now.
Okay, so again we have to go through everything here. We have to iterate.
All fine.
Start with one. Um, it should be less than n.
Just in case ++ k and here we have table of i comma j comma k. It should be equal to should be equal to what? Um table of i. So now we are in in the territory of SP table. So this J is a particular node which has a length minus one and here we have to use table actually table of I J.
Okay.
So from this particular node you are saying what is k minus one distance.
So for one k0 is going to be the element s.
So I will get the element.
Wait what?
That's wrong.
How do I build it?
So for one I have to go to the one first.
So should I start from smaller length or should I start from big length?
base case should be the next element.
Actually, it is incorrect.
Right? And once I know that what is maximum out of those two then I can do it possibly I actually need this is for zero. I need um maybe so this is going to store these two these two right let's go till M minus one table I Z is going to be max of we need max I comma matric So I J + 1, right?
So the zero is basically 2 to the^ 1.
We can also say like 2 but that is okay.
Now we can use this info here.
Same table. First we go to k minus one.
So what is one distance like what is the maximum over there?
Then we max it with the current one.
Okay. So this is absolutely fine.
this comma table of i, j and k minus one. Okay.
So if we have length two here to here.
Yeah.
Then we also have this.
So length two is going to be how much?
Maximum of this and maximum of this.
Wait.
Okay. Fine.
Are these bounds going to be fine here?
Because what is this thing?
Let's initialize it as can be two different initialization.
Let's do something.
Okay. Now we can check actually whether this is true or not. So that if it has been formed go from zero to n should have also stored like Okay, fine.
Let's see.
What? What happened?
Out of bounds.
This could be out of bounds. Oh, so this is going to give us not the node but the maximum.
Well, this is bad.
How do I get to the node first?
Maybe I can use J plus log some right?
H I can do that.
uh so if I'm here so four is done now I want this thing over here which is this index plus this four.
It is going to be to the power. Okay, let's do this.
So, this is going to be 2 to the power k minus one. Okay.
But then this should be in range, right?
So let me It's not an integer. This will return me integer.
J + P.
Let's do J + P.
We're getting something, but this is Um, I mean, we have zeros.
So we're printing K first.
We have I check with a space.
I break.
Okay.
Did you play chess? No, I don't.
Okay. So, we have a very good answer. So we have everything zero and this is s2.
So this is uh one equivalent. Now we are going to a territory of this thing absolutely fine because we don't have anything after it makes sense.
No issue with maximum as well. So for this two for this zero the maximum at 2 to the power I mean one length is two. So this is a good one.
This is for four. Okay. So we made the table. Okay. The table is correct.
Now what we have to do is um we need to get max. Okay, let's create a function or maybe just create a function inside.
Okay, get max uh I'm going to take a range I will be having.
Let's make them print.
So these are the ranges that I have.
Okay.
So now we have to think about how how we do it. So from this L I will go to a point.
So I have to do log of that particular distance.
Yeah.
Right.
difference is R minus L.
Now I have to go to L + log base 2 of this difference. Okay.
Okay. So difference is four. So if I'm here and let's say this is what I'm finding and the difference is four.
Then the value of K will be what?
Less than this. Okay, we don't want to include it.
It should be like four which is going to be two but huh that is fine.
Okay, this is for the first one. For another one, it will be what?
I have to do like R minus So we got this length. Now we also need to find this one right. We can choose the same K. By the way, we just have to go like K back from R. Okay.
So first L is same.
Let's keep it capital R and we will go back from R minus I guess right now this is going to be max of okay we also have a row here let's not forget and then we start with L first and K is going to be the same and then in a same row we will start with this capital R with the same K.
That is it.
Let's try to use this. Let's see if that works.
So, get me max.
Third row.
Okay. Third row.
Uh let's say 01. Okay.
Let's see there is some show invalid operants to what what I did not return.
Okay.
Oh, some issue. It's going out of bounds.
row is fine. I guess it would do like how it can go like out of bounds.
This is going to give me one less or equal.
We have 0 1 0.
So I did like 0 to one. So here to here.
Okay.
So I have this will start from zero and k 0 is going to give me zero. Fine.
Then r is one. So we start here with one and again zero. So I should be getting zero. What's the issue?
It is absolutely fine.
LRK 010 is a valid index.
It should not give overflow.
Maybe something is n rows m columns capital n is also isn't capital n is just 013.
What What is the problem?
What is the issue?
All the indices are accessible row right is accessible. Oh, there might be some other problem. Oh, let's reset the test cases. There we go.
I put any corner.
Now we have uh we have correct answer zero.
Let's put three here.
Okay. Wrong output 03.
Let's take a look.
So the index is zero. Then we have one which is 2 ^ 1 I believe. Yeah.
These two things we're getting. Here we should have two, right? But we have one.
Okay. So this should be two. So we should do + one here.
But why is that happening? Because we are going back that distance.
Makes sense. Makes sense.
But it may cause problem.
What about one? Now this is zero. So r - 0 + 1.
Maybe I can go back to L and then I can do it.
Okay.
So 01 the difference is one I should have one. Okay.
Yeah.
Wait, how do I fix it? How do I fix it?
So, I want difference to be L - R + 1.
Okay, I think that fixes it.
Both are at zero one distance. Fine.
This is also good. Now 0 1 2 3.
This is wrong.
Wait zero case to all this. Then I started from here and I'm going back.
It should give me zero again.
No, I'm getting one.
So this r minus whatever is log difference is um 3 + 1 4 right log should give me two so r - 2.2.
must be some solid idea to do this.
I can start Start with L, right?
Always start with L and I can put till R. I can include R.
So maybe I should not include K1 is storing this, right? Wait.
So 2 to the^ 1. Wait a minute.
2 to the power 1. Okay. one is storing like this thing 2 the^0. So k1 is 2 ^0.
So the issue was with k actually I'm I'm getting confused with the indexing. Oh my god.
Okay, it could be any number that X. Then from R I do the same thing.
It could be like anything till L and I subtract it.
All right.
Okay.
So, am I thinking of K minus one?
That is uh to be fine.
Yeah. um starting from zero and k is what?
Let's print k minus one.
Now this is correct. Okay, starting from here we are going till two and we're starting from here and we're going now this is going to give me good answer every single time I can try 2 3 4 5 this should be some good example but like this is working now.
Oh, I got some undefined.
No, there are six terms.
So, we have four 55 L is four.
We did four and six.
We're finding for this. We're looking for this.
So, we have four and five and both should start from zero. K should be case is five.
Why am I doing L+ and K?
If you're getting zero, this.
This should already be two. What is wrong?
What the is wrong?
M one and then K is two.
Is my indexing all right?
And and and and uh what is wrong?
Index three out of bounds.
this box.
But what is two?
Oh, it's four. No, no, no, no.
2 is 2 ^ 1 2 distance.
What's wrong with three then?
Oh my bad. My mistake. My mistake.
She should not be here. Um, it should be there to do the to do the forward.
Um, this is three. It should be four actually. Why is this three? Because I get it. I get it.
Because of the way I did the whole indexing, this causing problem.
Let's get max.
What was it? It was 06, right? Third row.
Six. Third row.
06 is also fine. Now we have um it's fine.
Now we have one. K is um we're getting four distance.
Okay. And then we are starting from three.
This is not good.
And same four distance it should be here.
R minus log D.
Should I just do this?
That is stressful.
this index.
Oh, that is correct.
Uh let's try something else.
We have seen all the edge cases. So any others should be zero.
Should we try a custom test case? But I think this is fine now.
But like K + 1 should if R is going back.
What is K + 1?
Should I go back 3 k + one times?
So this was the bound case, right?
So we're starting from zero and then six. We go back 1 2 3 4 Okay. 3 is 2 ^ 2.
So from here does it mean like this including this?
What is k to the power 1?
So zero is the matrix itself. No issue.
Right. K to the power 1 is this one and adjacent to the power zero.
This is 2 to the^ 1. What the Okay.
It has two elements.
Now this includes How is anyone supposed to focus?
No comments.
So total size should be + one now right you find the difference and you do it with L. Okay. Okay.
Everything we probably don't need + one.
I don't know.
Okay.
Then how much back you will go from R?
You will go back whatever was the difference of log.
Very nice.
Case two.
What the is to what is this thing storing?
It's storing from K1s We should have this four elements.
We don't have elements.
So for this zero, what is this 2 ^ k minus 1? K is 2. 2 ^ 1 is two. So you will go to two. Then I should have it. What the Why the are four? My god.
Oh, it should be like power, K.
Anyway, I'm getting wrong answers. So, this because I'm in fourth row. Oh my god.
Correct. So, um starting from zero, go to four of these and start from 012 and I go to four of these.
It does not include six though.
Right.
Yeah.
Hi bro. So my senior is here.
This is a very useless question. No. Uh the thing is I'm doing it I'm just practicing this sparse table. I'm implementing it by myself because I know like how to implement it.
Arrays of what? I'm building a sparse table so that I can query minax in like of one.
I have like some indexing issues here. I don't know why.
Now it should be fine. 032.
So I have this whole block and for three.
Now this is correct. Okay. Let's try out some other one to two.
So we're starting from one of length four.
Okay. So the whole block.
So we have this whole length. Then we have from two this whole length. Now this is solid. Okay. Now we can use this and solve this problem. Let me create a template.
But this is not helpful. This is like ready.
Should I add or not?
Now we can solve it.
What we can do is we have to see what is the value didn't apply for visa actually problem and I started at around like 7:45 basically shortlisting were that's the thing I don't know what was the reason was very easy I did like all there were like three I think four problems or three I'm not sure I have solved like all of them in like 30 minutes or so.
You stuck at 701 in this one.
Why did I start late? I did not start late. You may see like on lead code I'm doing it for the thing is I used to do it before my college but then I left then I again started I started in Python because I was also trying to do ML then first I need to learn language because you know you can learn things again.
Visa away. Why didn't I start it? I don't know. I was sleeping.
I was sleeping hard.
Yeah. So I don't know let's create a min max so so lower bound is going to be minimum of 0 comma so should be maximum of 0 comma whatever the current I is minus Okay.
Okay. So, R is going to be Do we need this? Yeah.
Minimum of N - one, comma current. What is row 5 + V?
I think that is fine. But over here I have to like subtract one as well less than equals to R + I.
Okay.
So I'm going to start from here which is I minus V and it won't go negative.
This is also fine.
But this particular thing what if the matrix I think if if it's going out of bounds then I don't have to consider it. I'm just assuming.
Okay. Now this thing X and Y that is again going to be J minus and J plus.
Okay.
Um, for this I can do something.
I can subtract minus one from both.
What if the size is only one?
Then I don't have to consider it, right?
Yeah.
Okay. So, um if I = to = to minus V.
Uh, wait a minute. Wait a minute.
Then x - and y - -.
Now if y is greater than x or even equals to x.
Now we have to check.
Now let's get uh let's get this maximum from get max and uh the row is going to be k and then we have x comma y.
Okay.
Now I need to accumulate like all the maximum Mexico.
Now if is maxi.
I want this to be equal to whatever the value is.
That was right.
Something is going out of bounds, bro. Not What the I just I just tested it. It was fine.
K should be fine, right?
Both are minus one.
They should be positive both of them.
Wait a minute. How can they because of that h because of zero?
What do I have to return?
number of local maximums.
local maximum if it is non zero and okay it should be non zero right correct correct again out of bounds oh my bad my bad my bad my My god, this sparse table it's crazy.
No wait I'm I'm having minus one because this might be a case it is zero and uh then it is going minus one. Wait this is wrong. Um now this is correct.
Okay, it works expected one for 01.
So for zero output should be nothing.
for one you go to all four directions but you actually can't do it right so my L is going to be I minus V this will be zero I believe let's see what is C out if this is fine then okay we have 0 0 so this element only.
So, okay, we are at this row.
X and Y both are zero, right?
And I'm getting wrong answer. It should give me some maxi So what is the maxi?
Wait, it should be like I minus and then I plus it should be one not zero. Why should be one?
I did like it should be I minus 1 and J + J is one.
So it should be like N minus one. No, I'm probably doing this.
This is where I'm not understanding.
Ignore the cells where the both row distance and column distance are exactly X.
Ignore the cells.
So for that particular cell, it's basically zero.
So that should not be ignored.
Yo, what up?
I need to fix this thing and then I will be good.
But this should not be true.
If k equals to i minus v.
So this is so it should get like plus x should be + one right cuz k actually equals to i minus v.
What is I?
Wait, something is wrong.
I, J, and then K.
And we're going to like these rows.
So, that guy is correct. There will be a stream at night, too.
Um but not on my channel.
Yeah.
What SQL Not sure what to do. You are not sure what to do.
Start solving problems, bro.
Okay. Zero.
In case of one like one distance over there, over there over there. So in case of one by the way in case of one I can increase count and continue.
Uh oh.
Because no 1 2 3 4 5 because this one is always the answer.
Now two won't be included.
Am I wrong?
Both.
Okay.
Okay. Okay. Okay. Both. Both. Both. B both. This and this.
And this is again wrong.
So I guess so these are coming out to be zero.
Maybe let's think about it. So we have so v is one I is zero and j is 1. Okay.
So we are going back b times but this is going to be 0 to n minus one.
So there is only one thing here. Okay that is zero.
Okay now we're doing z minus v j minus v. What is jus v?
Going back v * this is going to give me zero. Fine.
Then we have by this is wrong. Okay.
This is fine. This was wrong.
Is this correct?
There are two places.
You only have two one English feature from a chat section. I don't know. Should I skip this? No, I don't want to skip this now.
Let's do it.
By the way, did you give like uh code forces contest yesterday?
It should answer should be one. Why am I getting two? There's no way I'm getting two here.
two two operations for two.
So 0 0.
Okay.
in a first row.
What?
So it should give me one, right?
Should get one here.
I I am getting one.
Okay, for this one I should get this is the mistake.
So the mistake is the row K is Loki one.
K should be zero here and it is only executing once.
What is B here? V is one.
So you can go like row is one. So you can go like zero.
That is fine. So L should be zero.
Why is it not zero?
You were only able to solve I saw four.
Some people faced problem with two but like two but very easy and here the issue was I was not considering like minimum could either be the distance between them or and minus distance. So in a circle there are two ways to go.
I instantly got this because I thought if we have more than three nodes in a circle then player A can always like try to maximize some distance and then another one will again move to the same direction and will keep the distance same.
So the only way to catch her is first of all let her exhaust all the moves and then player B will go and catch her like something like that.
This one was like a implementation disaster.
Took me a lot of time to solve this but idea was like not very hard to think of because this particular because the number that will give you minimum absolute difference it can either be of same length or negative one or positive one length. So right so so if you think about the number that has one less length you will choose the maximum number of available digits. Okay.
So there we go. I just pushed whatever is b dot back the maximum number available. Right.
But for the smallest number. So this one is of length like n + one right? So for this you will try to pick the smallest one. But if that smallest number is zero then you are going to have some problem. So make sure it is not zero.
Right? So you have to check it. But as soon as you find something uh smaller that is not zero just break out and then again build the string. So how I did it I basically building I'm basically building the string and then I'm converting the string into like using functions like s2 long long or here in this case this can actually exceed long long so I have to use int one to8 I have created this function and this was the issue in these right I was not updating it properly I've got a lot of long wrong wrong answers And by the way, the hardest part was to think of the same length. So a same length string.
What you have to do? You have to iterate the digits and you have to um you need a specific digit that is very close to your uh original string the original number digit right and if it is smaller for example your number had two and the smallest available option let's say two is not available but one is available then for the rest of them you will try to Keep keep the maximum possible number here, right? To minimize the absolute difference. Similarly, let's say you don't have three but you have five. Then after picking five because five is greater than two, then you will try to minimize this. This is what I'm doing here. Right?
And every time I'm forming a forming this particular string, I'm also updating it. Right? So this basically efficiently updating updating my answer.
This was like the thing here.
It was more more of like a implementation problem.
So yeah, why this feels like so depressing to me?
I'll have to turn the stream off on 9:15. I'm going to go.
Okay.
So, for this one, I have one here. This should be two, right? This not This should not be 0 0.
This K should not be one. This K should be zero. I don't know what what's going on. What's wrong?
So at one I try to go to index but it is not available. So I mean X should be I is 2 I is 1. 1 - 1 should be zero. So L should be zero.
What's wrong?
L is zero.
But why K is not zero then?
What?
Oh, okay.
Let's see what's wrong.
So from where this two coming from I have removed it now.
Oh this this thing because of this.
Okay here k is zero but we have one and minus one because the distance is one.
So I'm trying to reduce it.
So one should not include two.
One should not include two. But again read it both row and column distance.
volume distance is this uh no um row distance is this I minus Okay.
Column distance is how much?
I can take either X or Y, right?
And both should be the case.
Okay.
Wrong answer.
Did I just submit it? Oh, my bad.
output is true.
So like for one we have two so it should not be the case right or 0 1 Zero. We have one X Y and K.
I'm brain fogged right now. Yes, I'm doing BAB.
So this is hashing and this is uh oh my god I have I have wasted so much time like I've started streaming it's been 3 hours oh my god mit my college this is bad But at least this is correct. So I can store it somewhere.
Um in your account. Your account on lead code should be have what is like the sub I think it's called slash profile something you Oh my god. Why I can't copy paste?
I can't copy.
Can you show your code of Q2?
Absolutely. Check this out.
You can also go to my profile and see it by yourself.
There you go.
It's like a very easy very easy problem.
Just make sure if like n is less than four then answer is always one because if you have three nodes the player b can catch in like only one move right.
This is the code.
Okay, this is the first one.
I mean first one again like count 01, right?
Then just remove the pairs, reduce them and then you can also make three ones and three twos. So add them that is it why four less than four it's less than four less than four. So if you have a single node then again like I think in a question it's given yeah so n is greater than equals to so if you have two nodes then again player b can catch now in a single move so optimal will be player a will stay in its position player b can catch in a single move okay um and in case of Three player B can move right whatever node is available but all those two nodes are connected directly with this player now so he can catch ultimately think of a triangle right so every single node is connected to every other node am I Right.
Yeah.
This is Y4.
I want someone to help.
Can someone spot a mistake? It is like some indexing mistake or something like very small thing.
Pretty sure about that.
This question is too annoying.
Why would they have to do this thing?
They could have just given like a simple matrix.
All right.
J min - X. So this is J and this is X.
Right? So in this case plus minus this looks pretty much fine to me. No issue with it.
I don't know.
So for one, why is this not executing?
Because Y is smaller than X.
How does this happen?
So 0 1 is this thing has been executed but then 1 0 it should be 0 0 something I skipped. Fourth one is more standard.
This is also standard. It just sparse table but I don't know what mistake I'm making here.
There is some very small mistake somewhere giving me wrong answer.
Let me think clearly.
So for both of these ones my answer should be so this is a problem right this is a problem in this uh in this particular question.
So you are starting your college just explore join some societies and make sure you keep learning things that's all I will suggest you keep learning row and column both. Okay.
So when I go to this next column which is one this operation is going to apply.
So initially oh so I have already reduced my value.
This is why it happened. Let's fix this.
Okay let's make it J minus P. Let's make it J + B.
Okay. And now x is equal to y = to minimum of okay please for god grace is I think I'm done.
This is so stupid.
GPD can do it now.
it.
Are you really thinking about ripping some stupid ass?
Just tell me how can I Oh my god.
The is that?
Foreigner exclusion. Yeah. Tell me where is the issue.
here.
GG.
What is the problem in mind?
Wait, what is the issue in my code?
So top is 0 1 - b exactly and minus 1 1 + b then uh equals to bottom and okay that is fine.
I don't need this. Okay. Variable.
Left and right.
Same as before.
This is equal to B J. And if matrix J minus B greater than B like node coordinate else.
What the It it shouldn't be that much complicated, right?
What's what's wrong?
My god.
Fan above, please. Fang on it.
What the What the Rohit?
Don't say these things. People are very sensitive.
Why do you need this?
Why do we need this?
Oh my god.
What is X?
Whatever is maximum J minus V.
Okay.
What is by minimum of either N minus one or either J + P.
Okay.
Now what is the overlapping situation?
when but I have to check for X and Y only.
Okay. So first of all absolute difference between J and K I and K it should be equal to V.
So this is like row difference then column difference is going to be x.
So I need to check for both x and y and do it that way maybe right. So absolute difference of x minus j is also equals to v.
Then x is going to be same Wait if zero is the case.
minimum of m minus one comma right let's do similar thing for y a different loop okay now if y minus j is E then y is going to be maximum of either zero y - one comma this is this logic is Good.
What's the issue here?
Pinpoint.
That is cool.
So, y - one and x + one.
Yep.
The main bug is in this part. You're shrinking the whole row interval.
It's not the whole row intervals. What the GPT is I'm not a brahmachari but I'm doing a live stream. Okay.
And it's public so I should not not be saying things that can be used against me.
Right?
You're right about that part. See chat GPD is so stupid, right? It told me the problem is that you're shrinking the whole room interval. I'm not doing it.
Actual issue issue is stable. Okay, my sparse table is inconsistent. you manually build with me then continue from k= to 2 standard 2 to the power I can write shift oh I so the real fix I mean that I won't do sorry I can just copy paste Oh my god. Oh my god. Prepare yourself, brother.
This is the What am I doing here? This K should be above Oh my god, my sparse table is wrong by itself.
Okay, this is fine.
My sparse table was wrong. Oh my god.
Because I'm printing. me. Okay, now this will work.
Spar table was wrong, bro.
This loop.
Honestly, sparse table deserves a template.
Okay, I'm not going to make these mistakes in the contest specifically. I could have used like segmentry in a contest, but let's build a sparse tablet CP, right? Um, but this is not a standard one. Uh I mean the code is not complex. I can build it but these type of mistakes are like hilarious.
Also this logic thanks GPT.
It's fine. I can do it.
So, are we solving fourth one too?
This is just hashing.
I was using a DQ.
So if I can do it with a string then maybe I can't do it with a string. I can do one thing. I can store a string but I can maybe just use a integer integer with like a mod.
So if I had to like remove some element at some index, how will I remove like the last element?
How will I remove five if this is like a integer?
Maybe hashing with a string is going to be better. I can use comma to separate different elements.
So it should be better for collision.
Could I just implement this method?
I can add a digit by multiplying 10 and summing it up.
Right?
But size can be like I can use mod but and I can remove using mod.
So just look at the size.
How do I know size of a string of integer?
convert it to string maybe.
Oh, wow.
Am I lagging?
Not really.
Okay.
So, I'm making a hash function.
I can increase it like let's keep it that way.
Maybe I should use long long and some other big prime number.
Okay, so hash is going to be hash into 10 plus whatever the number is. Okay.
And we should not forget to mod.
Okay.
Fine.
Maybe just for safety. I don't want a wrong answer because of that. So once I made a hash, I can do what? I can go to frequency convert it into string.
I can do ++. Okay.
Now I think it's lead code is lagging or maybe my browser is lagging. Not sure.
I have to remove the first number. For that I need size.
I I know what is the size.
So size is X.
So I need to mod it with 10 ^ 10 ^ I think. Yep.
That is okay.
Now my hash function will become hash mod s. Okay.
Now again same thing.
And now add it to frequency.
But now for checking this is a very big right could have like a lot of hash functions.
No, I can't do that.
That's not I will have to for auto.
That's too long.
Not again.
Python is my hash is this wrong?
How to implement double loading hash?
This is fine. This is fine. This is I don't know.
It's spitting out true every single time.
Okay, let me enlighten me about some hashing thing.
Your idea is this. So um but these parts are broken. Oh, it does not remove the uh number.
Yeah. Uh makes sense.
Are you just telling me like this is okay? This is wrong. So mod I mean, I should divide it. What the Right.
Something like this.
What are you saying?
What? How to remove this auto translation?
comments.
I don't know where is that option.
Translation from Y live auto setting here live chat window.
uh setting like chat window.
Click on edit. Customization.
Customization.
Live chat translation. Where is it?
There we go.
It should be fine now.
by.
So, this is a base that I'm using, but like Subtract 5,000 hash minus whatever is at this position into S, right?
This is bad. Hash function is not good.
I don't know.
I'll have to look into it later on. I have never implemented rolling hash. So, makes sense.
Using base 10 only makes sense. Numbers are single digits. Yep.
So it is a bad base. So I have to use some bigger base which is like at least greater than 10 ^ 5 double hashing. Okay. I need to learn double hashing.
H.
Oh my god, it's 9:39. I'm closing the stream, guys. I have to go and eat.
Uh, also other things.
Yeah. Bye. See you tomorrow, guys.
If you want to say something, Anurag, you have 2 minutes.
Only 1 minute.
Uh, nothing.
Okay guys,
Vidéos Similaires
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











