Solution:
1. Sort the array → nlogn
2. Consider i=0 and j=n-1(Last element)
3. sum= arr[i] + arr[j]
4. If sum<x then i++ ( we need to increase the value of sum so we should increment i to increase the overall sum as the array is sorted)
5. Similarly if sum>x then j—
6. If sum==x then match found
Therefore total complexity= nlogn + n = nlogn
Additional info:
This problem can be solved in O(n) complexity as well using extra space, this is a famous problem known as “Two Sum”.