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
4mo
Count distinct pairs in an array whose sum equals a given target.
Use two pointers approach to iterate through the array and find pairs with sum equal to target.
Keep track of visited pairs to avoid cou...read more
Charan Singh
1y
function pairSum(sortedArray , target){ let count = 0 let leftPointer = 0 let rightPointer = sortedArray.length - 1 while(leftPointer < rightPointer){ let sum = sortedArray[leftPointer] + sortedArray[...read more
Help your peers!
Add answer anonymously...
Deloitte Software Developer interview questions & answers
A Software Developer was asked 2mo agoQ. How many ways can you define a bean using the Spring Boot Application annotation...read more
A Software Developer was asked 6mo agoQ. What type of work does the Deloitte company engage in?
A Software Developer was asked 7mo agoQ. What is MVC and explain its pattern?
Popular interview questions of Software Developer
A Software Developer was asked 6mo agoQ1. What is MVC and explain its pattern?
A Software Developer was asked 7mo agoQ2. Given a string, determine whether it is a palindrome, considering only alphanume...read more
A Software Developer was asked 7mo agoQ3. Given two strings s and t, return true if t is an anagram of s, and false otherw...read more
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

