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. We can consider every pair of numbers, one taken from X and other from Y , using a double-nested loop and solve this in O(n 2 ) time (a faster algorithm than the ones proposed here exists, but the homework is about divide and conquer). (1) We want to design a better algorithm using divide and conquer. Let (X, Y ) refer to the given problem. Let T(n) be the complexity of your algorithm when each of X, Y has n elements. Consider each of X, Y divided into two equal halves as shown below: X = X1 X2 Y = Y1 Y2 If such a pair p, q of numbers existed, then p has to be in one of X1, X2, and q has to be in one of Y1, Y2. Thus, we can determine the answer to problem (X, Y ) from answers to the following four smaller problems of half the size: (X1, Y1), (X1, Y2), (X2, Y1), and (X2, Y2)
(d) (15 pts) Present complete pseudocode for solving the problem (X[i...j], Y [k...l]) using divide and conquer by specifying details of the following. Invoking the algorithm as zeroSum (X, 1, n, Y, 1, n) solves the problem (X, Y ).
// Solves the problem (X[i ... j], Y[k ... l])
// Returns true if some p in X[i ... j] and some q in Y[k ... l]
// add up to zero; returns false otherwise
// Pre: (1) X and Y are in nondecreasing order
// (2) (j-i+1) = (l-k+1), i.e., X, Y have equal number of elements
boolean zeroSum (X[], i, j, Y[], k, l)
{
}
You must clearly specify the base case(s).
/*steps to proceed:
1)write a function with arguments X array,Y array and with their lengths and 0.
2) implement that function as follows:
checksum(X,Y,m,n,p)
{
flag;
for loop from 0 to m;
value=p-X[i];
check a value in Y array(we can solve this in o(logn) time because array is sorted)
{
if(value )
flag=1;
return true;
}
return false;
} */
//full code in c++;
#include <bits/stdc++.h>
using namespace std;
// because array is sorted,we can search a element on (O(logn))
time.
bool isPresent(int arr[], int low,
int high, int value)
{
while (low <= high)
{
int mid = (low + high) / 2;
// value found
if (arr[mid] == value)
return true;
else if (arr[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
// value not found
return false;
}
int checksumpair(int arr1[], int arr2[],
int m, int n, int x)
{
int flag = 0;
for (int i = 0; i < m; i++)
{
int value = x - arr1[i];
if(flag==1)
{
return true;
}
if (isPresent(arr2, 0, n - 1, value))
{
flag=1;
}
}
return false;
}
// Driver Code
int main()
{
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 3, 5, 8};
int m = arr1.length();
int n = arr2.length();
int p = 0;
cout << checksumpair(arr1, arr2, m, n, p);
return 0;
}
//if you have any queries please ask me in the comment section;
// If you got the answer please upvote:)
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] ·...
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.
Consider n indivisible objects with distinct types i = 1, 2, 3,…, n. We are given positive integer weights W = {w1,w2...wn} and positive integer prices V = {v1,v2...vn} for the objects and a knapsack of weight capacity (m). Our problem is to find the maximum profit possible by including a subset of the objects into the knapsack with total weight of at most m. This form of the 0/1 Knapsack Problem can be solved by a Decrease and Conquer...
(5) Recall that X ~Uniform(10, 1,2,... ,n - 1)) if if k E (0, 1,2,... ,n -1, P(x k)0 otherwise (a) Determine the MGF of such a random variable. (b) Let X1, X2, X3 be independent random variables with X1 Uniform(10,1)) X2 ~Uniform(f0, 1,2]) Xs~ Uniform(10, 1,2,3,4]). X3 ~ U x2 ~ Uniform(10, 1,2)) 13Uniform Find the laws of both Y1 X1 +2X2 +6X3 and Y2 15X1 +5X2 + X3. (c) What is the correlation coefficient of Yi and ½?...
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...
(5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...
Suppose that (X1, X2) N (0,0,1,1,0). It follows from this that the joint PDF of (X1, X2) is given by Ixvx:(21,2) = exp (1} (27 +23)) Furthermore, if 1 Y Tā (X1 + X2) and Y2 (X1 - X2) Then (Y1,Y) ~ N(0,0,1,1,0) as well. (a) If X1 <X2, what are the possible values of Y¡ and Y2? (b) If Y, <0, what are the possible values of Xi and X,? (c) What is the marginal distribution of Yg? (d)...
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.
Suppose that P[1 · · · n] records the daily net profit GreatStore earns from days 1 to n. As with most stores, sometimes a P[i] is negative (i.e., when the store lost money that day) and sometimes it is positive (i.e., when the store made money that day). Alice, the owner of the store wants to use the information in P[1 · · · n]to plan for her vacation next year. Since she always has to worry about the...
Pa Suppose that (X1, X2) ~ N(0,0,1,1,0). It follows from this that the joint PDF of (X1, Xy) is given by fxıx(31,09) - exp(+34 +23) Furthermore, if GE Thte and 12(x3 + x2) - tao V5 (Xi-X) que Then (Y1,Y,) ~ N(0,0, 1, 1,0) as well. (a) If X, < X2, what are the possible values of Y1 and Y2? (b) If Y2 < 0, what are the possible values of X, and X,? (c) What is the marginal distribution...
Given an n*m chocolate bar, you need to break it into n*m 1*1 pieces. You can break a bar only in a straight line, and only one bar can be broken at a time. Design an algorithm that solves the problem with the minimum number of bar breaks. Decrease and Conquer Divide and Conquer Transform and Conquer