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

Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
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
How can I check to see if 2 arrays contain the same element using nothing worse...
How can I check to see if 2 arrays contain the same element using nothing worse than linear runtime? (Java) For example if I have 2 arrays with elements {7, 8, 5, 4, 3} and {10, 12, 15, 20, 8} I would want it to return true since there is an 8 in each array.
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...
Find the largest and smallest element of a linked list, print total of all elements and...
Find the largest and smallest element of a linked list, print total of all elements and find out the average. Code needed in java
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.
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT