a[6]={1,2,3,4,5,8} sum =5 find the all pairs of integer sorted by array
Answers
Answer:
Sort the array elements into increasing order.
Walk along the array inwards from the ends, finding pairs with the correct sum. That is, maintain two “current indices” in the sorted array, initially referencing the first and last values. If the sum of the values at these indices is too high, decrement the higher index. If the sum is too low, increment the lower index. If the sum is correct, note this as least of the solution, and increment the lower index as well as the higher index. Obviously, you should stop if the two indices meet or cross in the middle, so the “lower” index becomes no less than the “higher” index.
It's worth thinking why this approach is guaranteed not to miss solutions.
Answer:
a[6]={1,2,3,4,5,8} sum =5 find the all pairs of integer sorted by array