Move Zeroes (LeetCode 283) | Full Solution explained with examples | Study Algorithms

preview_player
Показать описание
Given an array of positive integers with some zeroes. You need to move all the zeroes to the end without changing the relative order of non-zero elements. At the first glance it feels that you will need to swap and move around a lot of elements to arrive at an efficient solution. This video explores a unique way of solving this problem and once you understand, it feels so easy and obvious. Watch the complete video with my final thoughts and a dry-run of code in JAVA.

Chapters:
00:00 - Intro
01:12 - Problem statement and description
02:50 - Straight forward approach to move zeroes
04:15 - Space Efficient Solution In-place
06:56 - Dry-run of code
09:22 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#leetcode #programming #interview
Рекомендации по теме
Комментарии
Автор

I honestly love your way of teaching, you would make an AMAZING professor

HarshPatel-husr
Автор

Hands Down this is the best soln i have seen so far thanks! keep on going.

reactninja
Автор

3:33 I Completed solving this problem in leetcode thank you ❤

avengersshorts
Автор

Your way of teaching is one the best i have ever seen thank you sir

saichaithrik
Автор

Bhaiya apki samjhane ki tarika sach mei pura alag hei ...koi nhi rok sakta apko DSA mei 💪💪💪💪

souravsarkarofficials
Автор

you have made my coding journey very easy and understandable

supershinobi
Автор

Man I swear I asked chat gpt to explain it to me. I read the leetcode solution, and watched 2 other videos but none of them made sense to me. I wanted to understand how to approach it and not just copy and paste the solution. When you explained it, I was like damn that's easy 😅

ramielwan
Автор

thank you for teaching the best and efficient approach. before watching this video i was struggling with the approach as mine took 336ms and the other videos were complex to understand, although it was only 5 min long but i couldn't get a loop;) nut this approach solved it in only 7ms...like this is crazy!

kashikasinghal
Автор

On point and quick to grasp explanation. Keep posting

ahmar
Автор

under if condition, we can just do swap(nums[i], nums[insertPos])

TechnicalMasterAman
Автор

This will take O(n + number of zeros) Time

Here is one more solution with O(n) Time

void moveZeroes(vector<int>& nums) {

int Pi =0, Zi = 0;

while(Pi < nums.size()) {
if (nums[Zi] == 0)
{
while((Pi < nums.size()) && !nums[Pi]) {
Pi++;
}

if (Pi >= nums.size())
break;

int tmp = nums[Zi];
nums[Zi] = nums[Pi];
nums[Pi] = tmp;
}

Zi++;
Pi++;
}
}
/*End*/

Vasoya_Jaydeep
Автор

I am watching your video first time, you are great

UjjwalRaj-CSE-
Автор

Thank you sir, really good explaination

TejwantDataLab
Автор

Two pointer approach is also better one

vis
Автор

Loving all the videos so far, but with this one it seems incorrect to say it's O(n) becuase the array is iterated over only once. I think it's actually of O(n) because it's simplified from O(2n) in the worst case that the array is all zeros. What about adding a line a code before inserttPosition is incremented that checks if i > insertPosition and just set the i-th element to zero then?

alexivuckovich
Автор

nikhil awaiting for the sheet that you said will release!!!

karthik-varma-
Автор

Sir you make me bound to subscribe to your channel and give like.😍😍😍😍

tufanpondu