- 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 largest d in array such that a + b + c = d in C++
Suppose we have a set of integers. We have to find a number ‘d’, where d = a + b + c, and we have to maximize (a + b + c), all a, b, c, and d are present in the set. The set will hold at least one element, and at max 1000 elements. Each element will be a finite number. If the set is {2, 3, 5, 7, 12}, then 12 is largest d. this can be represented by 2 + 3 + 7
To solve this problem, we can use the hashing technique. We will store the sum of all pairs of (a + b) in the hash table, then traverse through all pairs (c, d) and search (d - c) is present in the table or not. If one match is found, then make sure that no two elements are the same, and no same elements are considered twice.
Example
#include<iostream> #include<unordered_map> #include<climits> using namespace std; int findElementsInSet(int arr[], int n) { unordered_map<int, pair<int, int> > table; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) table[arr[i] + arr[j]] = { i, j }; int d = INT_MIN; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int abs_diff = abs(arr[i] - arr[j]); if (table.find(abs_diff) != table.end()) { pair<int, int> p = table[abs_diff]; if (p.first != i && p.first != j && p.second != i && p.second != j) d = max(d, max(arr[i], arr[j])); } } } return d; } int main() { int arr[] = { 2, 3, 5, 7, 12 }; int n = sizeof(arr) / sizeof(arr[0]); int res = findElementsInSet(arr, n); if (res == INT_MIN) cout << "Cannot find the value of d"; else cout << "Max value of d is " << res; }
Output
Max value of d is 12
- Related Articles
- Find four elements a, b, c and d in an array such that a+b = c+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?"\n
- 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 )."\n
- 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} ). "\n
- Find all pairs (a, b) in an array such that a % b = k in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- 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) ).
- 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$.
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- ( 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 ).
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in Python
- Emulating a 2-d array using 1-d array in C++
- 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$ "\n
- In Fig. 9.25, diagonals ( A C ) and ( B D ) of quadrilateral ( A B C D ) intersect at ( O ) such that ( O B=O D ). If ( mathrm{AB}=mathrm{CD}, ) then show that:( operatorname{ar}(mathrm{DOC})=operatorname{ar}(mathrm{AOB}) )"\n

Advertisements