Skip to content

Commit 235483e

Browse files
Updated move-zeroes-end-1-traversal
1 parent 44f7b38 commit 235483e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Arrays/move_zeroes_end_1traversal.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//Move all zeros to End
2+
//Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
23
//Approach 2
34
//O(n) O(1)
45

56

6-
// i/p: n=5
7-
// 0 1 0 3 12
7+
// i/p: n=5, 0 1 0 3 12
88
// o/p: 1 3 12 0 0
99

10+
// Here we replace another loop with count
11+
1012
#include<iostream>
1113
using namespace std;
1214
void moveToEnd(int arr[],int n)
@@ -16,15 +18,15 @@ void moveToEnd(int arr[],int n)
1618
{
1719
if(arr[i]!=0)
1820
{
19-
swap(arr[i],arr[count]); //if we get 0 we ignore it
20-
count++; //and if we get non zero we swap with count
21-
// which point at 0
21+
//if we get 0 we ignore it
22+
//and if we get non zero we swap with count which point at 0
23+
swap(arr[i],arr[count]);
24+
count++;
25+
2226
}
2327
}
2428
}
2529

26-
// Here we replace another loop with count
27-
2830
int main()
2931
{
3032
int n,i;

0 commit comments

Comments
 (0)