Question

Suppose that you have two sorted numerical arrays A[1 . . . m] and B[1 ....

Suppose that you have two sorted numerical arrays A[1 . . . m] and B[1 . . . n]. You want to compute the k’th smallest number in the merged array of all m + n elements. Please design a divide- and-conquer algorithm that can do so in O(log(m + n)). You can assume for simplicity that k is even.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

By using a divide and conquer approach, similar to the one used in binary search, we can attempt to find the k’th element in a more efficient way.

Explanation:
We compare the middle elements of arrays arr1 and arr2,
let us call these indices mid1 and mid2 respectively.

Let us assume arr1[mid1]  k, then clearly the elements after 
mid2 cannot be the required element. We then set the last
element of arr2 to be arr2[mid2].

In this way, we define a new subproblem with half the size
of one of the arrays.

Below is the implementation of function in cpp

int kth(int *arr1, int *arr2, int *end1, int *end2, int k)

{

    if (arr1 == end1)

        return arr2[k];

    if (arr2 == end2)

        return arr1[k];

    int mid1 = (end1 - arr1) / 2;

    int mid2 = (end2 - arr2) / 2;

    if (mid1 + mid2 < k)

    {

        if (arr1[mid1] > arr2[mid2])

            return kth(arr1, arr2 + mid2 + 1, end1, end2,

                k - mid2 - 1);

        else

            return kth(arr1 + mid1 + 1, arr2, end1, end2,

                k - mid1 - 1);

    }

    else

    {

        if (arr1[mid1] > arr2[mid2])

            return kth(arr1, arr2, arr1 + mid1, end2, k);

        else

            return kth(arr1, arr2, end1, arr2 + mid2, k);

    }

}

Note that in the above code, k is 0 indexed, which means if we want a k that’s 1 indexed, we have to subtract 1 when passing it to the function.
Time Complexity: O(log (n + m))

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Suppose that you have two sorted numerical arrays A[1 . . . m] and B[1 ....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] ·...

    Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....

  • 1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers,...

    1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.

  • Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct...

    Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)

  • Suppose you are given k sorted arrays of size n. Give an algorithm, that runs in...

    Suppose you are given k sorted arrays of size n. Give an algorithm, that runs in O(nk log k)time, that merges them into a single list.

  • (25pts) You are given two sorted lists of size m and n. Give an O(log m...

    (25pts) You are given two sorted lists of size m and n. Give an O(log m log n) time algorithm for computing the k-th smallest element in the union of the two lists Note that the only way you can access these values is through queries to the databases. Ina single query, you can specify a value k to one of the two databases, and the chosen database will return the k-th smallest value that it contains. Since queries are...

  • Please answer by mathematical language: An array of n elements is almost sorted if and only...

    Please answer by mathematical language: An array of n elements is almost sorted if and only if every element is at most k spots away from its actual location. Assuming that you can only perform pairwise comparisons, formally prove that any algorithm which can sort almost sorted arrays must have running time Ω(n log k), You may assume that n is a multiple of k.

  • I need help with this .java implementation: Implement an algorithm that given two sorted arrays of...

    I need help with this .java implementation: Implement an algorithm that given two sorted arrays of size m and n creates a sorted array of size (n + m) in O(n + m) time. The script must take as an input a command line argument specifying the name of a .txt file which will contain the arrays to be sorted. Each line in the .txt file will contain two arrays, with elements in arrays separated by commas and the arrays...

  • You are interested in analyzing some hard-to-obtain data from two separate databases. Each database contains n...

    You are interested in analyzing some hard-to-obtain data from two separate databases. Each database contains n numerical values—so there are 2n values total—and you may assume that no two values are the same. You’d like to determine the median of this set of 2n values, which we will define here to be the nth smallest value. However, the only way you can access these values is through queries to the databases. In a single query, you can specify a value...

  • Suppose that we are given a sorted array of distinct integers A[1, ......,  n] and we want...

    Suppose that we are given a sorted array of distinct integers A[1, ......,  n] and we want to decide whether there is an index i for which A[i] = i. Describe an efficient divide-and-conquer algorithm that solves this problem and explain the time complexity. 1. Describe the steps of your algorithm in plain English. 2. Write a recurrence equation for the runtime complexity. 3. Solve the equation by the master theorem.

  • You are given an input of k arrays each of size n. Each one of the...

    You are given an input of k arrays each of size n. Each one of the k arrays is sorted. Give an algorithm that turns the k sorted arrays into one sorted array of k * n elements. Please explain the algorithm in words and analyze the run time.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT