Given an integer array, the minimum cost to make all elements equal by only increasing elements (adding 1 to any element costs 1) is calculated by finding the maximum element in the array and summing the absolute differences between each element and this maximum value. This works because elements cannot be decreased, so the target value must be at least the maximum element, and the greedy approach of setting all elements to the maximum value yields the minimum cost.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
LIVE Leetcoding + Atcoding + Ask Me Anything!!!
Added:I'm live, right? Yes.
Audible.
Yeah, I'm audible.
Let me just quickly open Let me just quickly open chat.
Okay. Push today.
Okay. full today today I'm planning to have a lit code plus add code mix let me just quickly open code edcoder and code forces forces is the list priority Okay.
Okay.
Let me start with headquarters. Once audience will join and the viewers will join, we'll save to the okay. So the start is add for the recorder based on the audience I can what can I choose let me do one thing let me start with add photo once people will join we'll shift to the code yeah today like Okay, let me start with exporting manage and flower bed if flower bed currently contain a little of water. I think I have solved this question right which no okay so it's a different question I thought I solve this uh I lost all my extension that's why watering the flower beds. Okay, the crushing energy and flower beds. The flower bed contain a lit of water. The fashion wants to make sure want to make the amount of water in the all flower beds equal. Okay. The fashion can perform the following operation any number of time possibly zero. Okay.
There's a extension web page kind of extension not Yes.
Okay, let's let's let's just go with the question. I'll find the extension.
Meanwhile, if anyone you know the extension that I used in my live stream, I just forgot the name.
I can check my page.
can check, right? You can check my Let me just quickly check my last video.
Here I have used I just want to know the extension name >> map of >> alo five point >> I want to know the name of this extension >> hint I just want to mark all that is the else we have two choice Uh, okay. Let's find Let me just start.
You'll figure it out in the slide.
So, Takashi wants to make the amount of water in all flower beds equal. Okay.
Takashi can perform the following operation any number of times.
Choose one flower bass and add one liter of water to it.
This operation cost 1.
Okay. The same flower bed can be chosen multiple time. Okay.
Since water can't be removed from the flower bed, the amount of water in each flower bath after the operation will be the at least the amount of water before the operation. Okay. The catching goal is to make the amount of water in all s equal to the some integer value is free to choose the target value. Okay. What is the minimum y cost? So the point is the sum of see this is what we can achieve right point is we can't decrease the water we can increase right so anyway we have to make everyone equal like for example say this is two we can make two uh we can increase from two to five here we can increase from 3 to five but we can't decrease five to two or three. The goal is to we have to make everyone uh each flower water level same which is the maximum value.
5 pages long time I'm also doing the string after a long time I guess more than 15 days right if I'm not wrong the question is you are given a integer array you have to make all the value of that array equal and operation is you can only increase + one to each element and you have to make equal so the by default choice we have 70 I think ah Maybe uh no last weekend also I have not streamed right last to last weekend I have streamed.
Let me just quickly code it out. It's not difficult question.
So the question is like we have n integer we have to make all the integer equal and and the operation we can perform is let me say answer is equal to zero and max is equal to max element among all.
Yeah, for all person X answer plus is equal to ABS of X - max and simply see out answer. So there's nothing much to explain here right guys you guys got the point right the question is it's a simple question it's a easy question I can consider as a easy question easy adder question point is you are given an integer you have to make all the integer equal and to make all the integer equal you are allowed to perform operation what kind of operation you can increase any element by uh for example if you any element one like + one it will cost you one yan. So what is the minimum yan to make all the element equal? So the the choice we have right now is greedily we can see we can make every element equal to the largest element because we can't we don't we are not allowed to decrease any element right so we can't decrease the bigger element to make it equal to smaller element that's why we have to so now we are restricted to a smaller element so that's the point we have to increase increase our smaller element to has equal as our largest element And that will solve our question.
Do if anyone do you remember like I used the paint right the red one. Do you know what's the name of that paint?
Because I'm missing my paintings and I'm able I'm not able to draw things.
Yes, thanks.
I remember say that you have an Amazon interview, right?
You can share your experience.
Uh this no no the paint the extension I use like I can I draw on the like I can draw here. It's overextension.
This is good. But uh I don't want this thing. Yeah, cool. We can use we I can use for now this to explain such things. Let move to the next. How was your experience by the way? interviews can share.
Meanwhile, let's move to the next questions.
Okay. Okay.
So, the second question, I can update the status for the new viewers. Let's current status is copy and paste all one.
Okay. Let me not write this.
Okay. And let me update this.
I can consider A and B as the easy question.
from C I can consider a medium. C and D are medium and for add coders these two are easy. These two are medium and this is hard. Okay. So don't get confused.
Uh my LinkedIn is in uh description pages. You can find my link here if I yeah here I mentioned connect with me. This is my link.
Everyone can connect.
Cool.
Let's move to the second problem. Then luggage removal.
Takash is in charge. Takas is in charge of an organizing warehouse. sells n item of luggage line up in a row in the warehouse and the weight of item from the left is AI. Okay. Takashi use a truck to carry out the luggage. Takashi can repeat the following carry out operation any number of time.
Once an item has been carried out, it does not return. Okay. Okay. Okay. So what is carry out operation means? So from the current remaining rows of item okay choose one continuous interval containing one or more consecutive item in the row.
However the total weight of the item total weight of the item in the chosen interval must be at most case. Remove all the items from the intervals and carry them out. After the items are removed, the remaining items closure the gaps. Okay. Uh got it. Got it. So problem is like you have n items.
Okay.
And you are also given the limit. This is K. Limit is K. K is just how much a truck can carry at a time. The K is defined. So at a time at a time you can pick any interval let's say this interval whose weight is less than equal to k. So in one operation like you can load this luggage into a truck and perform your transport.
Okay I guess they're asking what is the minimum number of transportation what is the maximum number of takas he can carry out when he try to carry out. Okay. So in a any of the transport like you perform let's say X transports. So among X transport what is the maximum load you can take that is what they're asking right find the maximum number of item Takashi can carry out when he try to carry out as many item as possible.
Okay, this I will accept. Uh let me open email.
Accepted your request. Accept.
Okay. So find the maxor of item Tashi can carry out.
Okay.
So let me take this example can move. Let me just move a bit.
So we have this as an example right 3 1 3 1 3 and our load capacity is four. This is our truck load capacity.
Okay.
So I can take this. So I can take this.
I can take this also. Right?
What? Why we are getting five here?
1 2 3 4 5. Okay. So So we have to output how many item how many item we can like actually yeah transfer using our truck.
So see here capacity is low. So we can't transfer anything. That's true.
So the point is how we can approach this.
Let's let us make a general statement.
We want to solve this. We have n item.
Okay.
And we have K capacity.
One of the main thing I have forgot to watch like I forgot to say you always have to look at your constraint before implementing uh any idea or any logic will you attend F5 the point is headquarter does not allow to attend a live contest I will do a virtual because of the stream I'm unable to attend that contest I will not discuss the problem also that is uh again a case of cheating right a mass cheating I'm against that so probably what we can do is after the contest end in tomorrow's stream we will solve today's head at coder beginner contest and tomorrow we have lots of problem we have bi-weekly lit code contest also we have weekly lit code contest also and we have edge corder beginner So we have lots of problem and it will be a fun stream tomorrow also.
So that's why I don't want to put myself in a pressure because in a contest we are a hurry right?
Yes. Yes. Today we have bi-weekly plus add beginner. So what we can do is to in tomorrow's stream we'll try to solve the questions of bi-weekly and eder beginner and lead code weekly.
Right.
So how we can solve this question? Did you guys got uh able to understand right what this question is asking?
We have to tell how many luggage we can transport. If you have a setup like this, you have a n luggage. Your truck capacity is k.
Right? So my claim is see guys my claim is don't think much see they are asking how many luggage you can transport right at and there is no limit on the round like how many time you will load the luggage into truck so what I simply do is if this element is greater than k definitely I'm unable to see if this element is greater uh which controls if this is more than Okay, definitely I'm unable to to transfer this luggage, right?
So my claim is I'm always I I will always able to transfer all the luggage whose weight is less than equal to K.
Are you agree?
Luggage weight is less than equal to K.
I'm always transfer all the luggage whose weight is less than equal to K.
There is no limit on number of transportation operation we can perform.
Yeah. So simply just count the number of luggage n and k c in.
This question will be more interesting if they ask minimum number of transportation operation carry out operation plus maximum number of item you transfer then you have to think something optimally here it doesn't require that much optimization int x in x if x is less than equal to k answer ++ plus answer is equal to zero see out answer that's all right I hope this solve our problem so do you guys want me to continue see of add coder or do you guys want me to pick a lead coder lead code question now I want your input So we have solved two easy questions today.
Okay. DJ is saying good.
Let me solve one of which question do you have any particular question to me?
Question you want me to try or I can pick any random question.
I guess you're saying we are ling it just I will not open my link.
Okay.
Okay, let me pick a random question then.
I just I will look that the interview that you shared through LinkedIn after the spring.
Now I want to pick the contest on please God give me a contest that I have not solved.
Ah why I solve this phone test also this is a difficult task I'm what I'm thinking I can build a extension or some kind of some some kind of utility that will tell me like in which context I have solved how many problem because this is too weird to find like that will be useful extension right is creation.
I hope I have not solved this contest. I have solved this contest also.
Life is hard to find up empty contest.
Don't tell me I have solved this contest also.
Finally, I have not solved. So, we'll not pick the easy question. We'll pick this medium question. Are you guys ready?
Are you guys ready to apply brain?
It's an exercise. You can consider this is the your brain exercise.
You want to look good. So you build a body, right? So if you want to look smart.
I'm not sure if I'm able to solve or not. So I will try my best. It'll solve anyway. You have a smarter audience also.
So minimum cost to make array identical.
Huh? Okay.
What I'm expecting is what I'm suspecting is we have to make this array is equal A is equal to B and there is something K. So let me read the question statement. Probably you'll get a better understanding.
The option is you are given two integer array a r and b r r of length n and integer k. You can perform the following operation on array any number of time.
Okay. Split array uh let me call it a and b because it is too confusing.
We have two array a and b. split a into any number of continuous segment and rearrange this subar in any order.
The operation has suffix cost K. Okay.
Okay. So we required a cost K to split array A into a let's say X subsear X subarrays. Okay. Choose any element in array A and add or subtract a positive instance K. Okay, that cost of this operation is X. Okay, return the minimum total cost to make A is equal to P.
That's set. So let's just summarize.
Let's just summarize what the question the hell is. See, we have array A, we have array B and operation. We have two kind of operation. We can split array A into X subarrays. That will cost us X cost. And what's the another operation?
Choose any element and add or sub positive integer X to it. Okay, we can do + Y or minus Y. It will cost us Y.
Okay. So we have this things. So say what I feel is we need to do some case work here if I'm not wrong we have to do some case work or let us think what what we can do here first of all look at the constraint constraint is pretty good it's not bad constraint right so we have to come up with an optimization optimal solution the brute force won't work here okay we have confirmed that Okay. Okay. So what we can do one of the simplest thought what came to your mind we have to make this A into B. See what's the simplest thought came into your mind? Just let me know. We have let's say we have uh this let's say let's call arbitrary.
Okay. We have to make this element this array is equal to this array. What we can do here is whatever the different we'll we'll try to make each single element equal right and that serves our purpose but it is not guaranteed key if this is the min it will solve using a minimum let's say we'll call as answer one answer one right but my point is what the benefit of first operation ation split array A into any number of front and rear. Okay, we can rearrange also. Okay, so the this is the first simplest way came to your my mind my my mind. Okay, so do you guys agree right this will solve our question. It will give you some cost but it is not guaranteed. It will always a minimum right? Are you like we are in the same page right till now right?
Okay. So this is fine right? What else we can do here is see what we can do here is this.
This is the simplest way. And what else we can do here is see we can split into any number of subarray. Right? That will give you if you split into two subarray. If you split into three subarray, it will split into n subarray. It will definitely cost you x right doesn't matter how many subarray. So what's the more freedom you'll get? In which case you will get more freedom. you break the entire sub array into a single single element. Then you have a see this B is fixed right you can't reorder anything into B. So what you can do here is you break everything.
Let's say this is A, this is A, this is B, this is B, this is C, this is C. See now you have freedom. You can put this A here.
You can put this B here. You can put this C here. So you make three element equal just a cost of X, right? And still you have freedom.
Still you have freedom. See, still you have freedom. What you can do is for this element, what you'll do, you'll find a element which can fit here in a minimum cost, right?
For each this uh this empty spot we can fill this spot. What how we can fill this spot? We find a element which can fit here in a minimum cost like this looks intuitive or not. See I I'm sure it is not clear yet but I will explain you will see you'll get a clear idea what I'm trying to explain here.
First of all, you get why we have to split into a n sub area or not.
Hi sir, my first year CGPA is 5.5. I should focus entirely on improving my CGPA in the second year to maintain a seventh or should I also focus on development skill like DSA? My branch is PEX, CSBs.
from which college you are in like what's the placement stats like if if your college has a good placement record I definitely recommend you to improve your CGPA because on campus is a game of CGPA then your skill because what happened on campus is complete just put a filter I want all the student who clear the OA and have A plus CPA came to interview if you have very good skill but still you are unable to join the interview with your friend.
So I strongly recommend if you are planning for a on campus interview please focus on CGPA and then focus on DSL average is 8 LPA it's not bad actually see the mark right now the market according to this market placement stat is very good right It's not bad. I can recommend please bring a CGP at least 7.5 at least 7.5 plus right yes because see on campus mostly like the level of DS is not that high if you're able to sol code medium definite completely a good age for you but CGPA matters believe or not if you believe or not CGPA matters and I saw a people having good skill but without good CGPA they are unable to sit for on campus placements and that hurts a lot so I definitely recommend first increase the CGPA Yes, that's the solution. That's the ultimate solution. But I just want to I just want to show you how we can end up in a such solution. Yeah, I I I'm also thinking the same the you have mentioned this is the actually the solution of this problem.
Thanks man. Yeah, CGP is very important but to get short list. So the point is the whatever the thing this pages has summarized let me just let me just put this is a good suggestion this is what we have to do this is what I'm trying to explain let me just scroll a bit up So the way I discuss here, see this serves the purpose, right? what they just said there.
We need to find a pair from A and B which has the minimum difference, right?
And how like this is the idea but we want to implement how we can implement a better way binary search right.
What you can do is just sort out this array a arment. I just I'm like I'm trying to jump to a solution directly now because I feel like this statement clears what like what I'm trying to convey.
So I'm just trying I just skip to implementation like how we can implement this is the nice observation. This is the actually the solution I I'm also filling. So how we can implement it?
What we can do is we assume we assume we have we have performed the first operation that cost us a x right and now we are we have a freedom we can put any element at any given position.
Right? So for each candidate we'll apply a binary search and find I think binary search is a monotonic or not we don't need index actually pages why was See uh as I showed you like for example say using this we'll get answer one using this will get answer two and we'll take the minimum of both and it will cover all the cases right and my point is let's just focus on this approach first then we'll see for for this element what's my potential good candidate to fit here that is what I want to find out from this sub array and I can rearrange accordingly right okay just like pick no type uh I don't think so it's DP right I no it's a greedy right I'm greedily picking up the element which give me a lesser cost and how we can like how we can get sage like so if you want to so this is a sorted see so if this is the sorted and I want to know I want to know which element should fit here but the point is not always uh it's a monotonic right difference is a monotonic because this is in increasing order if I take difference uh the difference will increase after certain point it will start decreasing.
So we want uh we have something like this.
We want this point.
That's hard to find with binary search, right?
sort will work but the concern is wait yeah yeah sort will work see for this element I want element which can give me a lesser cost that means absolute cost right it's an absolute cost right yeah it's absolute lost.
Wait, what just happened?
Ah, okay.
Uh, so how can I pick absolute cost? If I start taking the absolute cost, so it will increase increase at certain point will start decreasing or it will decrease decrease decrease a certain point will start increasing.
This is not monotonic right absolute difference is not monotonic what I feel see let me give you an example this will clear up let's just take an example That will clear all the doubt. What I'm suspecting is uh that is not a good example.
Okay. So we have see - 7 5 and 9. I have sort out this sorted this array. Right? And this is 7 - 2 and -5 right.
So now I want uh element this is sorted if I start taking the difference. So this become - 7 minus after sorting it always what else we do we can't sort B right okay we can sort actually yes Yes. Yes.
Ah, that's when it solve the problem. Oh my god.
It's a good observation. They just salute man. Yeah, I guess it solve our problem.
Let me first find answer one.
Answer one is just uh No, I am pretty see let's let's just try run you are correct.
So if you sort out this also then turns out to minus5 then this is min -2 and 7. Okay.
Then what we left with this difference of this two that is minus uh if I want to make it equal then minus2 this will give me a two cost.
Okay. If I want to make this two equal, this will give me a 5 6 7.
And if I want to make this equal, then this will cost me a two. It's like 7 8 9 11 13 2 + 2 + 7 + 2. Huh? And two is for sorting. Yeah. So this is true, right?
And intuitively it's also makes sense right? See for this element.
Yeah. Yeah. Got it. Got it. For this element you want a element that combine like that will give you a minimum cost and that's all we want.
So actually we want two answer one and answer two. So why we want to answer is answer one is without performing one operation and we are doing this only second operation.
Answer two is using both the operation right just to cover all the possible test cases. Answer 1 plus is equal to abs of Yes, that will cover answer one, right?
If A is already sorted and B is already sorted, this will cover this will like take care by answer one.
A of I minus B of I. Okay. Now we have to sort all both all both the array answer to is K already.
Then this then we'll return the minimum of answer one, answer two.
Okay, n is not defined. Let me define the end length.
And we have a wrong answer. Why? Uh sorry. Sorry. This is answer choose and We are confident right? We can submit this. Let me just submit and see we got accepted.
It doesn't put you that impressed in your mind right but still you feel good when you see AC. I feel good.
So we solve one medium portion.
Can we take one more medium question?
Then we can to add coder. We already solve one medium. We solve we can take one more medium from lit code and then we can again switch to add coder.
Right.
Thanks. Thanks. Thanks. Thanks.
Let me just take one more. But this this doesn't have any more uh can we jump to an there should some kind of button to navigate between contest. If there is some button here let's say I can build such extension. It's not a big deal.
We just need a button that can change this URL to this let's say seven. That's it. See it's not difficult also right?
We can build that kind of extension.
Uh if you want we can have a w coding.
We can wipe code this. It's not big deal. You just need extension with a button here that will click and this will trigger the manual step. It's just automating the manually what we are doing. That's it.
Let me just open contest. I will not pick the easy one. I'll pick the medium one. And this seems to a design question. So I'll skip this question and let me pick this one.
Good. Right. Should we try this question?
>> Do you guys want I can play a background music a light. Do you have any free copyright music for YouTube? I don't know. Live stream.
Do you guys have any idea about the such things?
Copyright free music.
This is very bad. I want some kind of lofty rain.
What the hell is this?
It's a free cop. It's I want a copyright free music. Let's see. I don't want any strike on my screen.
Uh what do you suggest?
I don't think so. This is copyright free, right? Both I can open a YouTube. which copyright free music lying it's a copyright free or not I don't know copyright free this is copyright free but this seems to music to And yeah, NCS songs are copyright free, right?
Hello.
NCS, right? NCS music.
But I want some.
Let's it up.
Does no one provide a free copyight music, I guess.
Anyway, let's let's wait. Let's not just waste the time and solve the question.
If anyone find a good copyright free Ly Rain music for my live stream, please let me know. I can play so you guys will not get bored.
You guys get bored, right? Easily with my sound.
Hope so. No pattern bro. I just pick a random question with a difficulty level of medium from add coder or coder and try to solve that's that's all our like see I should you should focus on learning like how you your main motivation is to solve the problem. See that's that should be your main motivation. If you see any question in front of you, doesn't matter how long it will take you to solve that, it's fine because you are in a learning phase and being in a learning phase is a good uh one of the best phase in your life.
So just pick a question. It's fine if you are beginner definitely you should go top a pattern topic wise and once you like once you feel a confident enough try to take a question that you feel a this is a good difficulty I can solve pick that question then try to see like how you it's not key it's based on your pattern it will solve based on your understanding you're able to solve all the question it doesn't require to mug up any pattern or Okay, that's fine. Let me just see longest subsequence with a decreasing adjacent with decreasing aesent difference. Okay, the question says we are given an area of integer norms. The task is to find the length of longest it seems to longest LCS question. This sensual and the problem uh the difference between do you guys able know this problem right this question is a good one I yeah it seems to because the constraint is pretty low right so it required some kind of good tactics here but my my my intuition is saying there's a longest common sequence right there's a DP question find a longest common subsequence So what I'm feeling is this is a variant of such question maybe who knows such that the absolute difference between the consecutive element from a non-increasing sequence in other word we can it's like peak or not right it's it's a peak or not variant right you can start your subsequence from I and how long you can Yeah, it's a DP question. Peak and not W here, right?
Peak or not peak.
Got it? Right. So, see if I told you like let me summarize.
We have an element.
We have to find the longest.
It can be anything.
longest subsequence if I just pick this element and take the difference absolute difference let's say absolute difference this is d1 this is difference between this element is d2 this is d3 and this is d4 then this should be like this then you can say this is the longest uh subsequence with a decreasing difference. Right.
Finally, go has some royal free music library.
Uh R RF free music library. This music is free. It's a YouTube library or what?
It's a YouTube library or it's a channel.
Uh, that's true. This is something good.
Do you guys hear the voice or not?
It's audible, right?
You guys hear the voice or not?
This is good music.
Thanks, man. Who who suggested this music?
Thanks, T. You're a slaver, man. You're not a slaver. You are a master.
What kind of music this has some of if I want to play in a loop? Is there anything I want to play this in a loop?
Ah, I just forgot. Like there's something I know you can play in a loop.
Loop. That's it, guys.
That's it. Let's make a string first.
Hello bro.
Just pick any let's say just build a project. If you don't know react that's fine. Just pick a project a problem statement. Let's say anything. Just try to build a project.
Just take for example say a good UI.
build uh the restaurant website in a React and Oh, too loud. Sorry. Sorry.
Now it is fine. Right.
Now it's too low, I guess.
It is fine right.
Okay.
something.
It's fine. It's your first project, right? So, you are in a learning phase.
That's completely okay. If you're not able to understand, build one project.
It's not like you're wasting your entire day to build a single project. Use time effectively. Build a one project, build second project. And in your third project you'll start understanding the things one pro if you complete one project you'll get a confidence complete second project you will build robustness if you start building third project you are a developer I didn't get your question actually react Like what do you want to do? I sorry I didn't ask you. You want to be a good developer front end full stack right?
Anyone like everyone on the same page right? It's a DP question. Pick and not pick. Because at each point you have option see we have an element let's just take like this you have this much element right and whatever things they have described here that's what we have to store in our DP state and I guess that's what it this full tech web tech react react with us next.
Yeah, you have pretty good plan.
Good for the front end just learn react for the back end learn something TypeScript JavaScript J TypeScript it's a good NodeJS TypeScript that's fine and build authentication system because in back time back in my college days I built one back end it's a backend project actually uh user authentication where I have build a JWT token verification something from that I didn't remember correctly But it's a good uh authentication system for the back end.
You can go for that. For the front end, you can build any restaurant website.
1 month is fine, right?
You can learn React like basics of React in 1 month and basics of back end in 1 month.
So let's build the DP state. Then let's back to the question.
Is the music disturbing you guys? We can stop the sound. That's fine. It disturbing you.
If not, I can play.
Hi Nishant. I'm in my final year of Mtech.
Let us write a recursive solution first nums and we'll see if recussion works then we'll memorize. Right guys on the same page just let me know.
We have a same page. See, we just need to memorize after this precaution works like a quick reaction anyone then we can start coding this question.
Right gods but it's depend on your it's individual choice you can't say anything it's my like because I want to go to Taiwan college I did my B tech from T college I just want a glimpse of how Taiwan college looks like and a good placement That's why I prefer and tech and I have time pretty good and it's work out also.
Let just solve.
We have current index. We have this element and we want our previous difference also.
previous difference. We can take an uh see uh infinite in 9. Okay.
So what we'll do here is do size if so we have two option pick not pick this is the basic framework right uh anyp can follow you can follow in np better pick or not pick we We need some base case here. That base case is if index is equal to n then we have to return one.
Actually we want a size.
What's the longest size? We can maintain size also. Let's say length is equal to zero initially.
Right. Return the length. That's it.
What else we can do?
Yeah.
So while picking see if you want to pick any arbitrary element this this should follow like you should build the difference in the decreasing format. So what we can do here is we'll find the current difference. What's the current difference?
Is there any better way? See wait this is coding. I just want to confirm. Is there anything better we can do? We can go with that. Otherwise we can code it out. This Where are you guys?
Oh, I haven't updated this right second question.
It's a peak and not pick right need to keep track of previous index also.
So then we don't need to keep track of difference because if we keep track of previous see then we don't need previous difference because using previous index we can calculate the difference right so let me just call it the previous and the previous and one more thing we can do here uh say for the start element We can start from any index right we can start from any index. So to identify we are starting our new sequence from here or what we can do is we can add one infinite number here and we always pick this element for the reference then we don't need to handle this right that can be simply so what we can do here is let me just modify the input array let me just build a What we can do is they just save God.
There's one more guy who's doing DSLR.
Ah, that's fine. Yeah. Yeah. I I remember that guy. The dumbass guy is something like no need to modify. That's also true, actually.
Yeah, that's also true. We don't need to have to modify. We can say previous is minus one. If previous is equal to minus one, that means we are starting a fresh sequence. Yeah, that's works.
Okay. The current length is zero.
Everything is fine. Okay. So, previous is this. So, index num previous and length. That's it.
So if we return length like if we reach index I then we'll return if we pick when we will pick if previous is equal to minus1 then we have option to pick this element. So what we can do here is uh return 1 + call.
Actually we want a maximum. Answer 1 is equal to 0.
Answer 2 is equal to 0. Let me call this as answer 1 is equal to 1 + solve. Index can be index + 1.
Nums can be nums. Previous is the current index that is index.
And the length will be the length + one.
Right?
And if previous is if previous is not minus one then we have to check the current difference.
Let me call it a current difference.
What is the current difference? That is it's absolute or what are absolute value of nums of previous minus nums of index.
Okay.
If current difference uh we need to keep track of previous difference also then hy is left like is guys.
Okay.
Current is equal to.
Uh wait, wait, wait, wait.
No. Hey pages, we want a difference, right?
For the comparison.
See if something is minus one, then there's no like you're starting from here. So there's no constraint of difference here. But previous is not minus one. Then you have a restriction.
The current difference should always less than. It's a strictly less than or okay less than or equal to. So you you need a previous difference also.
Uh we can keep track of previous difference for now. Let's just whatever we need we just keep track of that.
is also minus one.
H no let me call minus one in this.
Okay.
And let me pass the previous difference also.
Okay.
Okay. So the point is see whenever we pick something this is the first element then previous different doesn't change because it's a one element. Pre difference will only change when we have two or greater elements. Right?
Why you are learning DSA to get job? No bro, for me like this is fun. I like I I love to interact with people. I love to guide them and DS is fun for me. I that's what I do. There is no pressure of my rating like I have to increase my rating because I have I got I got my placement in my on campus itself right yes perfect so that's what I want to hear yeah so if you start of six once previous difference should be a maximum difference so that is what we keep here let's say infinity is equal to 189 right if you're starting something then I can mark this previous difference as this and here I have to check my current difference is less than equal to previous difference then only I can pick this element then my answer will be max of answer format.
Yeah. So I index + one nums uh this will be index. Yes. Len + one and this should be my current difference.
Got it?
So till now it's fine. Right? So these are the constraint while we are picking the element. Not pick is simply like answer 2 is equal to it's simple. We're just skipping this and it should be a previous difference here.
Right? And what we'll do here is we'll return the maximum among both the answer.
Right?
We'll see if it works or not. If not work, we'll debug.
It should be answer one.
Should be answer one.
And it's not working. We are getting six. We are getting eight. We getting double.
Oh, we're getting something double into two. Why we getting into two answer?
See if you saw here we're getting multiply by two somewhere.
Wait.
Okay. Where? Why we getting multiply by two?
I'm working with IBM.
Line nine. Line nine. Wait, wait, wait.
I forgot something or what?
If I read somewhere, why return zero?
It's length, right?
This is what we are keeping track.
See if we whichever the longest whichever the length we are getting.
Ah got we got it. So we are actually so we don't even need this right length.
We don't even need this constraint.
And that's all that's all our purpose.
But the purpose is not a recursion a DP a memorization. So see how many like see what are the things we need here.
uh if I don't want to think much here what can I do is I can create a string key what I can a trick where many of you know that I can create a unique key of index plus uh what is this pre uh index plus previous plus difference Right.
I can create a key like this.
Previous is not required as a parameter.
Okay.
Wait, wait, wait, wait. Why we don't require previous?
Because using previous we have calculated this right current difference.
I think we required previous right.
compute previous difference directly using the previous and ah that's fine previous difference is not required that's true I thought previous is not previous difference is not required that's true actually Yes.
Wait, why I pass a negative here?
Negative infinity.
This is not making sense, right? Why fast negative infinite? Wait, let me just get rid of previous difference.
What we can do here is uh previous difference, right? We require previous difference, right?
pages. Wait, am I missing something or what?
Previous difference is actually the difference between uh see if you uh wait there's some misconception.
Previous difference is the difference between let's say if I'm processing if element if I am processing I element then previous is a difference between I -1 and I -2 element H.
Yeah, that's uh that's that's that's got I got it. This point it is a not it should be positive infinity only. You don't even require negative infinity I guess. See did you get it? Why we need a previous difference?
Because we required a previous difference. We required a previous also.
See real difference tells the difference. Currently I have taken let's say A B C D E and if I want to if at I want to pick this I the previous will help me to calculate this difference and previous difference will tell me the difference between these two element. So what we can do is we can store the two previous element.
Yeah.
So what we can do is we can store previous one and we can store previous two also then we don't require previous difference then we can get rid of previous difference instead of previous we can keep track of two previous element that's what we can do right can you teach DP brother in easy way I don't understand it uh what you can do do is just watch a v Gupta a a playlist DP playlist you had a good video like beginner friendly then you can came to a problem DP problem solving that's a good start or what can I do is if I some like if I start solving a a DP question I see it is we can get rid of previous difference. See right say what is previous difference is storing it's storing the difference between these two element right and what is the current element storing it's storing the difference between these two element we already have this parameter we already have this parameter so we just need this index right and index can grow up to 300 so it can be any cube solution but if we store the previous difference the difference can be anything right because it is 10 ^ 4 the difference can grow up to minus 10 ^ 4 to 10 ^ 4. So that is lots of state, right? So let me first modify this into a previous let's say minus one. Okay. Let's say this is previous one. Let's say this is previous two.
Okay. And if this is the first element, so this is index. This will be minus one only.
Okay.
And here if previous one is not equal to minus1 and and previous two is not equal to minus1 that means now I have previous difference that we like earlier we have in a form of parameter but now we can calculate that previous difference that is just a absolute difference between previous one and previous two and this is a current difference.
Okay.
And then we can apply this.
But here we miss one case.
Then here we need to pass uh this become previous two become my previous uh previous one. See this is one. This is previous two. This is previous one and this is I.
So now I become my previous one and this will become my previous.
Okay, it was confusing for now. Wait, I'll try to explain. And else if previous one is not equal to minus1 and and previous 2 is equal to minus1 then also we have a choice here.
We can make the current element a previous element here.
Then it just like this uh here it's not a one it index it will be index and previous one.
This is making sense or not if previous one.
Ah okay.
Parameter is not reduced but the scope of parameter is reduced. Right stages see I will let you know. So earlier what we did is earlier we have three parameter right index then we have previous then we have previous difference.
Right? If you look how like what's the how this will grow this will grow n into this can grow n into real difference can grow up to this this value right 10 ^ 4 because difference between any two element can go up to so this can be where it is here so this is 10 ^ 4 right and this is already 10² square.
This is also 10 square and this is you can 10 ^ 4 5 6 7 8. So this can I can say this can grow up to 10 ^ 8 right but the new the the new parameter instead of this what I'm saying I will keep track of index it is visible or not it's visible right instead of this what I'm saying I'll keep track of index comma previous one and previous two.
So this is n, this is n, this is n. This can grow up to n cube and n is 10, this can grow up to 10 ^ 6, right? And let me see if works or not.
Uh and here it can be if we skip if we skip here then previous one become previous one previous two become previous two.
It should work right. See it is working right.
So the what change we did is before this we are passing here a previous difference right? So the same code but use uh what we did is using previous and previous two we have calculated the previous difference and using previous one and current index we have calculated the current index and the entire logic is same. Now we have to memorize this.
So it's we have three parameter right.
So I will not use vector vector this time. Last time pages has suggested me to use the tpple, right? I will not even use tpple also.
I can use this.
Okay.
Then me set of DP comma size of DP comma minus one.
Right?
Let me just check the syntax is fine or not. I just want to know the syntax is fine. We getting some runtime error, right?
Why run time?
Because of this.
Oh, still we are getting some runtime error.
Wait, what we just did is we just add this to line, right?
Yeah.
And what the is going on? I can create DP like this, right? Or not? Oh.
Oh. Oh. Oh. We can't create more than 10 ^ 5.
Right.
Oh, this can grow up to 10 ^ 6. And we can't create DP.
We can create right what's the error stack of low what the hell is going we can create right?
Oh, why by why I don't know this concept error. I code in Java. Oh, okay. But we can create DP like uh this big DP. I think we can't create this big DP. See 300. This is already 10 to the power.
This is already 10 ^ 6 and this is 9 9 3 that's 81. This is actually 10 ^ 7. Ah I see we can't create DP like this.
This is 10 ^ 7. We have to reduce one more parameter then but it is impossible to reduce now.
What if I create a vector? Let me just No, I don't want to create a vector 3D vector.
Is this allowed or not? I just want to check see generally not allowed because allocation of array Okay. I think in Java also it won't allow right. pages because this is a big uh it's a 10 to the^ 7 DP more than at least 10 to the^ 7 Then how we can reduce the state? Can we reduce further?
No. Right. We can't reduce anything else like this is necessary. Index previous is also necessary.
How many test it pass with recussion solution?
Okay, fast. Let's find t how we can let me just try with vector of I pass 50p. Okay.
If DP of index comma previous one comma previous two I think plus two let's do plus two here Okay.
One just to handle the minus one case.
Then return this. Okay.
I got this. Okay.
Okay. And we have to pass the AP here.
And here also.
And here also.
and and noun dot size that's my n and yes it pass let me just summit it will give you memory limit exit maybe see we're getting memory limit exit right because this is too big DP So we need to reduce somehow we need to reduce this the state number of states.
Yes, this is what I want to see.
Uh according to me as I remember we can create a DP with the maximum 10 to the^ five element max.
Right. How we can reduce this how we can reduce this? Does anyone have any idea?
I have wait I have to reply this message.
Does anyone have any idea like how we can reduce this such a proof.
How we can reduce this?
How I can reduce this is Okay, I got a message. See if someone message you do reply because someone is waiting for you right at the another point.
So you should feel good about this because it's basically not much there's not much people in the world care about you. If someone cares you, you should reply. It just cost you a two two three second like hi Jane first of all congratulations to getting triple IT Delhi so my advice is it's a if you want to nail your on campus placement get CGP 8.5 and more.
It's your ultimate target. You'll get a good placement if you have 8.5 and most GPA great preparation is like just stick with the just stick with the planning and do consistence.
If you're consistent enough, you'll definitely correct.
How I can reduce this parameter?
This 3D DP I want to convert into 2D DP or I should look to need them.
So the maximum answer for each index and every possible difference.
Each index and every possible difference. Okay. So that they're telling me previous and difference. The tactic we are following that's correct tactic no index and previous difference that's the only thing you don't even need this previous one and previous two that's what that's what they are trying to see see I recommend everyone to prepare for gates see if you have time so and if you are from tired three college it's best it's definitely a good chance for you. See Abina is the one of one of my junior. I don't know him personally but just got prepared and prepared has a good placement stats right.
Am I offline?
Hi. Hi. Hi. Am I audible? Right.
Can anyone confirm? Am I audible or not?
Thanks, man. Now I'm starting getting hearts.
I'm audible, right?
Yeah.
So the main optimization here is we just need a two parameter. Parameter one is this difference.
This we put but I'm live H okay. Optimization is saying to store the maximum answer for each index and every possible difference.
Okay. Okay. Thanks. Thanks. Thanks guys.
Thanks pages. Thanks.
So I just need an optimization. Can anyone help with me? See, currently we have a 3DP. We just need to convert the 3D DP into 2D DP. See, and if for the current setup, current setup is bound.
See, if you go if we go with our current setup, it will definitely bound, right?
If you store previous one, you definitely need a previous two. So what we can do? We can get rid of two this both dimension and add something that can serve the purpose of previous one and previous two. That is just a previous difference. That is difference right that's difference.
So we have to formulate in this form then.
One more thing. See, one more thing.
Wow. One of the interesting thing is a difference is an absolute difference.
So, it can grow up to 10 ^ 4, right?
Are you guys we are on the same page, right? So, difference can grow up to 10 ^ 4, right? And the index can grow up to 10 to uh 10 10 10 square. See? See? See?
Yeah, now I have a clear picture.
Yes, now we have a clear picture. Now see, we can store two parameter index and difference because difference is an absolute difference. It can grow up to 10 ^ 4 and index can grow up to length that is 300.
So this can equally I can say this is 10 ^ 6. Previously our DP state state is 10 ^ 7 more than 10 ^ 7 I can say so 10 ^ 6 will work. Yeah. So this is what we want.
So what we can do here is but then we have to change our this logic.
So store the maximum answer for each index and every possible difference.
How we can formulate this?
How we can formulate this?
Right now this is index. This is previous. So instead of this I can convert into 2DP. First of all, this is index and this can be this right and this can be 1 4 + 2. Okay. So, DPS 2D now and main task the main task is this.
And instead of this we need to pass the difference the current difference. What's the current difference?
I can say uh int max infinite can be one4 for this problem.
Okay, let's just let me just mute the background music and we can move focus. Right?
So now instead of this we have a difference.
No the point is safe.
What I'm suspecting is we just need index and DP because for each index we have to calculate what is the maximum subsequence we'll get if we are ending at index I don't think so we required this but this is confusing now I don't know how can I implement this we need to change our complete logic Let me let me let me let me just take this code.
See this is the maximum we have came here right.
Oh should I look to a implementation how we can implement or does anyone have a idea like how we can implement 2D pay how we can implement 2DP now for each I I want to calculate what's the maximum longest subsequence ending at index I for all the friends.
Wait, I think I missed the today's snacks.
What's the today's snacks? We have two days.
Let me look the solution then we can end the string because this is very good question.
CC see wait what? What the We just take the index and the previous.
I I guess this is previous right?
Since the range of value is small, we can store information about longest subse subsequence starting from Oh my god, I got it. This is this is just a variant of longest common subsequence. If you guys have solved I got it. What this guy just did here is they just tried each possibility like starting and ending.
Okay.
So what this this guy just so what the thing is to do so we can we can use information computed only for each number num to update this uh this is a very good DP then it required oh order of n how is this possible order of n this is not order of n right off This is not order of n.
This is not order of n. Right?
Ah this is order of n. They just say if you tell this is order of n then this is not order of n. Right? Because the question constraint itself is entire question is order of one. If I can say n is 300 then this is not order of n.
Okay then I have to look into the solution because I'm not able to understand what the people are trying to say. N into 300 and 300. What's the n here? n is I think the difference. N is the difference. This is the previous starting point and ending point maybe left to right for each possible next number update the DP for current number.
Yeah, if you have a good practice on DP you can understand this is this is what the solution is. Yes, we tried a I tried my best but end of um memory limit exit that's fine. I'm happy with this result.
I have to look into the proper solution that give you IC right so that's all for the today's font it's not contest it's live stream right tomorrow will be the interesting live stream guys we have tomorrow around I'm hoping we have four medium question two easy question and two hard question see you guys tomorrow And that's all for today. Thank you for joining everyone.
Take care. Bye-bye.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23