Question

In: Computer Science

I need to find the kth smallest element in the union of 2 sorted arrays in...

I need to find the kth smallest element in the union of 2 sorted arrays in O(log |A| + log |B|) time. I understand this is similar to binary search, but I don't understand which parts of the arrays I can throw out at each level of recursion, and why. In the pseudocode below, what should go into $1 through $16 and why?

// A and B are each sorted into ascending order, and 0 <= k < |A|+|B|
// Returns the element that would be stored at index k if A and B were
// combined into a single array that was sorted into ascending order.
select (A, B, k)
    return select(A, 0, |A|-1, B, 0, |B|-1, k)

select(A, loA, hiA, B, loB, hiB, k)
    // A[loA..hiA] is empty
    if (hiA < loA)
        return B[k-loA]
    // B[loB..hiB] is empty
    if (hiB < loB)
        return A[k-loB]
    // Get the midpoints of A[loA..hiA] and B[loB..hiB]
    i = (loA+hiA)/2
    j = (loB+hiB)/2
    // Figure out which one of four cases apply
    if (A[i] > B[j])
        if (k <= i+j)
            return select(A, $1, $2, B, $3, $4, k);
        else
            return select(A, $5, $6, B, $7, $8, k);         
    else
        if (k <= i+j)
            return select(A, $9, $10, B, $11, $12, k);
        else
            return select(A, $13, $14, B, $15, $16, k);

Solutions

Expert Solution

$1 = Alo,$2 = i,$3, = Blo,$4 = Bhi

$5 = Alo,$6 = Ahi,$7 = j,$8 = Bhi

$9 = Alo,$10 = Ahi,$11 = Blo,$12 = j

$13 = i,$14 = Ahi,$15 = Blo,$16 = Bhi

Explanation:

This is a divide and conquer algorithm. There is a minor improvement in the algorithm which can be made by dividing both the arrays in k/2 instead of their respective mid-points.

The motivation of the algorithm is that we want to find some index I in A and J in B such that I + J = k, in this case max(A[I], B[J]) is the kth smallest element.

I will explain to you the pseudo code by going through the important parts line by line.

1. select (A, B, k)
return select(A, 0, |A|-1, B, 0, |B|-1, k)

This line is the initial call to our recursive function so Alo = 0 and Ahi = |A|-1.

2.if (hiA < loA)
        return B[k-loA]
    if (hiB < loB)
        return A[k-loB]

In these lines, if while comparing we reach the end of an array we just return the required element from the other array.

3.i = (loA+hiA)/2
    j = (loB+hiB)/2
    // Figure out which one of four cases apply
    if (A[i] > B[j])
        if (k <= i+j)
            return select(A, $1, $2, B, $3, $4, k);
        else
            return select(A, $5, $6, B, $7, $8, k);         
    else
        if (k <= i+j)
            return select(A, $9, $10, B, $11, $12, k);
        else
            return select(A, $13, $14, B, $15, $16, k);

This is the most important step in recursion. To get a better understanding to think of this recursive step as trimming the two arrays to achieve the above result.

If the mid element of a is greater than mid element of b

  • If mid-index of a + mid-index of b is less than k
    • we can ignore the first half of b
  • else we can safely ignore the second half of a

If mid element of a is less than mid element of b

  • If mid-index of a + mid-index of b is less than k
    • ignore the first half of a, adjust k.else ignore the first half of a
  • else we can ignore the second half of b.

Related Solutions

Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Problem 3: Minimum In this problem, we will write a function to find the smallest element...
Problem 3: Minimum In this problem, we will write a function to find the smallest element of a list. We are, in a sense, reinventing the wheel since the min() function already performs this exact task. However, the purpose of this exercise is to have you think through the logic of how such a function would be implemented from scratch. Define a function named minimum(). The function should accept a single parameter named x, which is expected to be a...
I'm trying to get the union of two arrays and I tried making a loop that...
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
Find the smallest n ∈ N such that 2(n + 5)^2 < n^3 and call it...
Find the smallest n ∈ N such that 2(n + 5)^2 < n^3 and call it n^0,Show that 2(n + 5)^2 < n^3 for all n ≥ n^0.
JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
I need to find the mean and standard deviation for this data SE-FamilySize 2 2 1...
I need to find the mean and standard deviation for this data SE-FamilySize 2 2 1 2 4 4 3 2 1 4 2 4 2 4 3 4 4 3 5 5 6 2 3 4 3 3 2 4 2 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT