Let there be an array: [6,9,2,4,1]
I need to find if input number have a pair or not.
Ex. Input - 7
Output - Yes (6+1 )
Input - 16
Output - No (no pair addition is 16)
I know the obvious way by running 2 loops but it have time complexity n^2 . Can someone help me with some optimized solutions ?
Programming Language : Java
What I tried :
1) Sort array
2) Based on input number divide it into 2 subarray (input/2).
3) Inner loop on first array and outer loop on second
This reduces iterations.