Find Pairs with Given Sum in a Sorted Array – Java Code

Find pairs with given sum in a sorted array.

Given an array A of size N. Write a code to find all pairs in the array that sum to a number equal to K. If no such pair exists then output will be 1.

NOTE – The array elements are distinct and in a sorted order.

For example –

Input :

arr[] = {1, 2, 3, 4, 5, 6, 7};
sum = 9

Output:

Pairs with given sum 9 is

Pair (2 , 7 )
Pair (3 , 6 )
Pair (4 , 5 )