Pair Sum Problem Statement

You are provided with an array ARR consisting of N distinct integers in ascending order and an integer TARGET. Your objective is to count all the distinct pairs in ARR whose sum equals TARGET.

Note:

  • Pair (x, y) and Pair (y, x) are considered as the same pair.
  • If no such pair exists with a sum equal to 'TARGET', then return -1.

Example:

Input:
ARR = [1, 2, 3], TARGET = 4
Output:
1
Explanation:

There exists only one pair in ARR with a sum of 4, which is (1, 3). (1, 3) and (3, 1) are counted as a single pair.

Constraints:

  • 1 ≤ T ≤ 100
  • 2 ≤ N ≤ 5000
  • 1 ≤ ARR[i], TARGET ≤ 105
  • The array ARR contains distinct elements.
AnswerBot
2mo

Count distinct pairs in a sorted array that sum up to a given target value, returning -1 if no pairs exist.

  • Two-Pointer Technique: Use two pointers, one at the start and one at the end of the array, to...read more

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