Find All Pairs Adding Up to Target

Given an array of integers ARR of length N and an integer Target, your task is to return all pairs of elements such that they add up to the Target.

Input:

The first line contains an integer 'T' denoting the number of test cases.
Then, the test case follows.
The first line of each test case contains two space-separated integers 'N' and 'Target'.
The second line contains 'N' space-separated integers denoting the elements of the array.

Output:

For each test case, print pairs of integers denoting the elements which add up to the target.
Each pair must be printed in a new line.
If no pair is found, print (-1, -1).

Example:

For example, consider the following input/output:

Input:
2
5 9
2 7 4 5 6
4 10
1 2 8 4
Output:
2 7
4 5
-1 -1

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • -10^9 <= TARGET <= 10^9
  • -10^9 <= ARR[i] <= 10^9

Note:

You do not need to print anything; the function is already set up to handle output.
soumya ranjan nayak
2y

function printPairs(arr, n, sum)

{

// let count = 0; // Initialize result

// Consider all possible pairs and check

// their sums

for (let i = 1; i < n; i++)

for (let j = i + 1; j < n; j++)

if ...read more

Help your peers!
Add answer anonymously...
Kellton Software Engineer 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