My task is in the title.
I guess a good start could be to use a hash function that seperates all number >Z from all numbers smaller Z. That would take O(n) time. But afterwards I would need to sort all Elements
My task is in the title.
I guess a good start could be to use a hash function that seperates all number >Z from all numbers smaller Z. That would take O(n) time. But afterwards I would need to sort all Elements
Given To paraphrase the question: given an array a with n numeric elements and a number Z find whether there are to elements a[x], a[y] such that a[x]+[y]=Z.
Approach: To do it in O(n) you could add each element to the array to a hash-set. - O(n).
Then another loop to check if Z-a[i] exists in the set another O(n) total O(n).
You can even combine the insertion and the check and do a few optimizations but it will still be O(n)