public void rerange(int[] A) {
        // write your code here
        int n = A.length;
        int positive = 0;
        int begin = 0, end = n - 1;
        for (int i : A)
            if (i > 0)
                positive += 1;
        while (begin + 1 <= end) {
            int val = A[begin];
            if (val > 0) {
                while (begin < end && A[end] > 0) {
                    --end;
                }
                swap(A, begin, end);
            }
            ++begin;
        }
        int negative = n - positive;
        begin = 1;
        end = n - 2;
        // if more negative, put negative on the end
        if (negative > positive) {
            swap(A, 1, n - 1);
            begin += 2;
            end = n - 3;
        } else if (negative < positive) {
            // if more positive numbers, put positive number on the head
            swap(A, 0, n - 2);
            begin = 2;
            end = n - 4;
        }
        while (begin + 1 <= end) {
            swap(A, begin, end);
            begin += 2;
            end -= 2;
        }
    }