Question

In: Computer Science

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 = 4
If 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} and B[1..n] = {1, 9, 10, 15}
Then nth smallest integer is 8.

B: Modify your program in A to find the kth smallest number with k < n. Your program must run in O(log n) time.

For example:

n = 4 and k=3
If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}
The kth (i.e. 3rd) smallest integer is 4.
If A[1..n] = {2, 5, 8, 13} and B[1..n] = {1, 9, 10, 15}
Then kth smallest integer is 5.

C: Modify your program in A to find the kth smallest number when k > n by finding the jth largest number in the 2n combined elements where j=2n-k+1. Your program must run in O(log n) time.

For example:

n = 4 and k=6

If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}
The kth (i.e. 6th) smallest integer is also the jth (2*4-6+1=3rd) largest integer in the two combined arrays, which is 7.

Solutions

Expert Solution

A.

package demo;

import java.util.Scanner;

public class DemoTranslation {
   public static void main() {
       int n, temp;
       System.out.print("Enter the size of arrays:");
       n = STDIN_SCANNER.nextInt();
       int[] a = new int[n], b = new int[n], c = new int[2 * n];
       System.out.println("Enter the elements of array A");
       for(int i = 0; i < n; i++) {
           a[i] = STDIN_SCANNER.nextInt();
           c[i] = a[i];
       }
       System.out.println("Enter the elements of array B");
       for(int i = 0; i < n; i++) {
           b[i] = STDIN_SCANNER.nextInt();
           c[i + n] = b[i];
       }
       for(int i = 0; i < 2 * n; i++) {
           System.out.print(c[i] + " ");
       }
       for(int i = 0; i < 2 * n; i++) {
           for(int j = i + 1; j < 2 * n; j++) {
               if(c[i] > c[j]) {
                   temp = c[i];
                   c[i] = c[j];
                   c[j] = temp;
               }
           }
       }
       System.out.print("\n" + n + "th smallest element is " + c[n - 1]);
   }

   public final static Scanner STDIN_SCANNER = new Scanner(System.in);
}

For parts A and B, the same logic is applied. Just use the value of k accordingly.


Related Solutions

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...
Step (D) of the divide-and-conquer strategy (i.e. combine the solutions to smaller instances of the problem...
Step (D) of the divide-and-conquer strategy (i.e. combine the solutions to smaller instances of the problem to obtain the solution of the original instance) is not a necessary step for this design strategy. Mergesort is an example of such cases. Select one: True False
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
Write a program to solve the boundary value problem ? ′′ = ? ′ + 2?...
Write a program to solve the boundary value problem ? ′′ = ? ′ + 2? + cos ? for ? ? [0, ?/2] with ?( 0) = 0.3, ?( ?/ 2) = 0.1. Check your numerical solution with actual using necessary plot.(MATLAB)
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Write a linear program for the following problem. (Do not solve.) A ship is transporting rice...
Write a linear program for the following problem. (Do not solve.) A ship is transporting rice and wheat from California to Alaska. It has three cargo holds with the following capacities: • The forward cargo hold can carry at most 10,000 tons, and at most 400,000 cubic feet . • The middle cargo hold can carry at 5,000 tons, and at most 250,000 cubic feet. • The aft cargo hold can carry at most 12,000 tons, and at most 600,000...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal algorithms (preorder, inorder, and postorder) for binary trees. Assuming that your algorithm is recursive, find the number of recursive calls made.
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of...
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of the array have equal values. For simplicity, suppose the array size is n=2k for some integer k. Input S is the starting index, and n is the number of elements starting at S. The initial call is SAME(A, 0, n). Boolean SAME (int A[ ], int S, int n) { Boolean T1, T2, T3; if (n == 1) return TRUE; T1 = SAME (A,...
• You should use the divide-and-conquer strategy and write multiple functions. • You should always use...
• You should use the divide-and-conquer strategy and write multiple functions. • You should always use arrays for the data sequences. • You should always use const int to define the sizes of your arrays. a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally, the program shall tell which sequence has a larger average. For...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT