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.
AnswerBot
4mo

Given an array of integers and a target, find all pairs of elements that add up to the target.

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

Help your peers!
Select
Add answer anonymously...

Eternal Limited Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Write a SQL query to find the X percentile of students.
A Software Developer Intern was asked Q. Convert a Binary Tree to a Sum Tree Given a binary tree of integers, convert the...read more
A Software Developer Intern was asked Q. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Write a SQL query to find the X percentile of students.
A Software Developer Intern was asked Q2. Convert a Binary Tree to a Sum Tree Given a binary tree of integers, convert the...read more
A Software Developer Intern was asked Q3. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
Eternal Limited Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits