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

preview_player
Показать описание

Watch all my playlist here:

Want to land a software engineering job in the IT industry? This course - 'Visualizing Data Structures and Algorithms' is here to help. The course walks you through multiple Java algorithms, data structures problems, and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.

The course covers in and outs of Data Structures and Algorithms in Java. Java is used as the programming language in the course. Students familiar with Javascript, Python, C#, C++, C, etc will also get to learn concepts without any difficulty. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers many interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -

1. Animated slide. (To make visualization of algorithms faster)
2. Coding algorithm on IDE.

The course covers topics such as -
0. Algorithm Analysis
1. Arrays
2. Matrix
3. Singly Linked List
4. Doubly Linked List
5. Circular Singly Linked List
6. Stacks
7. Queues
8. Binary Tree
9. Binary Search Tree
10. Graphs
11. Priority Queues and Heaps
12. Recursion
13. Searching
14. Sorting
15. Strings
16. Trie Data Structure
17. Dynamic Programming
and many more ...

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

Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over YouTube with java implementation and found this. Hats Off To You Man....Excellent Work. GOD BLESS YOU :)

shubhamagarwal
Автор

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<array.length;i++) {
if(array[i]!=0) {
array1[j]=array[i];
j++;
}
}

for(int i:array1) {
System.out.println(i);
}
}

}

ALJ_
Автор

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.

shibaramsathua
Автор

😮 Very good .Well done .Respect for you.

harshvardhansingh
Автор

Amazing explanation with really awesome animated content

achiever
Автор

Very nice explanation sir thankyou 🙏🏼🙏🏼🙏🏼

someshsahu
Автор

Sir please Upload A Video about How To Build Our Own Logic To Solve DS&algo Problems

StoicX
Автор

Great vedeo sir 👍ur teaching style is totally amazing 🙏

pawankumar-liib
Автор

Sir The logic you said for reversing the array won't work for this problem?

prabhu
Автор

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

karentechnologies
Автор

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

nafishasantonmoy
Автор

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

dineshkm
Автор

can you please tell me about the time complexity of the given algorithm?

MohdDanish-kvry
Автор

good amazing algo but you can use another way to swap without using temp

arr[ j ] =arr [ i ];
arr[ i ] =0 ;

SOUALMIABDENNOUR
Автор

this solution will not work for this input {1, 2, 0, 0, 0, 3, 6};
please check

harshsinha
Автор

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

chinniachari
Автор

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

themesiah
Автор

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.

vamshiraj
Автор

please try this solution instead
public static int[] movetoEnd(int arr[]) {

int j=0;
for(int i=0;i<arr.length;i++) {

if(arr[i]!=0) {

int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
j++;


}}
return arr;

}

harshsinha
Автор

Jaffa nupetni vedio focus gavinalani but artham kakunte code chudalani but title vesi

siva-mto