median of 2 sorted array - by binary method

84 views Asked by At

This is a binary method for finding the median in two arrays of equal length that are arranged in ascending order. The method returns an integer. Specifically, when the median is not a whole number, the method rounds it down. For example:

If the arrays a1 and a2 are as follows:

a1 = {1, 12, 15, 26, 38}
a2 = {12, 13, 18, 30, 45}

The method will return the value 16. After merging the two arrays into one sorted array, we get:

{1, 12, 12, 13, 15, 18, 26, 30, 38, 45}

The two middle numbers in the array are 15 and 18, and the average between them is 16 (rounded down due to dividing by wholes). Therefore, the method is designed to return 16.

 /**
     * Finds the median of two sorted arrays of equal length.
     * Assumes that the input arrays are sorted in ascending order.
     *
     * The time complexity of this algorithm is O(log n), where n is the length of the input arrays. This is because the
     * binary search is performed until the subarrays reduce to a size of 2.
     * The space complexity is O(1) since the method uses a constant amount of extra space regardless of the input size.
     * The pointers and variables do not scale with the input size.
     *
     * @param arr1 The first sorted array.
     * @param arr2 The second sorted array.
     * @return The median value of the combined arrays.
     */
    public static int findMedian(int[] arr1, int[] arr2) {
        // Get the length of the arrays (assumed to be equal)
        int n = arr1.length;

        // Initialize pointers for binary search
        int left1 = 0;
        int right1 = n - 1;
        int left2 = 0;
        int right2 = n - 1;

        // Base cases for arrays of length 0, 1, or 2
        if (n == 0)
            return 0;
        if (n == 1)
            return (arr1[0] + arr2[0]) / 2;
        if (n == 2)
            return (Math.max(arr1[0], arr2[0]) + Math.min(arr1[1], arr2[1])) / 2;

        // Binary search to find the median
        while (right1 - left1 > 1 || right2 - left2 > 1) {
            // Calculate midpoints for both arrays
            int mid1 = (left1 + right1) / 2;
            int mid2 = (left2 + right2) / 2;

            // Get the middle elements of both arrays
            int midElement1 = arr1[mid1];
            int midElement2 = arr2[mid2];

            // Check if the medians are equal
            if (midElement1 == midElement2) {
                return midElement1;
            }

            // Adjust pointers based on comparison of medians
            if (midElement1 < midElement2) {
                left1 = mid1;
                right2 = mid2;
            } else {
                right1 = mid1;
                left2 = mid2;
            }
        }

        // Calculate and return the rounded median for even-length arrays
        return (Math.max(arr1[left1], arr2[left2]) + Math.min(arr1[right1], arr2[right2])) / 2;
    }

The method does not work well when I enter arrays of even length, such as:

{1, 3, 5, 7}, {2, 4, 6, 8}

In this case the method returns 3 instead of 4. How can I fix it? I need the method to return an int and not a double.

0

There are 0 answers