Given an array of positive and negative integers, re-arrange it so that you have positive integers on one end and negative integers on other

后端 未结 30 2590
醉梦人生
醉梦人生 2020-12-07 07:37

I recently came across a Microsoft Interview Question for Software Engineer.

Given an array of positive and negative integers, re-arrange it so that you

30条回答
  •  半阙折子戏
    2020-12-07 08:39

    It can be done in O(n) and space O(1).

    We need to scan 3 times through the array and change some values carefully.

    Assumption: the max value in the array with size N is should be smaller than (N+1) * Integer.MAX_VALUE.

    We need this assumption since we well change some positive values in the array.

    • In the first scan, find # of negative and positive values, and the max.
    • In the second scan we create the negative section of array as follows:

    We start from the beginning of the array and we "swap" the first found positive number (e.g. at index i) with the first found negative number (e.g. j). Since negative numbers are being considered with respect to their location, the swap will be okay.

    The problem is the positive numbers because there might be some other positive numbers between i and j. To handle this issue, we have to somehow encode the index of the positive number in that value before swapping. So then we can realize where it was at the first point. We can do this by a[i]=(i+1)*(max)+a[i].

    • In the third scan, we create the positive section of array. by end of the second scan, the negative array is constructed, and the positive numbers are shifted to the right side, but their location may not be correct. So we go though it and correct their position since this info was encoded their value.

    Here is the code:

    import java.util.Arrays;
    
    public class LinearShifting {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = {-1,7,3,-5,4,-3,1,2};
            sort(a);
            System.out.println(Arrays.toString(a));  //output: [-1, -5, -3, 7, 3, 4, 1, 2]
        }
        public static void sort(int[] a){
            int pos = 0;
            int neg = 0;
            int i,j;
            int max = Integer.MIN_VALUE;
            for(i=0; imax) max = a[i];
            }
            max++;
            if(neg==0 || pos == 0) return;//already sorted
            i=0;
            j=1;
            while(true){
                while(i<=neg && a[i]<0) i++;
                while(j=0) j++;
                if(i>neg || j>=a.length) break;
                a[i]+= max*(i+1);
                swap(a,i,j);
            }
    
            i = a.length-1;
            while(i>=neg){
                int div = a[i]/max;
                if(div == 0) i--;
                else{
                    a[i]%=max;
                    swap(a,i,neg+div-2);// minus 2 since a[i]+= max*(i+1);
                }
            }
    
        }
        private static void swap(int[] a, int i , int j){
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    
    }
    

提交回复
热议问题