IDNLearn.com offers a collaborative platform for sharing and gaining knowledge. Join our community to access reliable and comprehensive responses to your questions from experienced professionals.

given an integer x and an array of numbers, find if two numbers in the array sum up to x. (or, find the pair of numbers whose sum is closest to x.)

Sagot :

The program for finding the two numbers in the array sum up to x is made.

Explain the term sorting?

  • Sorting involves putting data into a meaningful order so how you can more efficiently evaluate it.
  • For instance, if you want to plot a graph of sales performance, you could wish to sort the sales data per calendar month.

Array A[n], x

  1. Sort by the use of Mergesort ------- O(nlogn)
  2. Initialize 2 index variables to estimate candidit.
  3. Initialize 1st to that leftmost index: l = 0
  4. Initializ 2nd to that rightmost index: r = n - 1
  5. While l < r -----------O(n)
  6. if A[l] + A[r] == x; return true
  7. else if A[l] + A[r] < sum; increment l
  8. else decrement r
  9. return false

Time Complexity:

As was covered in class, step one employs mergesort, which has an O(nlogn) complexity.

Since the worst-case scenario involves traversing through the full array, step three consumes O(n) time.

Total Running Time = O(nlogn + n) = O(nlogn)

Thus, the program for finding the two intergers in the array sum up to x is made.

To know more about the sorting, here

https://brainly.com/question/1385135

#SPJ4

Your engagement is important to us. Keep sharing your knowledge and experiences. Let's create a learning environment that is both enjoyable and beneficial. Your questions deserve accurate answers. Thank you for visiting IDNLearn.com, and see you again for more solutions.