Question

In: Computer Science

Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size...

Design an O(nlogn) 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.

Solutions

Expert Solution

Algorithm -

Step 1: Sort both the arrays A and B.

Step 2: Set i = 1 and j = n (Assuming array indices are from 1 to n)

Step 3: If i<=j, proceed to Step 4. Otherwise, return that no such i,j exist and terminate the algorithm.

Step 4: If A[i] + B[j] = v, then we have found i,j as needed. We can return i and j as the result and terminate the algorithm.

If A[i] + B[j] < v, then increment i by 1.

If A[i] + B[j] > v, then decrement j by 1.

Step 5: Goto Step 3.

Time Complexity -

We can sort both the arrays A and B in O( n log n ) time. (Step 1)

The loop will run for a maximum n times in the worst case. So, it will take O( n ) time.

Hence, the overall time taken is O ( n + n log n ) which is O( n log n ).

C++ Code -

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

int main() {
   int n,v;
   cout<<"Enter array size\n";
   cin>>n;
   vector<int> A(n);
   vector<int> B(n);
   int k;
   cout<<"Enter "<<n<<" values for array A(seperated by space)\n";
   for(k=0;k<n;k++)
       cin>>A[k];
   cout<<"Enter "<<n<<" values for array B(seperated by space)\n";
   for(k=0;k<n;k++)
       cin>>B[k];
   sort(A.begin(),A.end());
   sort(B.begin(),B.end());
   cout<<"Enter value of v\n";
   cin>>v;
   int i = 0, j = n-1;
   while(i<=j) {
       if(A[i]+B[j]==v) {
           cout<<"i and j exist such that A[i]+B[j]=v"<<" --> i = "<<i<<" j = "<<j<<endl;
           return 0;
       }
       else if(A[i]+B[j]<v)
           i++;
       else
           j--;
   }
   cout<<"No such i and j exist such that A[i]+B[j]=v\n";
   return 0;
}


Related Solutions

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.
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there...
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there is any name common to all three arrays, and if so, return the first such name.? 2. Given an input array with all equal values, compare the following sorting algorithm using big-O notation. Running Time Space Complexity Merge Sort Quick Sort Heap Sort
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one sorted array. Note the algorithm is not the one for merge sort. 2) What is the best asymptotic upper bound for the algorithm? List reasoning steps.
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
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...
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT