Question

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. 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).

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

/*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:)

Add a comment
Know the answer?
Add Answer to:
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[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
  • (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...

    (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...

    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 ...

    (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,...

    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 P[1 · · · n] records the daily net profit GreatStore earns from days...

    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...

  • Given an n*m chocolate bar, you need to break it into n*m 1*1 pieces. You can...

    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

  • Pa Suppose that (X1, X2) ~ N(0,0,1,1,0). It follows from this that the joint PDF of...

    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...

  • Consider the problem where you are given an array of n digits [di] and a positive...

    Consider the problem where you are given an array of n digits [di] and a positive integer b, and you need to compute the value of the number in that base. In general, you need to compute For example: (1011)2 = 1(1) + 1(2) + 0(4) + 1(8) = 11; (1021)3 = 1(1) + 2(3) + 0(9) + 1(27) = 34, and (1023)4 = 3(1) + 2(4) + 0(16) + 1(64) = 75. In these examples, I give the digits...

  • Pleass write Java Program with comments. For this peoblem it supposed to create ur own list of po...

    Pleass write Java Program with comments. For this peoblem it supposed to create ur own list of points, then use that list to find the closest point. A list of points and the points have x and y values such as [(1,2),(3,5),(6,7),....] that’s just example. Implement the algorithm to meet the following requirements Define a class named Pair with data fields pl and p2 to represent two points and a method named getDistance) that returns the distance between the two...

  • Suppose that, even unrealistically, we are to search a list of 700 million items using Binary...

    Suppose that, even unrealistically, we are to search a list of 700 million items using Binary Search, Recursive (Algorithm 2.1). What is the maximum number of comparisons that this algorithm must perform before finding a given item or concluding that it is not in the list “Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into n subinstances of size n/3, and the dividing and combining steps take linear time. Write a...

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
ADVERTISEMENT