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

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

Dinesh Varyani

Dinesh Varyani

Күн бұрын

Пікірлер: 63
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
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🙄🙄
@harshvardhansingh5647
@harshvardhansingh5647 Жыл бұрын
😮 Very good .Well done .Respect for you.
@itsdineshvaryani
@itsdineshvaryani Жыл бұрын
Thanks
@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
@achiever27
@achiever27 3 жыл бұрын
Amazing explanation with really awesome animated content
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@shibaramsathua6683
@shibaramsathua6683 2 ай бұрын
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.
@pawankumar-li9ib
@pawankumar-li9ib 3 жыл бұрын
Great vedeo sir 👍ur teaching style is totally amazing 🙏
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@someshsahu4638
@someshsahu4638 Жыл бұрын
Very nice explanation sir thankyou 🙏🏼🙏🏼🙏🏼
@itsdineshvaryani
@itsdineshvaryani Жыл бұрын
Welcome
@venkatramreddysomala14900
@venkatramreddysomala14900 10 ай бұрын
Best explanation, Thank you
@itsdineshvaryani
@itsdineshvaryani 10 ай бұрын
Glad you liked it !!!
@lordstark6927
@lordstark6927 3 жыл бұрын
Simply awesome👏
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@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 Жыл бұрын
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
@DILIPKUMAR-qc8zw
@DILIPKUMAR-qc8zw 3 жыл бұрын
Great explanation...Thanks a lot
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Welcome !!!
@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; }
@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 !!!
@prabhu5569
@prabhu5569 Жыл бұрын
Sir The logic you said for reversing the array won't work for this problem?
@abhishekranjan1094
@abhishekranjan1094 3 жыл бұрын
Very nice sir
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Welcome !!!
@MohdDanish-kv8ry
@MohdDanish-kv8ry 3 жыл бұрын
can you please tell me about the time complexity of the given algorithm?
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
O(n)
@swapniljagatap3533
@swapniljagatap3533 3 жыл бұрын
Thanks 👍👍👍👍
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
welcome !!!
@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]); } }
@SOUALMIABDENNOUR
@SOUALMIABDENNOUR Жыл бұрын
good amazing algo but you can use another way to swap without using temp arr[ j ] =arr [ i ]; arr[ i ] =0 ;
@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; }
@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
@siva-m2t7o
@siva-m2t7o 6 ай бұрын
Code not visible
@NFM-nb7dl
@NFM-nb7dl 3 жыл бұрын
💙
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
thanks !!!
@harshsinha6067
@harshsinha6067 Жыл бұрын
please try this solution instead public static int[] movetoEnd(int arr[]) { int j=0; for(int i=0;i
@pavanrajp4633
@pavanrajp4633 3 жыл бұрын
👍
@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.
@chaitanyasingh8620
@chaitanyasingh8620 3 жыл бұрын
👍👍👍👍🏻
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@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; }
@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 ай бұрын
Jaffa nupetni vedio focus gavinalani but artham kakunte code chudalani but title vesi
@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
Рет қаралды 36 М.
How to move Zeroes to end of an Array? | Implementation
5:59
Dinesh Varyani
Рет қаралды 21 М.
Happy birthday to you by Secret Vlog
00:12
Secret Vlog
Рет қаралды 6 МЛН
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 24 МЛН
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 8 МЛН
Generate Binary numbers from 1 to n using a Queue | Animation
18:38
Dinesh Varyani
Рет қаралды 16 М.
Next Greater Element | Animation | Coding Interview
21:23
Dinesh Varyani
Рет қаралды 17 М.
How to find nth node from the end of a Singly Linked List in Java?
13:17
How to Reverse an Array in Java - Animation
8:47
Dinesh Varyani
Рет қаралды 40 М.
How to search an element in a Singly Linked List in Java ?
10:37
Dinesh Varyani
Рет қаралды 49 М.
Why Floyd's Cycle Detection algorithm works?
19:30
Dinesh Varyani
Рет қаралды 21 М.
How to find start of a loop in a Singly Linked List? (Animation)
12:45
Happy birthday to you by Secret Vlog
00:12
Secret Vlog
Рет қаралды 6 МЛН