This video presents solutions to LeetCode Biweekly Contest 183 problems A and B, demonstrating two key algorithmic approaches: (1) For moving zeros to the end of an array, count total zeros and identify zeros in the non-zero region (indices 0 to N-count-1), where each such zero requires one swap operation; (2) For making an array modulo K alternating (even indices with modulo X, odd indices with modulo Y), brute force all possible X and Y values (0 to K-1), calculate minimum operations for each element to achieve each modulo value, and find the minimum total cost across all valid (X,Y) pairs where X≠Y.
Approfondir
Prérequis
- Pas de données disponibles.
Prochaines étapes
- Pas de données disponibles.
Approfondir
Leetcode Biweekly Contest 183 | Video Solutions - A to D | by Devansh | TLE EliminatorsAjouté :
So, hello everyone. My name is Vikas Soni and today we are going to solve the Lead Code Biweekly Contest 183.
So, [snorts] first let me check if my if I am audible.
Okay, also let me know in the live chat how many questions you were able to solve and how was the contest overall.
Okay, also let me know in the Yeah, if everything is okay, we will just start in less than 1 minute.
I think everything is good.
So, we should start the stream now. So, we will start by our first question which is minimum swaps to move zeros to end. Okay.
So, in this question we are given an integer array nums and in one operation we can choose any two distinct indices I and J. So, we can choose any two indices and just swap this element at that indices.
And I have to return the integer denoting the minimum number of operation required to move all the zeros to the end of the array.
So, I just have to move all of these zero to this somewhere in end.
Yeah, and I have to return the minimum number of operation required.
Okay, so let me see here.
So, this is question A.
So, let's assume that there is some array and array is like 1 0 2 0 0 3 and 4.
Okay.
So now we have to move all of like all these three zeros into into end here, right?
So Okay.
So how we are going to uh do that? Okay, someone is asking is question three Kadane? Well, somewhat a Kadane. So it's not exactly Kadane's algorithm. So question three was amazing question. So Yeah, we will discuss later.
Okay, is it two pointer?
You can use two pointer approach here, but there is no need of that. So what thing I used is just think that in the optimal array okay or in the answer, let's say final array.
So in the final array uh how my array is going to look like? So I know that there will be some elements here.
Just assume that some elements are like this and in the end there are going to be three zeros, right?
Is this correct?
So if there are three zeros in whole array, then I know that all of the three zeros are going to be in the end.
Right? So now just find that how many zeros I have to move.
So okay, these are the this should be the position of the zeros.
And initially the zeros are here. So can you see that? You don't have to move this zero, right? This zero is already at the correct correct position, right?
So what I can see is like if there are three zeros in total in the whole array any Uh any zero which is in this in this region we just have to move it. Right? So, yeah. And it is also two pointer.
Okay, so you can just take the P from here and P2 here and easily do that with the two pointer approach.
Okay, and yeah, the simple approach I used was this one. So it is just that I counted that how many zeros are there total.
So, the N is the size of the our array and uh I just iterate all of elements and if the nums of I is equal to zero, which means it is zero. So, I just do count plus plus. So, now my count is the number of zeros in the array. So I know that in the last right section, there are going to be all of the zeros. So, in the left section which is this one, from zero to N minus CNT if it is zero, then I know that I have to move it. So, I just do answer plus plus.
And that is that is it and I just have to return my answer.
So, let's just uh dry run the code, okay? So let's assume that uh my array Okay, just a second.
Okay, okay.
Okay, so sorry for this.
So, assume that my array is a 1 0 0 Okay, just >> Just a second, there is some issues in my tablet.
Okay, I faced cloud fair disconnection issue. Yeah, I also faced that for like around 5 or 10 minutes.
Okay, let me back to this one.
So, I'm sorry for the delay here. So, assume that my array is like this.
And now I want to find the answer. So, what I will do is I will just find that how many zeros will come in the end. So, I know that total total there are three zeros.
So, here in this three position, it will be all zeros.
All zeros. And here, it it should be all non-zero position. All non-zero.
So, in this area, in this region, I just I iterate and count how many zeros are there. And that much zero I have to move. So, that's it.
And I hope that everyone understood this question.
So, yeah, if everyone understood, let me know if you have any doubt.
Okay, I think we should move to the next question. Like, I feel that this were easy, like So, this So, it would work.
So, we are given a integer array nums and an integer k.
So, we can use and we can use nums array and And the array of the modulo If this thing then Okay.
If it should be thing in that modulo.
But every even in and nums of and for each of uh nums of I modulo will be Y.
And I will return the minimum number of operations required to make the the nums array as the modulo alternating.
Okay.
And the size of the still it only on the Q was also counted only.
So, there is no big issue of time complexity like exponential in this.
Okay, so now let me see here.
Okay.
Uh my voice is not clear. Okay, let me check this just a second.
Uh hello, like is it correct now? Like please Okay, let me know if my internet connection is good now and you can see everything.
Yeah, thank you for your response.
Okay, now like just see this.
Uh let me just see the smaller subproblem of this one.
Assume that Assume that okay, what this is uh our question be.
Okay, so this is our array.
Assume that the elements are like A1, A2, A3, A4, and A5.
So, like in the end, what we need to make is like this A1, this A3, this A2, and like uh these are like modulo K uh for this even element element modulo K should be something like that.
Okay. So, like the first thing you can see here that the K is only up till 100, right? So, the first thing that comes to my mind is why not just brute force everything, right? Brute force everything.
Okay, what do you mean by brute force?
So, let's say let's say for odd elements, so this is our subproblem, okay? So, for all odd indices element, odd indices element, the minimum operation to make them let's say value is X modulo K.
Okay, and okay, if this is the question, yeah.
Let's say this X is equal to one, and I asked that what is the minimum operation you have to do so that on all of the odd elements are uh uh are X when divided are modulo X divided by K.
This question.
So, we are going to do this.
And then we have and then we have all of So, okay. So, I modulo K because these are the possible modulo 0 to k minus 1.
Okay, let me just uh explain this with a test Correct.
And the array is like this.
Now, all of the odd elements uh all of the elements at the odd indices you uh you and assume that k is greater than You want to make all of the print indices or indices element modulo k is equal to three.
For now, what what are the minimum number of operations?
So, you will comment this five.
Okay.
So, this is five.
Now, there are two possible options.
Either you can reduce uh you can reduce it and you can just keep increasing until it becomes the odd modulo k is equal to three, okay?
So, this is a okay, let me take a bigger test case. Let's say it is 15.
So, this is 15.
So, either you can make it 23 or you can make it Right? These are the two possible options for it.
So, for this 15 to make it 23, the cost is going to be eight.
And here the cost is going to be what?
It is five plus seven.
We just took right?
Okay, so let me take 13. The cost is only four.
Right? So, there are two possible options.
Right?
Okay, so like that many options. Like you can make For this 15 you can make it uh 33 or 23 or 13 or three or any anything, right? But because you have to minimize the element, you will just make make it 30 or 23, right? Whichever is whichever the cost is minimum.
So yeah, you can see this.
So for the element two four you want to make it will be uh right It is going to be two plus one plus four, which is seven, right?
Similarly, you can find that what if you want to make the odd indices modulo okay is equal to four? What if you make the odd indices modulo okay is equal to five? Till k minus one, right?
Okay, and then then you can just do you can just do pairing, okay? You can pair it Okay, I hope you can understand everything.
Okay, I'm sorry for the lag, right? Let me know if I am audible now.
I think there is some issue with the mic.
I'm really sorry for that.
I'm so I am sorry for the lag, right?
Let me know if I am audible now. Okay, I'm really sorry for that. Okay, so I will repeat this question again. Okay.
So This is our question two and In this question like we are given two integer the integer and nums and an integer K.
And in one operation I can just increase or decrease any element the num by one.
And the array is called modulo alternating if there exist two distinct integer X and Y.
Such that like for each even index I it's modulo when divided by K is X. And for every odd index when you divide it by K and the remainder should be Y.
And I I have to return the minimum number of operation required to make the nums modulo alternating, right?
And I I have to return the minimum number of operation. Okay, so Let me just see the first of all the smaller question, okay?
So assume that uh you have some element, okay? Let's say the element is 15.
And you want to make it Let's you want to make it some number X such that that X is X modulo 10 is equal to three, okay?
So now if you want to make So first of all you can So if you want it like this, what you can do?
So there are two options. You can add some values and you can make 23, right? Or you can just reduce it for two times and make it 13, right?
So there are two options for you to make this uh integer when you uh integer modulo 10 is equal to three, right? So either you can make it 23 or you can make it 13.
And which value you will make uh whichever the cost is smaller, right? So for this you have to do operation two times, and for this, you have to do the operation how much time?
Uh eight times, right?
Okay, so that is clear.
Now, the question is Uh sorry.
Now, the question is this one, right? So, for all of the even index, you have to You have to make uh their modulo same, okay? So, for each even index, this nums I modulo K should be some integer of X.
So, first of all, the K is very small.
So, you can just brute force it. So, what what I think is that why don't you count everything? So, for even index, let's say for even index, elements, the cost to make this index this element modulo K is equal to zero.
Or what if you want to make this is equal to one, right? So, for all of the odd even elements, their value modulo K will be one.
Or for all of the even index elements, what is the cost if you want to make their modulo is equal to two?
And you you can find everything till K minus one, right?
And you can just brute force it. So, Okay, I I How you are going to brute force it? So, let's say So, I just iterated on all of the elements, all of the even elements.
So, what we are going to do is that assume that assume that we want uh even element Modulo K is equal to some value X, okay?
Some value X. So, for this one I will find what is the minimum cost.
For this one, I will find the minimum cost. For this one, I will find the minimum cost.
And the sum of this value, okay? So, let's say this is A1.
Or this is Let's say this is C1. This is C2. This is C3.
And the sum of them is going to be my minimum cost, right?
This is going to be my cost to make this.
So, that's what we are going to do.
And for all elements also, we are going to find that. And why we have to do that? So, by this method, we will have all of the cost, okay? So, let's say for all indices the cost when modulo K is equal to zero. Or we will have the cost when modulo K is equal to one.
And the cost when the modulo K is equal to K minus one, right? And for the even element also, we will have the cost modulo K is equal to zero till like cost modulo K is equal to K minus one. And why we have to do this? So, because we know that this cannot be paired with this one, right?
The the uh the odd element modulo K is equal to zero, then the even modulo K cannot be zero because this both value should be different.
Okay, so like just see here. There should be two distinct integer X and Y, right?
So, therefore, we have to just brute force this and we have to find the this.
Okay, so what you can do is you can just find for each element like what if the cost modulo K is equal to zero for all of the all elements.
Or for all for all odd elements, if you want to make their modulo K is equal to one, what is the cost?
And you just find everything by brute force because K is very small value.
And then what you do is that because this is this can be paired with anything, right?
This can be well paired with everything, but not this one.
Right?
Because both of them cannot be zero.
So that's what you do. So you take this one and you take the minimum among these one.
That's what you do. So let's say this is equal to one.
Modulo K equal to one.
Then just by brute force you value what is the cost.
And take the minimum of this all. You don't take this.
What is the minimum of this thing?
And you pair it with and that is the answer.
So let me just see here.
I think when you see the code, it is going to be you are going to be understanding.
So this uh this is the sum of my then take two a vector and answer.
So >> Mhm.
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
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
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
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











