How to move Zeroes to end of an Array? | Animation

  Рет қаралды 40,855

Dinesh Varyani

Dinesh Varyani

Күн бұрын

Пікірлер: 63
@itsdineshvaryani
@itsdineshvaryani 4 жыл бұрын
Please *Like* , *Comment* , *Share* , *Subscribe* and *Click Bell* 🔔🔔🔔 Icon for More Updates. To get *Data Structures and Algorithms* complete course for free please follow this link - kzbin.info/aero/PL6Zs6LgrJj3tDXv8a_elC6eT_4R5gfX4d
@shubhamagarwal1434
@shubhamagarwal1434 2 жыл бұрын
Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over KZbin with java implementation and found this. Hats Off To You Man....Excellent Work. GOD BLESS YOU :)
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Thanks !!!
@ritamdas_ju
@ritamdas_ju Жыл бұрын
same comment in every video🙄🙄
@ALJ_8840
@ALJ_8840 Жыл бұрын
package BASICS; public class array5 { public static void main(String[] args) { // TODO Auto-generated method stub int [] array= {0,0,1,2,1,2,0,1,1,8,5,0,0,0,9,0,0,0,-9,100}; int [] array1=new int[array.length]; for(int i=0,j=0;i
@harshvardhansingh5647
@harshvardhansingh5647 Жыл бұрын
😮 Very good .Well done .Respect for you.
@itsdineshvaryani
@itsdineshvaryani Жыл бұрын
Thanks
@achiever27
@achiever27 3 жыл бұрын
Amazing explanation with really awesome animated content
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@shibaramsathua6683
@shibaramsathua6683 3 ай бұрын
could you explain the actual algorithm before showing the exact code to do the problem , it is not an effective way to understand form the answer to question , actually we have to explain how the answer comes out from the question , and differnet way to solve it.. By the way you are a great teahcer and learning from you is valuable to me.
@nafishasantonmoy2272
@nafishasantonmoy2272 9 ай бұрын
I have taken two pointers approach for this problem- static int[] moveZeroesToEnd(int[] array){ //two pointers approach int start = 0; int end = array.length-1; while(start < end){ if(array[end] == 0){ end--; continue; } if(array[start] == 0){ int temp = array[end]; array[end--] = array[start]; array[start] = temp; } start++; } return array; }
@someshsahu4638
@someshsahu4638 Жыл бұрын
Very nice explanation sir thankyou 🙏🏼🙏🏼🙏🏼
@itsdineshvaryani
@itsdineshvaryani Жыл бұрын
Welcome
@karentechnologies3990
@karentechnologies3990 2 жыл бұрын
Here's another algorithm to achieve the same goal : public int[] shiftZerosToEnd(int[] numbers){ int[] result = new int[numbers.length]; int position = 0; for (int val : numbers){ if (val != 0){ result[position] = val; position++; } } return result; }
@wizardop2100
@wizardop2100 2 жыл бұрын
Thanks 🙏🏻
@AvionInn
@AvionInn Жыл бұрын
it does not work for {0,0,2,3,4,0,0} this array
@AvionInn
@AvionInn Жыл бұрын
sorry it worked , my bad
@shubhamagarwal1434
@shubhamagarwal1434 Жыл бұрын
Comparing: 1. Both have same time complexity O(n) 2. Space taken by your algo is more as it requres an extra array.
@LuizFelipe-ow2gf
@LuizFelipe-ow2gf Жыл бұрын
i think this algorithm is better because it doesn't access and return a reference to the numbers array, correct me if i'm wrong
@venkatramreddysomala14900
@venkatramreddysomala14900 11 ай бұрын
Best explanation, Thank you
@itsdineshvaryani
@itsdineshvaryani 11 ай бұрын
Glad you liked it !!!
@pawankumar-li9ib
@pawankumar-li9ib 3 жыл бұрын
Great vedeo sir 👍ur teaching style is totally amazing 🙏
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@dineshkm7832
@dineshkm7832 6 ай бұрын
This one is more complet time and memory .. for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) { nums[nonZeroIndex++] = nums[i]; } } // see while below then add 0 at end to match array size // Fill the remaining elements with zeroes while (nonZeroIndex < nums.length) { nums[nonZeroIndex++] = 0; }
@prabhu5569
@prabhu5569 Жыл бұрын
Sir The logic you said for reversing the array won't work for this problem?
@StoicX7
@StoicX7 2 жыл бұрын
Sir please Upload A Video about How To Build Our Own Logic To Solve DS&algo Problems
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Will add !!!
@DILIPKUMAR-qc8zw
@DILIPKUMAR-qc8zw 3 жыл бұрын
Great explanation...Thanks a lot
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Welcome !!!
@lordstark6927
@lordstark6927 4 жыл бұрын
Simply awesome👏
@itsdineshvaryani
@itsdineshvaryani 4 жыл бұрын
Thanks !!!
@MohdDanish-kv8ry
@MohdDanish-kv8ry 3 жыл бұрын
can you please tell me about the time complexity of the given algorithm?
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
O(n)
@chinniachari
@chinniachari Жыл бұрын
Another simple way of doing it, private static void zerosAtLast(int arr[]){ int zeroIndex = arr.length-1; for (int i = 0; i < zeroIndex; i++) { //No need to iterate till arr.length if (arr[i] == 0){ arr[i] = arr[zeroIndex]; arr[zeroIndex--] = 0; while (arr[zeroIndex]==0 && i > zeroIndex){ zeroIndex--; } } } System.out.print("Zeros at last = "); for (int i = 0; i < arr.length; i++) { System.out.print(" - "+arr[i]); } }
@swapniljagatap3533
@swapniljagatap3533 3 жыл бұрын
Thanks 👍👍👍👍
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
welcome !!!
@abhishekranjan1094
@abhishekranjan1094 3 жыл бұрын
Very nice sir
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Welcome !!!
@siva-m2t7o
@siva-m2t7o 6 ай бұрын
Code not visible
@SOUALMIABDENNOUR
@SOUALMIABDENNOUR Жыл бұрын
good amazing algo but you can use another way to swap without using temp arr[ j ] =arr [ i ]; arr[ i ] =0 ;
@harshsinha6067
@harshsinha6067 Жыл бұрын
this solution will not work for this input {1, 2, 0, 0, 0, 3, 6}; please check
@ahmetyasarozer
@ahmetyasarozer Жыл бұрын
Nope my friend, it works. I've tested just now.
@saidbifalankeep
@saidbifalankeep Жыл бұрын
it's work
@themesiah8429
@themesiah8429 Жыл бұрын
I am able to think of this algo better but seems lengthy. I wonder if it impacts time-complexity much. Is it greedy? loop over j looking for a zero - if j finds zero, loop i from (j+1) till end of array -- Case 1: if i finds a non-zero number, swap and break out of i loop -- Case 2: if i reaches end of array, no non-zero number was found for swap and array can be returned. Thoughts? public int[] function(int[] arr) { for (int j = 0; j < arr.length; j++) { if (arr[j] == 0) { for (int i = j + 1; i < arr.length; i++) { if (arr[i] != 0) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; break; } if(i == (arr.length - 1)){ return arr; } } } } return arr; }
@harshsinha6067
@harshsinha6067 Жыл бұрын
please try this solution instead public static int[] movetoEnd(int arr[]) { int j=0; for(int i=0;i
@vamshiraj7097
@vamshiraj7097 Жыл бұрын
You are the just explaining the solution, but you are not explaining how you come up with that solution.We have to learn how to approach to solve a problem not just merly understand the solution. I really appreciate your work but this is my opinion.
@NFM-nb7dl
@NFM-nb7dl 3 жыл бұрын
💙
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
thanks !!!
@siva-m2t7o
@siva-m2t7o 6 ай бұрын
Jaffa nupetni vedio focus gavinalani but artham kakunte code chudalani but title vesi
@pavanrajp4633
@pavanrajp4633 3 жыл бұрын
👍
@chaitanyasingh8620
@chaitanyasingh8620 4 жыл бұрын
👍👍👍👍🏻
@itsdineshvaryani
@itsdineshvaryani 4 жыл бұрын
Thanks !!!
@beanneverseengaming3915
@beanneverseengaming3915 Жыл бұрын
import java.util.Arrays; public class MoveZerosAtEnd { public static void main(String[] args) { int arr[]={5,0,0,2,3,6}; newArr(arr); System.out.println(Arrays.toString(arr)); } static int[] newArr(int[] arr){ int end=arr.length-1; for (int i = 0; i
@jkrollin9768
@jkrollin9768 Жыл бұрын
it swaps zero at the end with zero in beginning
@siva-m2t7o
@siva-m2t7o 6 ай бұрын
Title vesi moddagudipiccha
@prajwal.gadakh
@prajwal.gadakh Жыл бұрын
Why you haven't used, if and else if ? Why you used if, if ?????
@saidbifalankeep
@saidbifalankeep Жыл бұрын
because which write else if the logic will be like this(the consition if is true the else if won't be execute because if was true but made if,if that meanse check first-> if is true perform it and check also the second if -> if is true will be perform it that's different i hope you understand it
@prajwal.gadakh
@prajwal.gadakh Жыл бұрын
@@saidbifalankeep yeah got it, thank you so much.
Find Second Maximum value in an Array | Animation
12:51
Dinesh Varyani
Рет қаралды 37 М.
How to move Zeroes to end of an Array? | Implementation
5:59
Dinesh Varyani
Рет қаралды 21 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 112 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 36 МЛН
POTD-31/10/2023 | Move all zeroes to end of array | Problem of the Day GeeksforGeeks
10:49
Why Floyd's Cycle Detection algorithm works?
19:30
Dinesh Varyani
Рет қаралды 21 М.
Shift All Zero Elements to Right Side of the Array - Java interview Question
15:05
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Winning Google Kickstart Round C 2020
30:57
William Lin (tmwilliamlin168)
Рет қаралды 4 МЛН
How to find nth node from the end of a Singly Linked List in Java?
13:17
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 112 МЛН