

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
<p>Suppose we have an array A, from that array, we have to choose two pairs (a, b) and (c, d), such that ab = cd. Let the array A = [3, 4, 7, 1, 2, 9, 8]. The output pairs are (4, 2) and (1, 8). To solve this, we will follow these steps −</p><ul class="list"><li>For i := 0 to n-1, do<ul class="list"><li>for j := i + 1 to n-1, do<ul class="list"><li>get product = arr[i] * arr[j]</li><li>if product is not present in the hash table, then Hash[product] := (i, j)</li><li>if product is present in the hash table, then print previous and current elements.</li></ul></li></ul></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/qheo6W" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">#include <iostream> #include <unordered_map> using namespace std; void displayPairs(int arr[], int n) { bool found = false; unordered_map<int, pair < int, int > > Hash; for (int i=0; i<n; i++) { for (int j=i+1; j<n; j++) { int prod = arr[i]*arr[j]; if (Hash.find(prod) == Hash.end()) Hash[prod] = make_pair(i,j); else{ pair<int,int> pp = Hash[prod]; cout << "(" << arr[pp.first] << ", " << arr[pp.second] << ") and (" << arr[i]<<", "<<arr[j] << ")"<<endl; found = true; } } } if (found == false) cout << "No pairs have Found" << endl; } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; int n = sizeof(arr)/sizeof(int); displayPairs(arr, n); }</pre><h2>Output</h2><pre class="result notranslate">(1, 6) and (2, 3) (1, 8) and (2, 4) (2, 6) and (3, 4) (3, 8) and (4, 6)</pre>
- Related Questions & Answers
- Find all pairs (a, b) in an array such that a % b = k in C++
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Find largest d in array such that a + b + c = d in C++
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++
- Find numbers a and b that satisfy the given condition in C++
- Count index pairs which satisfy the given condition in C++
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in C++
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- Count all pairs of an array which differ in K bits in C++
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in Python
- Find the canonical cover of FD {A->BC, B->AC, C->AB} in DBMS
- Construct Quadruples, Triples, and Indirect Triples for the expression -(a + b) * (c + d) - (a + b + c)
- C++ Program to Generate All Possible Combinations Out of a,b,c,d,e
- Find the Number which contain the digit d in C++
Advertisements