• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 07, 2022 |730 Views
Given two unsorted arrays, find all pairs whose sum is x
  Share   Like
Description
Discussion

As we know sorting algorithms can sort data in O (n log n) time. So we will choose a O (n log n) time algorithm like : Quick Sort or Heap Sort. For each element of second array , we will subtract it from K and search it in the first array.
First sort the given array using a O(n log n) algorithm like Heap Sort or Quick Sort.
Run a loop for each element of array-B (0 to n).
Inside the loop, use a temporary variable say temp, and temp = K – B[i].
Search the temp variable in the first array i.e. A, using Binary Search(log n).


Given two unsorted arrays, find all pairs whose sum is x  : https://www.geeksforgeeks.org/given-two-unsorted-arrays-find-pairs-whose-sum-x/

Read More