| 1 | public class Main |
| 2 | { |
| 3 | public static void main(String[] args) { |
| 4 | // int arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8}; |
| 5 | // int arr[] = {1, 2, 3, -4, -1, 4}; |
| 6 | int arr[] = {2,-8,-3,-5,-7,-1,-2}; |
| 7 | int output[]=new int[arr.length]; |
| 8 | output=rearrange(arr,arr.length); |
| 9 | print(output); |
| 10 | |
| 11 | } |
| 12 | public static void print(int output[]) |
| 13 | { |
| 14 | for(int i=0;i<output.length;i++) |
| 15 | { |
| 16 | System.out.print(output[i]+" "); |
| 17 | } |
| 18 | } |
| 19 | public static void rotate(int nums[],int start, int end) |
| 20 | { |
| 21 | int temp=nums[end]; |
| 22 | for(int i=end-1;i>=start;i--) |
| 23 | { |
| 24 | nums[i+1]=nums[i]; |
| 25 | } |
| 26 | nums[start]=temp; |
| 27 | } |
| 28 | public static int[] rearrange(int nums[], int n) |
| 29 | { |
| 30 | int i=0,j=0,k=0; |
| 31 | while(k<n && i<n && j<n) |
| 32 | { |
| 33 | if(k%2==0) |
| 34 | { |
| 35 | if(nums[k]>=0) |
| 36 | { |
| 37 | i=k; |
| 38 | j=k; |
| 39 | while(i<n && nums[i]>=0) i++; |
| 40 | if(i>=n) break; |
| 41 | else rotate(nums,j,i); |
| 42 | } |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | if(nums[k]<0) |
| 47 | { |
| 48 | i=k; |
| 49 | j=k; |
| 50 | while(j<n && nums[j]<0) j++; |
| 51 | if(j>=n) break; |
| 52 | else rotate(nums,i,j); |
| 53 | } |
| 54 | } |
| 55 | k++; |
| 56 | } |
| 57 | return nums; |
| 58 | } |
| 59 | } |
nothing calls this directly
no outgoing calls
no test coverage detected