-
Notifications
You must be signed in to change notification settings - Fork 1
/
StableRearrangePosNeg.java
executable file
·43 lines (38 loc) · 1.22 KB
/
StableRearrangePosNeg.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
// here 0 is considered as positive
import java.util.*;
class StableRearrangePosNeg{
public static void main(String[] args)
{
//int arr[n] = {-5, 3, 4, 5, -6, -2, 8, 9, -1, -4};
//int arr[] = {-5, -3, -4, -5, -6, 2 , 8, 9, 1 , 4};
//int arr[] = {5, 3, 4, 2, 1, -2 , -8, -9, -1 , -4};
//int arr[] = {-5, 3, -4, -7, -1, -2 , -8, -9, 1 , -4};
int arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8};
rearrange(arr);
System.out.println("RearrangeD array is "+ Arrays.toString(arr));
}
static void rearrange(int arr[]){
int outOfPlace =-1,idx;
for(idx = 0; idx < arr.length;idx++){
if(outOfPlace>=0){
if((arr[outOfPlace]<0 && arr[idx]>=0) || (arr[outOfPlace]>=0 && arr[idx]<0)){
rotateRight(arr,idx,outOfPlace);
if(idx-outOfPlace>=3)
outOfPlace+=2;
else
outOfPlace=-1;
}
}
if(outOfPlace<0){
if( ((arr[idx]>=0) && (idx & 0x01)==0) || ((arr[idx]<0) && (idx& 0x01)==1))
outOfPlace=idx;
}
}
}
static void rotateRight(int []arr,int curr,int out){
int tmp=arr[curr];
for(int i=curr;i>out;i--)
arr[i]=arr[i-1];
arr[out]=tmp;
}
}