Move zeroes | Leetcode

preview_player
Показать описание
This video explains the day-4 problem of leetcode 30 days coding challenge in april 2020. I have explained the problem with given constraints in mind using example and code. This video first explains the simple approach and then an intuitive approach of solving the problem using two pointer technique. It solves the problem in O(N) time and O(1) extra space with inplace algorithm. As usual, CODE LINK is given below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

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

Nicely explained. Wow. We can avoid swap. Take count=0 and whenever we see non zero arr[counter++]=arr[i] and in last if counter<n arr[counter++]=0 .

guptasaurabh
Автор

We don't need to worry about the size condition:
int p1=0, p2=0;
while (p2<nums.size())
{
if (nums[p2]==0)
++p2;
else
{
swap(nums[p1], nums[p2]);
p1++;p2++;
}
}

siddhanttiwary
Автор

In Javascript

function moveZeroes(arr){
let counter = 0;
for(let i=0; i < arr.length; i++){
if(arr[i] !== 0){
arr[counter++] = arr[i];
}
}
while(counter < arr.length){
arr[counter++] = 0;
}
return arr;
}

sandeepmenta
Автор

first I counted the number of zeroes, then I removed all zeroes using STL erase and remove. Then I pushed back the number of zeroes that I counted earlier.
Time Complexity is O(n) only and Space Complexity is O(1), because I just used an extra variable to store number of zeroes

RohanSaluja
Автор

Nice explanation!
Same 2 pointer approach, but without swap
void moveZeroes(vector<int>& nums) {
if(nums.size()<=1)return;
int j=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]!=0)
{
nums[j]=nums[i];
if(j!=i)nums[i]=0;
j++;
}
}
}

life_of_anjali
Автор

we need to swap only when val @ left pointer == 0 and right_pointer not equal to zero;
summary of the solution:
- 2 pointer left and right initially at 0
- repeat until right @ end of array:
r == 0: increment right
r != 0
- left == 0 => swap left and right pointer val
- left != 0 increment left and right pointer

jsarvesh
Автор

Thank you. You made the solution look very simple. Appreciate it!

dondondonify
Автор

i am a python programmer just come see explaination very helpful

AVIS_
Автор

Good solution. But, I think full swap logic is not required by creating extra variable temp. As we know element to be swapped is fixed 0. Something like this will work. nums[i] = nums[j];
nums[j] = 0;

shinku
Автор

instead of taking extra variable temp we can use in built swap function.

imshivendra
Автор

sir explains the best, i solved it but came to like ur video thank you sir👏❤️🤗

yashpreetbathla
Автор

This code is showing time exceeded.. :(
void moveZeroes(vector<int>& nums)
{
int i=0;
int x=0;
while(i<nums.size()-1)
{
while(nums[i]==0 )
{
nums[i]=nums[i+1];
x++;
}

for(int k=0;k+i<nums.size()-1;k++)
{
nums[i+k]=nums[i+k+1];
}
i++;

}
for(int
{
nums[f]=0;
}



}

vireshdeshamane
Автор

thankYou for the explaination..keep up the good work...

sagarpatel
Автор

So basically we don't have to shift zeros to the end, we have to shift non zeros to the front . Cool 🔥 👍

vinayakf
Автор

@TECH DOSE You said in the video..if you have a better logic then let me I will just provide a Python code where it still can be thought as a 2 pointer approach with the left pointer and the right pointer being the array traverser same logic can be implemented in Java, C or C++ or any other language according to syntax...
left = 0
for index in range(len(nums)):
if nums[index]:
nums[left], nums[index] = nums[index], nums[left]
left+=1

ronsinha
Автор

Also make the feb 2021 leetcode playlist

jasmeenkaur
Автор

Hello, your approach is so elegant but you forgot to write return array. However, thanks

sabrinarahman
Автор

Hello sir, I want to learn about time complexity and space complexity. How we calculate it is o(n) or o(log(n). Any tutorial are there for that? your explaination is so good.

divyamaheshwari
Автор

Hi sir can you tell why it's++right instead of right++?

akashacharya
Автор

thanks for the explanation, but how would you answer if the requirement is to move all zeros to the beginning

pl