Pair Sum Problem Statement

Given an integer array ARR of size N and an integer S, your task is to return a list of all pairs of elements such that the sum of each pair equals S.

Input:

The first line contains two space-separated integers N and S, representing the size of the array and the target sum.
The second line contains N space-separated integers, representing the elements of the array ARR.

Output:

For each pair, print a line containing two space-separated integers indicating the numbers in the pair. Pairs should be sorted in non-decreasing order based on the first element, and if two pairs have the same first element, the pair with the smaller second element should come first.

Example:

Input:
N = 5, S = 5
ARR = [1, 2, 3, 4, 2]
Output:
1 4
2 3
2 3

Constraints:

  • 1 <= N <= 103
  • -105 <= ARR[i] <= 105
  • -2 * 105 <= S <= 2 * 105
  • Time Limit: 1 sec

Note:

Ensure the pairs are sorted as mentioned and implement the function without handling output print operations as this is pre-managed.

AnswerBot
5d

The task is to find pairs of elements in an array that sum up to a given target value.

  • Iterate through the array and for each element, check if the complement (target - current element) exists in a has...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter