Posts

Showing posts from September, 2020

Find number of pairs in an sorted array whose sum is given number

Image
Number of pairs whose sum is given number here we have to find the pair of numbers from the sorted array whose sum must be equal to the given number. for example, suppose array A[10]={2,6,9,12,15,19,25,29,32,38} and given numer is 27. in this array, there are two pairs: (2,25) and (12,15). Logic:- first, we take a sorted array and check the sum of the last two numbers, if the sum is less than the given number then no such pair exists in the array, and if the sum is greater than the given number then there are possibilities of the existence of such pair. now we use only one loop to make the complexity be linear. C++ Result:- Note:- In Output, the result showing numbers are the addresses of numbers in that sorted array in which they are paired with.