Welcome to Westonci.ca, where your questions are met with accurate answers from a community of experts and enthusiasts. Ask your questions and receive detailed answers from professionals with extensive experience in various fields. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

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

We hope you found this helpful. Feel free to come back anytime for more accurate answers and updated information. Your visit means a lot to us. Don't hesitate to return for more reliable answers to any questions you may have. We're glad you visited Westonci.ca. Return anytime for updated answers from our knowledgeable team.