Question

In: Computer Science

Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...

Divide and conquer problem.

Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.)
You can assume that the arrays contain no duplicate elements using divide and conquer.
(b) Now suppose we are given three sorted arrays A[1 . . . n], B[1 . . . n], and C[1 . . . n], and an integer k. Describe an algorithm to find the kth smallest element in A ∪ B ∪ C in O(log n) time using divide and conquer (not heap)

Solutions

Expert Solution

Solution:

  • Using the divide and conquer approach, below solution time complexity is: O(log n)
  • Check whether kth element lies in first half of either of the 2 arrays.
  • If yes, only seek kth element in first half only, else seek only in second half.
  • This recursively, reduce the original problem into a subproblem with smaller search space.
  • Check comments in the code section for a better understanding

Code: O(log n) solution

#include <bits/stdc++.h>   
using namespace std;

int kSmallest(int A[], int B[], int *endA, int *endB, int k)
{ 
    // if array A is empty, then return kth value in B 
    if(A==endA)
        return B[k];
    
    // if array B is empty, then return kth value in A
    if(B==endB)
        return A[k];
        
    // Find mid index of array A and B
    int midA = (endA - A)/2;
    int midB = (endB - B)/2;

    // If condition checks whether k lies in second half of either of the arrays 
    if(midA + midB < k){
        // when array A has more elements than B, ignore first half of B
        if(A[midA] > B[midB])
            return kSmallest(A, (B+midB+1), endA, endB, (k-midB-1));
        // otherwise first half of A
        else
            return kSmallest((A+midA+1), B, endA, endB, (k-midA-1));
    }
    // Else k lies in first half of either array A or B
    else{
        if(A[midA] > B[midB])
            return kSmallest(A, B, A+midA, endB, k);
        else
            return kSmallest(A, B, endA, B+midB, k);
    }
    
    return -1;
} 
  
// Driver code 
int main() 
{ 
    int A[6] = {1, 2, 5, 6, 7, 9}; 
    int B[4] = {0, 3, 8, 10}; 
  
    int k = 4;
    
    cout<<"Kth smallest number in union of A and B : "<< kSmallest(A, B, A+6, B+4, k-1); 
    return 0; 
} 

Input: (hardcoded)

int A[6] = {1, 2, 5, 6, 7, 9}; 
int B[4] = {0, 3, 8, 10}; 
int k = 4;
    

Output:

Kth smallest number in union of A and B : 3

Attached screenshot of code:

For question 2, extend same idea for 3 arrays, try do it on your own. Ask if you face any issue in implementation.

PS: Let me know if you have any doubt.


Related Solutions

Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an...
Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an efficient algorithm for finding the median of all numbers in arrays X0 and X1. Now, suppose you have not two, but three such arrays, each with n elements. Devise and analyze an efficient algorithm for finding the median. Do the same for n arrays, each with n elements.
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n...
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n x n each, find their multiplication matrix. Naive Method Following is a simple way to multiply two matrices.                void multiply(int A[][N], int B[][N], int C[][N]) {     for (int i = 0;   i < N; i++) {         for (int j = 0; j < N; j++) {             C[i][j] = 0;             for (int k = 0; k < N; k++) {                 C[i][j] += A[i][k]*B[k][j];             }...
You are given two arrays A1 and A2, each with n elements sorted in increasing order....
You are given two arrays A1 and A2, each with n elements sorted in increasing order. For simplicity, you may assume that the keys are all distinct from each other. Describe an o(log n) time algorithm to find the (n/2) th smallest of the 2n keys assuming that n is even.
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
1. If we view the selection sort as an application of "divide and conquer" strategy, what...
1. If we view the selection sort as an application of "divide and conquer" strategy, what is the two-part partitioning of the index range [0, n)? Is it a balanced partition? 2. For the "divide and conquer" strategy for developing algorithm, do we pursue balanced partitioning or unbalanced partitioning? Why? 3. For the quicksort, the selection of pivot determines the quality of partitioning. Do we have any easy or cheap way for selecting a good pivot that always leads to...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1?...
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1? + ?2?2 ... + ??−1?n-1 can be represented by an array ?[0. . ? − 1] of its n coefficients. Suppose P(x) and Q(x) are two polynomials of degree n-1, each given by its coefficient array representation. Their product P(x)Q(x) is a polynomial of degree 2(n-1), and hence can be represented by its coefficient array of length 2n-1. The polynomial multiplication problem is to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT