Find largest d in array such that a + b + c = d

Given a set S (all distinct elements) of integers, find the largest d such that a + b + c = d
where a, b, c, and d are distinct elements of S.

Constraints:
1 ≤ number of elements in the set ≤ 1000
INT_MIN ≤ each element in the set ≤ INT_MAX

Examples :

Input : S[] = {2, 3, 5, 7, 12}
Output : 12
Explanation: 12 is the largest d which can be represented as 12 = 2 + 3 + 7

Input : S[] = {2, 16, 64, 256, 1024}
Output : No solution

Method 1(Brute Force)
We can solve this problem using simple brute force approach which is not very efficient as |S| can be as large as 1000. We’ll sort the set of elements and start by finding the largest d by equating it with the sum of all possible combinations of a, b and c.
Below is the implementation of above idea :

C++


[sourcecode language=”CPP” highlight=”6-53″]
// CPP Program to find the largest d
// such that d = a + b + c
#include <bits/stdc++.h>
using namespace std;

int findLargestd(int S[], int n)
{
bool found = false;

// sort the array in
// ascending order
sort(S, S + n);

// iterating from backwards to
// find the required largest d
for (int i = n – 1; i >= 0; i–)
{
for (int j = 0; j < n; j++)
{

// since all four a, b, c,
// d should be distinct
if (i == j)
continue;

for (int k = j + 1; k < n; k++)
{
if (i == k)
continue;

for (int l = k + 1; l < n; l++)
{
if (i == l)
continue;

// if the current combination
// of j, k, l in the set is
// equal to S[i] return this
// value as this would be the
// largest d since we are
// iterating in descending order
if (S[i] == S[j] + S[k] + S[l])
{
found = true;
return S[i];
}
}
}
}
}
if (found == false)
return INT_MIN;
}

// Driver Code
int main()
{
// Set of distinct Integers
int S[] = { 2, 3, 5, 7, 12 };
int n = sizeof(S) / sizeof(S[0]);

int ans = findLargestd(S, n);
if (ans == INT_MIN)
cout << "No Solution" << endl;
else
cout << "Largest d such that a + b + "
<< "c = d is " << ans << endl;
return 0;
}

[/sourcecode]

Java


[sourcecode language=”Java” highlight=”9-59″]
// Java Program to find the largest
// such that d = a + b + c
import java.io.*;
import java.util.Arrays;

class GFG
{

// function to find largest d
static int findLargestd(int []S, int n)
{
boolean found = false;

// sort the array in
// ascending order
Arrays.sort(S);

// iterating from backwards to
// find the required largest d
for (int i = n – 1; i >= 0; i–)
{
for (int j = 0; j < n; j++)
{

// since all four a, b, c,
// d should be distinct
if (i == j)
continue;

for (int k = j + 1; k < n; k++)
{
if (i == k)
continue;

for (int l = k + 1; l < n; l++)
{
if (i == l)
continue;

// if the current combination
// of j, k, l in the set is
// equal to S[i] return this
// value as this would be the
// largest d since we are
// iterating in descending order
if (S[i] == S[j] + S[k] + S[l])
{
found = true;
return S[i];
}
}
}
}
}
if (found == false)
return Integer.MAX_VALUE;

return -1;
}

// Driver Code
public static void main(String []args)
{
// Set of distinct Integers
int []S = new int[]{ 2, 3, 5, 7, 12 };
int n = S.length;

int ans = findLargestd(S, n);
if (ans == Integer.MAX_VALUE)
System.out.println("No Solution");
else
System.out.println("Largest d such that " +
"a + " + "b + c = d is " +
ans );

}
}

// This code is contributed by Sam007

[/sourcecode]

C#


[sourcecode language=”CSHARP” highlight=”8-59″]
// C# Program to find the largest
// such that d = a + b + c
using System;

