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
5d

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...
Bajaj Finserv Health Software Developer Intern 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