
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all pairs with given sum in C++
In this problem, we are given an array of integers and an integer sum and we have to print all pairs of integers whose sum is equal to the sum value.
Let’s take an example to understand the problem :
Input − array = {1, 6, -2, 3} sum = 4
Output − (1, 3) , (6, -2)
Here, we need pairs with the given sum value.
A simple solution to the problem will be checking pairs of elements that generate the sum. This can be done by traversing array and find the number in array that sums up to sum value.
Example
This program will illustrate the solution −
#include <iostream> using namespace std; int printPairsWithSum(int arr[], int n, int sum){ int count = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (arr[i] + arr[j] == sum) cout<<"[ "<<arr[i]<<", "<<arr[j]<<" ]\n"; } int main(){ int arr[] = {1, 6, -2, 3}; int n = 4; int sum = 4; cout<<"Pairs with Sum "<<sum<<" are :\n"; printPairsWithSum(arr, n, sum); return 0; }
Output
Pairs with Sum 4 are : [ 1, 3 ] [ 6, -2 ]
This method is easy to understand but not quite efficient. Another way will be using hashing.
We will initialise a hash table and traverse the array and find pairs in it. On match, we will print the array :
Example
The following program will make you understand the algorithm better −
#include <bits/stdc++.h> using namespace std; void printPairsWithSum(int arr[], int n, int sum){ unordered_map<int, int> pair; for (int i = 0; i < n; i++) { int rem = sum - arr[i]; if (pair.find(rem) != pair.end()) { int count = pair[rem]; for (int j = 0; j < count; j++) cout<<"["<<rem<<", "<<arr[i]<<" ]\n"; } pair[arr[i]]++; } } int main(){ int arr[] = {1, 6, -2, 3}; int n = 4; int sum = 4; cout<<"The pair with sum is \n"; printPairsWithSum(arr, n, sum); return 0; }
Output
Pairs with Sum 4 are : [ 1, 3 ] [ 6, -2 ]
- Related Articles
- Print all triplets with given sum in C++
- Print all pairs in an unsorted array with equal sum in C++
- Find all the pairs with given sum in a BST in C++
- Count pairs with given sum in C++
- Count all pairs with given XOR in C++
- Print all the sum pairs which occur maximum number of times in C++
- Print all pairs of anagrams in a given array of strings in C++
- Print all subarrays with 0 sum in C++
- Javascript Program to Count pairs with given sum
- Find pairs with given sum in doubly linked list in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Print all integers that are sum of powers of two given numbers in C++
- Print all possible sums of consecutive numbers with sum N in C++
- Sum of XOR of sum of all pairs in an array in C++
- Print all distinct permutations of a given string with duplicates in C++