class GFG
{

// function to find largest d
static int findLargestd(int []S,
int n)
{
bool found = false;

// sort the array
// in ascending order
Array.Sort(S);

// iterating from backwards to
// find the required largest d
for (int i = n – 1; i >= 0; i–)
{
for (int j = 0; j < n; j++)
{

// since all four a, b, c,
// d should be distinct
if (i == j)
continue;

for (int k = j + 1; k < n; k++)
{
if (i == k)
continue;

for (int l = k + 1; l < n; l++)
{
if (i == l)
continue;

// if the current combination
// of j, k, l in the set is
// equal to S[i] return this
// value as this would be the
// largest dsince we are
// iterating in descending order
if (S[i] == S[j] + S[k] + S[l])
{
found = true;
return S[i];
}
}
}
}
}
if (found == false)
return int.MaxValue;

return -1;
}

// Driver Code
public static void Main()
{
// Set of distinct Integers
int []S = new int[]{ 2, 3, 5, 7, 12 };
int n = S.Length;

int ans = findLargestd(S, n);
if (ans == int.MaxValue)
Console.WriteLine( "No Solution");
else
Console.Write("Largest d such that a + " +
"b + c = d is " + ans );

}
}

// This code is contributed by Sam007

[/sourcecode]

PHP


[sourcecode language=”php” highlight=”5-52″]
<?php
// PHP Program to find the largest
// d such that d = a + b + c

function findLargestd( $S, $n)
{
$found = false;

// sort the array in
// ascending order
sort($S);

// iterating from backwards to
// find the required largest d
for ( $i = $n – 1; $i >= 0; $i–)
{
for ( $j = 0; $j < $n; $j++)
{

// since all four a, b, c,
// d should be distinct
if ($i == $j)
continue;

for ( $k = $j + 1; $k < $n; $k++)
{
if ($i == $k)
continue;

for ( $l = $k + 1; $l < $n; $l++)
{
if ($i == $l)
continue;

// if the current combination
// of j, k, l in the set is
// equal to S[i] return this
// value as this would be the
// largest d since we are
// iterating in descending order
if ($S[$i] == $S[$j] + $S[$k] + $S[$l])
{
$found = true;
return $S[$i];
}
}
}
}
}
if ($found == false)
return PHP_INT_MIN;
}

// Driver Code

// Set of distinct Integers
$S = array( 2, 3, 5, 7, 12 );
$n = count($S);

$ans = findLargestd($S, $n);
if ($ans == PHP_INT_MIN)
echo "No Solution" ;
else
echo "Largest d such that a + b + " ,
"c = d is " , $ans ;

// This code is contributed by anuj_67.
?>

[/sourcecode]

Output :

Largest d such that a + b + c = d is 12

This brute force solution has a time complexity of O((size of Set)4).

Method 2(Efficient Approach – Using Hashing)
The above problem statement (a + b + c = d) can be restated as finding a, b, c, d such that a + b = d – c. So this problem can be efficiently solved using hashing.

  1. Store sums of all pairs (a + b) in a hash table
  2. Traverse through all pairs (c, d) again and search for (d – c) in the hash table.
  3. If a pair is found with the required sum, then make sure that all elements are distinct array elements and an element is not considered more than once.

Below is the implementation in C++.

C++


[sourcecode language=”CPP”]
// A hashing based CPP program to find largest d
// such that a + b + c = d.
#include <bits/stdc++.h>
using namespace std;

// The function finds four elements with given sum X
int findFourElements(int arr[], int n)
{
// Store sums (a+b) of all pairs (a,b) in a
// hash table
unordered_map<int, pair<int, int> > mp;
for (int i = 0; i < n – 1; i++)
for (int j = i + 1; j < n; j++)
mp[arr[i] + arr[j]] = { i, j };

// Traverse through all pairs and find (d -c)
// is present in hash table
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 d – c is present in hash table,
if (mp.find(abs_diff) != mp.end()) {

// Making sure that all elements are
// distinct array elements and an element
// is not considered more than once.
pair<int, int> p = mp[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;
}

// Driver program to test above function
int main()
{
int arr[] = { 2, 3, 5, 7, 12 };
int n = sizeof(arr) / sizeof(arr[0]);
int res = findFourElements(arr, n);
if (res == INT_MIN)
cout << "No Solution.";
else
cout << res;
return 0;
}
[/sourcecode]

Output:

12

The overall time complexity for this efficient approach is O(N2) (where N is the size of the set).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.