forked from ossamamehmood/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
positivenegativealt.java
57 lines (46 loc) 路 1.06 KB
/
positivenegativealt.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
package array;
public class positivenegativealt {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { -5, -2, 5, 2, 4, 7, 1, 8, 0, -8 };
posnegalt(arr);
}
// if order matters
public static void posnegalt(int[] arr) {
int i = 0;
while (i < arr.length) {
if (i % 2 == 0 && arr[i] >= 0 || i % 2 != 0 && arr[i] < 0) {
int wrongindex = i;
if (i % 2 == 0) {
while (i < arr.length && arr[i] >= 0) {
i++;
}
} else {
while (i < arr.length && arr[i] < 0) {
i++;
}
}
int lastindex = i;
if (lastindex == arr.length) {
break;
}
rotatearr(arr, wrongindex, lastindex);
i = wrongindex + 1;
} else {
i++;
}
}
for (
int j = 0; j < arr.length; j++) {
System.out.println(arr[j]);
}
}
private static void rotatearr(int[] arr, int wrongindex, int lastindex) {
// TODO Auto-generated method stub
int temp = arr[lastindex];
for (int i = lastindex - 1; i >= wrongindex; i--) {
arr[i + 1] = arr[i];
}
arr[wrongindex] = temp;
}
}