public double findMedianSortedArrays(int[] A, int[] B) {
        // write your code here
        int sizeA = A.length;
        int sizeB = B.length;
        int[] res = new int[sizeA + sizeB];
        int Aindex = sizeA - 1;
        int Bindex = sizeB - 1;
        int index = sizeA + sizeB - 1;
        while (Aindex >= 0 && Bindex >= 0) {
            if (A[Aindex] > B[Bindex]) {
                res[index--] = A[Aindex--]; 
            } else {
                res[index--] = B[Bindex--];
            }
        }
        while (Bindex >= 0) {
            res[index--] = B[Bindex--];
        }
        while (Aindex >= 0) {
            res[index--] = A[Aindex--]; 
        }
        double median = 0;
        if (res.length % 2 == 0){
            median = ((double)res[res.length / 2] + (double)res[res.length / 2 - 1]) / 2;
        } else {
            median = (double)res[res.length / 2];
        }
        return median;
    }

results matching ""

    No results matching ""