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

Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
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)
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
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...
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,...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT