forked from itsjohnty/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovingZeroToEnd.java
67 lines (66 loc) · 1.63 KB
/
movingZeroToEnd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class MoveZeros
{
// a method that moves the zeros
// to the end of the array inputArr
public int[] solve(int[] inputArr)
{
int size = inputArr.length;
// creating an auxiliary array for
// storing the result
int auxiliaryArray[] = new int[size];
// for counting the total number
// of elements whose value is
// zero in the input array
int zeroCount = 0;
// the current index of the auxiliary array
int outputIndex = 0;
for(int i = 0; i < size; i++)
{
if(inputArr[i] != 0)
{
// a non-zero element is found
// hence, we store it in the auxiliary array
// and increase the outputIndex by 1
auxiliaryArray[outputIndex] = inputArr[i];
outputIndex = outputIndex + 1;
}
else
{
// a zero element is found
// hence, increase the zeroCount by 1
zeroCount = zeroCount + 1;
}
}
while(zeroCount != 0)
{
// adding all of the zeroes at the end
// of the auxiliary array
auxiliaryArray[outputIndex] = 0;
outputIndex = outputIndex + 1;
zeroCount = zeroCount - 1;
}
return auxiliaryArray;
}
// main method
public static void main(String[] argvs)
{
// creating an object of the class MoveZeros
MoveZeros obj = new MoveZeros();
// input 1
int arr[] = {6, 7, 0, 2, 1, 78, 0, 56, 0, 4};
int size = arr.length;
int ans[] = obj.solve(arr);
System.out.println("For the following array: ");
for(int i = 0; i < size; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("The answer after moving the zero at the end is: ");
for(int i = 0; i < size; i++)
{
System.out.print(ans[i] + " ");
}
System.out.println();
}
}