-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMoveOnes.java
More file actions
39 lines (33 loc) · 960 Bytes
/
Copy pathMoveOnes.java
File metadata and controls
39 lines (33 loc) · 960 Bytes
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
package interviewQuestions;
import java.util.Arrays;
import java.util.stream.IntStream;
public class MoveOnes {
public static void main(String[] args) {
int[] arr = {4, 0, 2, 0, 7, 1, 0, 6, 8};
moveOnes(arr);
// moveOnes1(arr);
}
static void moveOnes(int[] arr1) {
int index = 0;
int n = arr1.length;
for(int i = 0; i < n; i++) {
if(arr1[i] != 0) {
int temp = arr1[index];
arr1[index] = arr1[i];
arr1[i] = temp;
index++;
}
}
for(int i=0; i<n; i++) {
System.out.print(arr1[i]);
}
}
static void moveOnes1(int[] arr1) {
int[] result =
IntStream.concat(
Arrays.stream(arr1).filter(x -> x != 0), // all non-zero values
Arrays.stream(arr1).filter(x -> x == 0) // all zeros at the end
).toArray();
System.out.println(Arrays.toString(result));
}
}