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

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Ninja And The Fence Problem Statement Ninja is given a task of painting a fence ...read more
A Software Developer Intern was asked Q2. Delete a Node from Linked List Problem Statement Given a linked list of integers...read more
A Software Developer Intern was asked Q3. Count Pairs with Given Sum Given an integer array/list arr and an integer 'Sum',...read more
Bajaj Finserv Health 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