forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveZeroesToLeft.java
76 lines (58 loc) · 1.95 KB
/
MoveZeroesToLeft.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
68
69
70
71
72
73
74
75
76
package arrays;
import java.util.Arrays;
public class MoveZeroesToLeft {
/*
* Given an integer array, move all elements containing '0' to the left while maintaining the order of
* other elements in the array.
* */
/*
* Runtime Complexity = Linear, O(n).
* Memory Complexity = Constant, O(1).
*
* Step 1: Keep two markers (readIndex and writeIndex) and point them to the end of the array
* Step 2: If readIndex < 0, skip
* Step 3: Iterate through the array in reverse, & if current element points to non zero,
* update the array current element to readindex element & increment writeIndex
* */
public static int[] moveZeroesToLeft(int[] arr) {
int readIndex = arr.length-1;
int writeIndex = arr.length-1;
while (readIndex >= 0) {
if(arr[readIndex] != 0) {
arr[writeIndex] = arr[readIndex];
writeIndex--;
}
readIndex--;
}
while (writeIndex >=0) {
arr[writeIndex] = 0;
writeIndex--;
}
return arr;
}
/*
* Given an integer array, move all elements containing '0' to the right while maintaining the order of
* other elements in the array.
* */
public static int[] moveZeroesToRight(int[] arr) {
int readIndex = 0;
int writeIndex = 0;
while (readIndex <arr.length) {
if(arr[readIndex] != 0) {
arr[writeIndex] = arr[readIndex];
writeIndex++;
}
readIndex++;
}
while (writeIndex < arr.length) {
arr[writeIndex] = 0;
writeIndex++;
}
return arr;
}
public static void main(String[] args) {
int[] arr = {1, 10, 20, 0, 59, 63, 0, 88, 0};
System.out.println(Arrays.toString(moveZeroesToLeft(arr)));
System.out.println(Arrays.toString(moveZeroesToRight(arr)));
}
}