- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find four elements a, b, c and d in an array such that a+b = c+d in C++
Suppose we have a list of integers. Our task is to find four distinct integers as two pairs like (a, b) and (c, d), such that a+b = c+d. If there are multiple answers, then print only one. Suppose the array elements are like: A = [7, 5, 9, 3, 6, 4, 2], then pairs can be (7, 3) and (6, 4)
Here we will use the hashing technique. We use the sum as key as pair as the value in the hash table. We have to follow these steps to solve this problem.
- For i in range 0 to n – 1, do
- for j in range i + 1 to n – 1, do
- find the sum
- If the hash table has the sum already, then print the previous pair and the current pair
- Otherwise, update the hash table.
- for j in range i + 1 to n – 1, do
Example
#include<iostream> #include<map> using namespace std; bool getTwoPairs(int arr[], int n) { map<int, pair<int, int> > hash_table; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int sum = arr[i] + arr[j]; if (hash_table.find(sum) == hash_table.end()) hash_table[sum] = make_pair(i, j); else { pair<int, int> pp = hash_table[sum]; cout << "(" << arr[pp.first] << " + " << arr[pp.second] << ") = (" << arr[i] << " + " << arr[j] << ")"; return true; } } } cout << "No pairs found"; return false; } int main() { int arr[] = {7, 5, 9, 3, 6, 4, 2}; int n = sizeof arr / sizeof arr[0]; cout << "The pairs are: "; getTwoPairs(arr, n); }
Output
The pairs are: (7 + 4) = (5 + 6)
- Related Articles
- Find largest d in array such that a + b + c = d in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- $triangle A B C$ is an isosceles triangle such that $A B=A C$, $A D perp B C$a) Prove that $triangle A B D cong triangle A C D$b) Prove that $angle B=angle C$c) Is D the mid-point of BC?"
- In ( triangle A B C, A D perp B C ) and ( A D^{2}=B D . C D ).Prove that ( angle B A C=90^o )."
- The positions of four elements A, B, Cand D in the modern periodic table are shown below(a)A(b)B(c)C(d)D"
- Find all pairs (a, b) in an array such that a % b = k in C++
- In the adjoining figure, ( Delta A B C ) is an isosceles triangle such that ( A B=A C ). Side BA is produced to D such that $AD=AB$. Show that ( angle B C D=90^{circ} ). "
- The atomic number of four elements A, B, C and D are 12, 13, 15 and 3 respectively. The element which cannot form a cation is :(a) A (b) B (c) C (d) D
- In a quadrilateral ( A B C D, angle B=90^{circ}, A D^{2}=A B^{2}+B C^{2}+C D^{2}, ) prove that $angle A C D=90^o$.
- ABCD is a quadrilateral in which ( A D=B C ) and ( angle D A B=angle C B A ). Prove that.(i) ( triangle A B D cong triangle B A C )(ii) ( B D=A C )(iii) $angle ABD=angle BAC$ "
- In ( Delta mathrm{PQR}, mathrm{PD} perp mathrm{QR} ) such that ( mathrm{D} ) lies on ( mathrm{QR} ). If ( mathrm{PQ}=a, mathrm{PR}=b, mathrm{QD}=c ) and ( mathrm{DR}=d ), prove that ( (a+b)(a-b)=(c+d)(c-d) ).
- ( A B ) is a diameter and ( A C ) is a chord of a circle with centre ( O ) such that ( angle B A C=30^{circ} ). The tangent at ( C ) intersects ( A B ) at a point ( D ). Prove that ( B C=B D ).
- In the figure, a circle touches all the four sides of a quadrilateral ( A B C D ) with ( A B=6 mathrm{~cm}, B C=7 mathrm{~cm} ) and ( C D=4 mathrm{~cm} . ) Find ( A D )."
- In the figure below, ( Delta A B C ) and ( Delta D B C ) are on the same base ( B C ). If ( A D ) and ( B C ) intersect at $O$, prove that ( frac{text { Area }(Delta A B C)}{text { Area }(Delta D B C)}=frac{A O}{D O} )"
- A water tank has four taps fixed at points A, B, C and D as shown in figure. The water will flow out at the same pressure from taps at$(a)$. B and C $(b)$. A and B $(c)$. C and D $(d)$. A and C"

Advertisements