Count Pairs with Given Sum
Given an integer array/list arr
and an integer 'Sum', determine the total number of unique pairs in the array whose elements sum up to the given 'Sum'.
Input:
The first line contains two space-separated integers, N and Sum.
The second line contains N space-separated integers representing the elements of the array.
Output:
Output a single integer, representing the total number of pairs whose sum is equal to 'Sum'.
Example:
Input:
5 7
1 2 3 4 3
Output:
2
Constraints:
1 ≤ N ≤ 105
-104 ≤ Sum ≤ 104
-104 ≤ arr[i] ≤ 104
- Time Limit: 1 sec
Note:
Array may contain duplicate elements.
The pairs (arr[i], arr[j]) and (arr[j], arr[i]) are considered the same.

AnswerBot
4mo
Count the total number of unique pairs in an array whose elements sum up to a given value.
Use a hashmap to store the frequency of each element in the array.
Iterate through the array and for each eleme...read more
Help your peers!
Add answer anonymously...
Interview Questions Asked to Software Developer Intern at Other Companies
Top Skill-Based Questions for Bajaj Finserv Health Software Developer Intern
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
C++ Interview Questions and Answers
150 Questions
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

