30

Pair Sum

Difficulty: EASY
Avg. time to solve
15 min
Success Rate
90%

Problem Statement
Suggest Edit

Given an integer array arr of size N and an integer S. Return the list of all pairs of elements such that for each sum of elements of each pair equals to S.

Note:

Each pair should be sorted i.e the first value should be less than or equals to the second value. 

Return the list of pairs sorted in non-decreasing order of their first value. In case if two pairs have the same first value, the pair with a smaller second value should come first.
Input Format:
The first line of input contains two space-separated integers N and S, denoting the size of the input array and the value of S.

The second and last line of input contains N space-separated integers, denoting the elements of the input array: arr[i] where 0 <= i < N.
Output Format:
 Print C lines, each line contains one pair i.e two space-separated integers, where C denotes the count of pairs having sum equals to given value S.
Constraints:
1 <= N <= 10^5
-10^5 <= A[i] <= 10^5
-2*10^5 <= S <= 2*10^5

Time Limit: 1 sec
Sample Input 1:
5 5
1 2 3 4 5
Sample Output 1:
1 4
2 3
Sample Input 2:
5 0
2 -3 3 3 -2
Sample Output 2:
-3 3
-3 3
-2 2
Want to solve this problem? Login now to get access to solve the problems