Question

In: Computer Science

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.

Solutions

Expert Solution

CODE:

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

// function to search 'value' in the given array 'arr[]' it uses binary search technique as 'arr[]' is sorted
int isPresent(int arr[], int low, int high, int value)
{
   while (low <= high)
   {
       int mid = (low + high) / 2;
      
       // value found
       if (arr[mid] == value)
           return mid;     
          
       else if (arr[mid] > value)
           high = mid - 1;
       else
           low = mid + 1;
   }
  
   // value not found
   return -1;
}

// function to find pair from both the sorted arrays whose sum is equal to a given value
void Pairs(int arr1[], int arr2[], int m, int n, int x)
{
   int count = 0;     
   for (int i = 0; i < m; i++)
   {
       // for each arr1[i]
       int value = x - arr1[i];
      
       // check if the 'value' is present in 'arr2[]'
   int j = isPresent(arr2, 0, n - 1, value);
       if (j != -1)
           cout << i << " " << j << endl;
   }
}

// Driver Code
int main()
{
   int arr1[] = {1, 3, 5, 7};
   int arr2[] = {2, 3, 5, 8};
   int m = sizeof(arr1) / sizeof(arr1[0]);
   int n = sizeof(arr2) / sizeof(arr2[0]);
   int x = 10;
   Pairs(arr1, arr2, m, n, x);
   return 0;     
}

Method (Binary Search): For each element arr1[i], where 1 <= i <= m, search the value (x – arr1[i]) in arr2[]. If search is successful, increment the count.

Time Complexity: O(mlogn). Binary search time complexity: O(log n). So, for m elements time complexity is: O(mlogn)


Related Solutions

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.
(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
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
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.
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
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...
Show for the following recurrence that T(n) = T(n/3) + n*log(n) is O(n*log(n)) (not using the...
Show for the following recurrence that T(n) = T(n/3) + n*log(n) is O(n*log(n)) (not using the Master theorem please)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT