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

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
Рекомендации по теме
Комментарии
Автор

I also tried moving all zeroes to the start of the array. Following your algorithm I played a little bit and changed the direction of for loop to read the elements in reverse order and the if(arr[j] != 0) { j--; }
Here is the code:
public void moveZeros(int[] arr, int n) {
int j = n-1;
for(int i = n-1; i >= 0; i--) {
if(arr[i] != 0 && arr[j] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
if(arr[j] != 0) {
j--;
}
}
}
I am still beginner so I hope it works for everybody !!
Thanks for the lesson!

ivelingachev
Автор

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
Автор

You are most underrated channel,
This question was asked to me for Amazon.

ERDheerajmann
Автор

I followed your DSA series and able to solve this problem in the following way. Please correct if am wrong.
public static void main(String[] args) {
int[] A = { 9, 8, 7, 6, 5, 0, 4, 0, 3, 2, 0, 1 };
int[] B = new int[A.length];

int count = 0;
for (int i=0; i<A.length; i++){
if(A[i] != 0 ){
B[count] = A[i];
count++;
}
}

for (int i=0; i<B.length; i++)
System.out.print(B[i] + " ");
}

shikanderraja
Автор

It would be more easier if we check that whenever we have zero in our array if left shifts at that particular index and then assigning the last index value as 0

thakurji
Автор

sir can you please explain the part in which you called method moveZeroes to arraydemo

Acousticanjali
Автор

Hi Sir,
There's some issue with this algorithm, I tried this algorithm with 2 different values - 1st (int b[] = {0, 1, 2, 0, 4, 0, 5};) and 2nd ( int b[] = {0, 1, 0, 2, 1, 0, 3};). In both the cases the output was wrong, the last element of the array is not getting swapped with 0. where as your values are giving perfect output.

AiyushSaini
Автор

Hi Sir, Here Kumar from Andhra Pradesh I completed udemy course on DS but there i didn't See these type of solving and animations, I happy to see and I understand Better with Animations.
The above problem is working well but some of the test cases it is not Working
Ex test Case 1: 9, 8, 7, 6, 5, 0, 4, 0, 3, 2, 0, 1 Expected Output :9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0 Actual Output:9, 8, 7, 6, 5, 4, 3, 1, 0, 2, 0, 0
Test Case 2: 0, 1, 0, 2, 1, 0, 3 Expected Output:1, 2, 1, 3, 0, 0, 0 Actual Output: 1, 2, 3, 0, 1, 0, 0
Can You Please Check It Sir

ChinnaKumarReddy
Автор

For 0, 1, 0, 3, 12...the output is wrong sir, can you figure it out sir?

DURGADEVIPRASANNAVETSA
Автор

EASIEST SOLUTION :)

int number = 0 ;
for(int i = 0 ; i < arr.length ; i++){
if(arr[i] != number){
int temp = arr[i];
arr[i] = arr[number];
arr[number] = temp ;
number++ ;
}
}

godl_disaster
Автор

Video is blurred totally, pls upload clearly visible one sir, thanks

sreddy
Автор

sir i am writing like this why it is not working



package Array;

public class MoveZeroIntoEndOfArray {

static void printArray(int[]arr) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}

static void moveZeros(int[]arr, int n) {
int j=0;
for(int i=0; i<n; i++) {
if(arr[i] !=0 && arr[j]==0) {
int temp=arr[i];
arr[i]=arr[j];
arr[i]=temp;
}
if(arr[j] !=0) {
j++;
}
}
}

public static void main(String[] args) {
int arr[]= {1, 2, 0, 54, 87, 0, 12};
printArray(arr);
moveZeros(arr, arr.length);
printArray(arr);

}

}

subhankardash