Search if two elements in a pre-sorted array add up to a specific value

I am working on a homework problem and am having difficulty creating an O (n * logn) solution. I need to write a function that takes a pre-sorted array and a search value. Then I need to find if any two elements of the array are summed in the same way as this value.

I need to create both O (n) and O (n * logn) algorithms for this.

O (n) was easy to create; however, I am having difficulty creating the O (n * logn) algorithm without adding some free code that actually does not help in solving the problem. If anyone could give me some pointers to what I can skip, this would be appreciated.

+5
source share
2 answers

Start with the first element and continue in sequence. While searching for the second element is done using binary search.

+4
source

Since they are pre-sorted, you can use binary search and linear search

0
source

All Articles