
- 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
Count number of distinct pairs whose sum exists in the given array in C++
We are given an array of, let's say, arr[] of integer values of any respective size and the task is to calculate the count of the number of distinct pairs available in a given array whose sum also exists in the same array.
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Points to remember
A pair will be counted once with the same elements irrespective of their orders. For example, (3,2) and (2,3) will be counted as 1.
If there is a number occurring multiple times in an array then it will be considered exactly twice to form one pair. For example, if an array has elements as {2, 2, 2, 2} then the pair will be (2,2) and it will be counted as 1.
For Example
Input − int arr = {6, 4, 10, 14} Output − count is 2
Explanation − pairs with the sum in an array are (6,4) and (10,4) so count is 2
Input − int arr = {6, 6, 6 ,6, 6, 13} Output − count is 0
Explanation − there is no pair in an array with the sum in the same array. So, count is 0.
Approach used in the below program is as follows
Create an array let’s say, arr[]
Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.
Take a temporary variable that will store the count of elements.
Create a map type variable let’s say mp
Start loop for i to 0 and i less than the size of an array
Create another map of pairs type variable let’s say par
Start loop for i to 0 and i less than the size of an array
Inside the loop, start another loop with j to i+1 and j less than the size of an array
Inside the loop, check if mp[arr[i]+arr[j]] > 0 AND pr[{arr[i], arr[j] }] =0 then increment the count by 1
Increment par[{ arr[i], arr[j] }] by 1
Increment par[{ arr[j], arr[i] }] by 1
Return the count
Print the result.
Example
#include <iostream> #include <map> using namespace std; // Returns number of pairs in ar[0..n-1] with // sum equal to 'sum' int countpairs(int ar[], int n){ // Store counts of all elements in map m // to find pair (ar[i], sum-ar[i]) // because (ar[i]) + (sum - ar[i]) = sum map<int, int> mymap; for (int i = 0; i < n; i++){ mymap[ar[i]]++; } // To remove duplicate items we use result map map<pair<int, int>, int> p; int result = 0; // Considering all pairs for (int i = 0; i < n; i++){ for (int j = i + 1; j < n; j++){ // If sum of current pair exists if (mymap[ar[i] + ar[j]] > 0 && p[{ ar[i], ar[j] }] ==0){ result++; } // Inserting the current pair both ways to avoid // duplicates. p[{ ar[i], ar[j] }]++; p[{ ar[j], ar[i] }]++; } } return result; } // main function int main(){ int ar[] = { 6, 4, 10, 14 }; int n = sizeof(ar) / sizeof(ar[0]); cout << "count is "<<countpairs(ar, n); return 0; }
Output
If we run the above code we will get the following output −
count is 2
- Related Articles
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Program to count number of fraction pairs whose sum is 1 in python
- Count of pairs in an array whose sum is a perfect square in C++
- Pair whose sum exists in the array in JavaScript
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- Count pairs in a sorted array whose sum is less than x in C++
- Count pairs with given sum in C++
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Count pairs whose products exist in array in C++
- Count distinct pairs from two arrays having same sum of digits in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Number of pairs whose sum is a power of 2 in C++
