Move zeroes | Leetcode

Sdílet
Vložit
  • čas přidán 3. 04. 2020
  • 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 :)
    CODE LINK: gist.github.com/SuryaPratapK/...

Komentáře • 113

  • @sagarpatel8853
    @sagarpatel8853 Před rokem

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

  • @sabyasachisahoo8975
    @sabyasachisahoo8975 Před 2 lety

    thanks for uploading,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,your method is always better.

  • @dondondonify
    @dondondonify Před 3 lety +4

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

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Thanks

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @guptasaurabh688
    @guptasaurabh688 Před 4 lety +3

    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

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Nice idea :)

    • @anujithsingh4868
      @anujithsingh4868 Před 2 lety

      Isn't this a bad idea if all the elements in the given array are zero, then both right pointer and counter is going to iterate through the entire array.
      Where as if we do swap, we'd be iterating only once both pointers put together.

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @siddhanttiwary
    @siddhanttiwary Před 4 lety +1

    We don't need to worry about the size condition:
    int p1=0,p2=0;
    while (p2

  • @yashpreetbathla4653
    @yashpreetbathla4653 Před 4 lety +15

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

  • @codeisgoodbro1810
    @codeisgoodbro1810 Před 3 lety

    Good stuff brother, two pointer technique optimal solution

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @ashokjat555
    @ashokjat555 Před 4 lety +1

    Nice video,Thank you bro.

  • @sanskarkumar7984
    @sanskarkumar7984 Před 4 lety +1

    Amazing Approach

    • @raghukumar2874
      @raghukumar2874 Před 4 lety

      Sanskar Kumar The concept is very well explained in this video infact i have my channel which has similar content.pls check, subscribe and show me your support if u like them and give me u r feedback in comment section.thank you:)

  • @eyasir2047
    @eyasir2047 Před 2 lety +1

    Two pointer technique 😎

  • @hardikshreyas8540
    @hardikshreyas8540 Před 2 lety

    i am a python programmer just come see explaination very helpful

  • @life_of_anjali
    @life_of_anjali Před 3 lety +4

    Nice explanation!
    Same 2 pointer approach, but without swap
    void moveZeroes(vector& nums) {
    if(nums.size()

    • @imshivendra
      @imshivendra Před 2 lety

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

    • @SHRUGX_YT
      @SHRUGX_YT Před 2 lety +1

      really appreciate this code anjali

  • @koushiks9292
    @koushiks9292 Před 3 lety +1

    Ur contents r really good

  • @sandeepmenta9579
    @sandeepmenta9579 Před 4 lety +2

    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;
    }

  • @abhaythakur2597
    @abhaythakur2597 Před rokem

    well explained

  • @shinku100
    @shinku100 Před 4 lety +10

    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;

    • @Alikhan1993
      @Alikhan1993 Před 2 lety +1

      you need swap logic otherwise it will fail for [1,0] input

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @vinayak186f3
    @vinayak186f3 Před 3 lety

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

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @riyarana1353
    @riyarana1353 Před 2 lety

    Great explanation! Really appreciate it.

  • @RohanSaluja
    @RohanSaluja Před 2 lety +1

    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

    • @supriyamanna715
      @supriyamanna715 Před rokem

      ig on lc for this u will get MLE

    • @Kevin-xp1dp
      @Kevin-xp1dp Před 7 měsíci

      When you remove a zero from the list, you have to shift all the items afterwards down an index, which is O(n). So your method would be O(n^2).

  • @navinchainani4721
    @navinchainani4721 Před 4 lety +1

    Nice video bro

  • @shubham007121
    @shubham007121 Před 4 lety +1

    nice video

  • @ronsinha21
    @ronsinha21 Před rokem +1

    @TECH DOSE You said in the video..if you have a better logic then let me know.....So 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 .....The 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

  • @ilanaizelman3993
    @ilanaizelman3993 Před 2 lety

    Thx!

  • @imshivendra
    @imshivendra Před 2 lety +1

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

  • @dilipsuthar4206
    @dilipsuthar4206 Před 4 lety +2

    👌🏽👌🏽👌🏽

    • @raghukumar2874
      @raghukumar2874 Před 4 lety

      Dilip Suthar The concept is very well explained in this video infact i have my channel which has similar content.pls check, subscribe and show me your support if u like them and give me u r feedback in comment section.thank you:)

    • @raghukumar2874
      @raghukumar2874 Před 4 lety

      Dilip Suthar The concept is very well explained in this video infact i have my channel which has similar content.pls check, subscribe and show me your support if u like them and give me u r feedback in comment section.thank you:)

  • @vireshdeshamane
    @vireshdeshamane Před 2 lety

    This code is showing time exceeded.. :(
    void moveZeroes(vector& nums)
    {
    int i=0;
    int x=0;
    while(i

  • @pritamsarbajna681
    @pritamsarbajna681 Před 2 lety

    can you tell me why can't this algorithm is not passing all the test cases:
    class Solution {
    public:
    void moveZeroes(vector& nums) {
    vector:: iterator itr = nums.begin();
    int count = 0;
    for(int i=0; i

  • @akashacharya2143
    @akashacharya2143 Před rokem

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

  • @divyamaheshwari4066
    @divyamaheshwari4066 Před 3 lety +1

    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.

  • @GurpreetSingh-ps6kl
    @GurpreetSingh-ps6kl Před 2 lety +1

    thank u

  • @IamShivaniPandit
    @IamShivaniPandit Před 3 lety

    Your code is not working for this data {0,1,0,3,12}.

  • @sahilbisht3661
    @sahilbisht3661 Před 4 lety +2

    If first element is non zero and second element is also non zero like [1,2,4,6] then after first swap array will change to [2,1.....] Which is not allowed so if first element is nonzero then what we do ?

    • @b.sainathreddy4567
      @b.sainathreddy4567 Před 3 lety

      #include
      using namespace std;
      int main()
      {
      int n;
      coutn;
      int arr[n];
      cout

    • @mohammedthahajk7619
      @mohammedthahajk7619 Před 3 lety +2

      I had the same doubt.
      Since left and right are both starting at the same point the swap happens with the same number. Hence 1 is swapped with 1 and both pointers are incremented by 1.
      Tweak the algorithm a little bit to skip swapping number with itself

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @jasmeenkaur6001
    @jasmeenkaur6001 Před 3 lety +1

    Also make the feb 2021 leetcode playlist

  • @karthikeyanannamalai2805
    @karthikeyanannamalai2805 Před 4 lety +1

    How do you handle the two pointers' method with the following inputs?
    int[] nums = new int[]{1, 3, 12,0,0};
    int[] nums = new int[]{1, 3, 12};

    • @techdose4u
      @techdose4u  Před 4 lety +1

      You can dry run this input using 2-ptr technique. When swap happens then left and right are actually pointing to the same value and so element remains at the same position. Nothing is altered :)

    • @yashgoswami5374
      @yashgoswami5374 Před 4 lety +1

      @@techdose4u we can modify program by adding extra check that swap only if left pointer is pointing to 0(zero).can we?
      It can reduce the number of swaps but it is adding extra condition check.
      So, which is batter,
      more swaps and less conditions
      Or
      Less swaps and more conditions
      ????????????

    • @techdose4u
      @techdose4u  Před 4 lety

      Actually you should not put condition for left but only right. This will make the logic much simpler. You can add a case if both left and right are pointing to same index then increment both (if value is not zero) otherwise if they don't point to same index then left will always have zeroes. So this logic won't work. We want to swap when right points to zero and not left. I hope you got the point. Take some examples and see.

    • @yashgoswami5374
      @yashgoswami5374 Před 4 lety

      @@techdose4u yes, I got your point but i just want to ask you
      which is batter,
      more swaps and less conditions
      Or
      Less swaps and more conditions
      ????????????

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @navinchainani4721
    @navinchainani4721 Před 4 lety +1

    Bro if we not write if(n==0 ) ||n==1) the code will run?

  • @SurajGupta-zx5lj
    @SurajGupta-zx5lj Před 4 lety +1

    what about using a queue instead.. when find zero push to the bottom

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Don't use queue. Instead just count the 0s using a counter variable.

    • @SurajGupta-zx5lj
      @SurajGupta-zx5lj Před 4 lety

      @@techdose4u ok sir :)

  • @sabrinarahman2156
    @sabrinarahman2156 Před 3 lety +1

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

    • @techdose4u
      @techdose4u  Před 3 lety +1

      😅 welcome

    • @rohithv63
      @rohithv63 Před 3 lety

      Sorry, here we don't want to return anything

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @pl5778
    @pl5778 Před rokem

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

  • @randy4443
    @randy4443 Před 4 lety +1

    What is the first two elements are zero?

    • @techdose4u
      @techdose4u  Před 4 lety

      Try to dry run. Its simple. You will get it.

  • @LDarshanKasar
    @LDarshanKasar Před rokem

    void moveZeroes(vector& nums) {

    for(auto it=nums.begin() ; it != nums.end() ;it++){

    if(*it == 0){
    nums.erase(it);
    it--;
    nums.push_back(0);
    }
    }
    }
    why is this giving TLE?

  • @paraslohia9292
    @paraslohia9292 Před 2 lety +1

    What if the 0th element is non zero?

    • @sauravjos
      @sauravjos Před rokem

      Swapped with itself. Both left and right pointers move ahead. Problem space reduced by 1.

  • @jsarvesh
    @jsarvesh Před 3 lety +3

    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

    • @imshivendra
      @imshivendra Před 2 lety

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

  • @awakashsinha9926
    @awakashsinha9926 Před 4 lety +1

    I would suggest to submit solution a day later

    • @techdose4u
      @techdose4u  Před 4 lety +1

      It's true that you should first solve it and then see the video. Solutions become available once you solve it. I will post the videos late anyways in weekdays. Does it create any problem for others? Do you find any inconvenience ?

    • @awakashsinha9926
      @awakashsinha9926 Před 4 lety +1

      @@techdose4u actually I think when solution is there we don't push ourselves enough to solve by own

    • @techdose4u
      @techdose4u  Před 4 lety

      Yea true. But actually you should try the question first and solve it. It is not advised to spend the entire day on these questions. Actually answers are available at many sources of CZcams itself during the competition. But I would advise you to not watch the video. Only when you have fully tried then only watch. You should make this resolve. I hope this should be fine. Anyways on weekdays you won't see me post early due to my own work 😅

    • @awakashsinha9926
      @awakashsinha9926 Před 4 lety

      @@techdose4u yeah! BTW thanks for all these videos they are really helpful and also Google codejam was till today if u could post videos from there it will be highly appreciated

  • @santhipriyadinakaran
    @santhipriyadinakaran Před měsícem

    Why ++left, and not left ++

  • @awakashsinha9926
    @awakashsinha9926 Před 4 lety +1

    If first element is non zero then when we r never incrementing left

    • @techdose4u
      @techdose4u  Před 4 lety

      If element pointed by right pointer is not zero then we swap with left otherwise we just increment right ptr.

    • @anuabraham2524
      @anuabraham2524 Před 4 lety +4

      Well in the beginning both left n rt pointer points to 1st elt
      Since it is non zero swap will happen but since both index are same swap is meaningless and both pointer will move fwd. Hope youve got my point 🙂✌️

    • @Horodgamin
      @Horodgamin Před 4 lety

      czcams.com/video/ScwiXBGyVz4/video.html

  • @botkiller563
    @botkiller563 Před 3 lety

    If the first element is non zero then what to do🙄

    • @rajeshg8649
      @rajeshg8649 Před 2 lety

      Bro start, i with 0 and j with 1:
      Conditions:
      >> if(i==j || arr[i] == arr[j]) { j++; }
      >> else if(i!=j and arr[i]!=0) { i++, j++; }
      >> else { arr[i]=arr[j], arr[j]=0, i++, j++; }
      By this approach, if the first element is non zero then both i and j will be increamented.

  • @stankafan6688
    @stankafan6688 Před 4 lety

    /*the easiest implementation*/
    int s=nums.size();
    int i,k=0;
    for(i=0;i

  • @aravindk8181
    @aravindk8181 Před 2 lety +1

    how to think like this

  • @khuzhi.sharma
    @khuzhi.sharma Před 2 lety

    wrong aa rha h run krke yeh soln

  • @__nitinkumar__
    @__nitinkumar__ Před 2 lety

    Your statement is incorrect here 3:16

  • @abhaytiwari6411
    @abhaytiwari6411 Před 4 lety

    hey tech dose please make video in hindi so that we can understand easily

    • @techdose4u
      @techdose4u  Před 4 lety +3

      Hindi will not be possible bro. Those who are programming will obviously have to learn English and appear for interviews in english language only. So you should try to understand English. If you need any advice then ask me because I was not able to speak English myself in college 😅

    • @abhaytiwari6411
      @abhaytiwari6411 Před 4 lety +1

      i understand english but but we understand more easily in hindi
      because hindi is our mother tongue
      that's why i said that

    • @techdose4u
      @techdose4u  Před 4 lety

      True. But you need to get good at english quickly because software companies always prefers communication skills. It is extremely important.

    • @abhaytiwari6411
      @abhaytiwari6411 Před 4 lety +1

      ya i heared about that

  • @b.sainathreddy4567
    @b.sainathreddy4567 Před 3 lety

    #include
    using namespace std;
    int main()
    {
    int n;
    coutn;
    int arr[n];
    cout