Find two numbers that sum closest to a given number
Your approach is good. Improvement you can do is use a better sorting technique and rest the approach you are using seems best one.


Sort all the input numbers (Use better sorting techniques Eg. Quicksort).
Use two index variables l and r to traverse from left and right ends respectively. Initialize l as 0 and r as n-1.
sum = a[l] + a[r]
If sum is less than number, then l++
If sum is greater than number, then r–-.
Keep track of min sum
Repeat steps 3, 4, 5 and 6 while l &lt; r.


Time complexity: complexity of quick sort + complexity of finding the optimum pair 

= O(nlogn) + O(n) = O(nlogn)