📌  相关文章
📜  在数组中找到最大的 d,使得 a + b + c = d

📅  最后修改于: 2021-10-27 16:48:41             🧑  作者: Mango

给定一个整数集合 S(所有不同的元素),找到最大的 d,使得 a + b + c = d
其中 a、b、c 和 d 是 S 的不同元素。

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

例子 :

方法一(蛮力)
我们可以使用简单的蛮力方法解决这个问题,这种方法效率不高,因为 |S|可以大到 1000。我们将对元素集进行排序,然后通过将其与 a、b 和 c 的所有可能组合的总和相等来找到最大的 d。
以下是上述想法的实现:

C++
// CPP Program to find the largest d
// such that d = a + b + c
#include 
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;
}


Java
// 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


Python3
# Python Program to find the largest
# d such that d = a + b + c
 
def findLargestd(S, n) :
    found = False
 
    # sort the array in ascending order
    S.sort()
 
    # iterating from backwards to
    # find the required largest d
    for i in range(n-1, -1, -1) :
        for j in range(0, n) :
 
            # since all four a, b, c,
            # d should be distinct
            if (i == j) :
                continue
 
            for k in range(j + 1, n) :
                if (i == k) :
                    continue
 
                for l in range(k+1, n) :
                    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 -1
 
# Driver Code
 
# Set of distinct Integers
S = [ 2, 3, 5, 7, 12 ]
n = len(S)
 
ans = findLargestd(S, n)
if (ans == -1) :
    print ("No Solution")
else :
    print ("Largest d such that a + b +" ,
        "c = d is" ,ans)
 
# This code is contributed by Manish Shaw
# (manishshaw1)


C#
// 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


PHP
= 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.
?>


Javascript


C++
// A hashing based CPP program to find largest d
// such that a + b + c = d.
#include 
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 > 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 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;
}


Java
// A hashing based Java program to find largest d
// such that a + b + c = d.
import java.util.HashMap;
import java.lang.Math;
 
// To store and retrieve indices pair i & j
class Indexes
{
    int i, j;
 
    Indexes(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
 
    int getI()
    {
        return i;
    }
 
    int getJ()
    {
        return j;
    }
}
 
class GFG {
 
    // The function finds four elements with given sum X
    static int findFourElements(int[] arr, int n)
    {
        HashMap map = new HashMap<>();
 
        // Store sums (a+b) of all pairs (a,b) in a
        // hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                map.put(arr[i] + arr[j], new Indexes(i, j));
            }
        }
 
        int d = Integer.MIN_VALUE;
         
        // Traverse through all pairs and find (d -c)
        // is present in hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int abs_diff = Math.abs(arr[i] - arr[j]);
 
                // If d - c is present in hash table,
                if (map.containsKey(abs_diff))
                {
                    Indexes indexes = map.get(abs_diff);
                     
                    // Making sure that all elements are
                    // distinct array elements and an element
                    // is not considered more than once.
                    if (indexes.getI() != i && indexes.getI() != j &&
                    indexes.getJ() != i && indexes.getJ() != j)
                    {
                        d = Math.max(d, Math.max(arr[i], arr[j]));
                    }
                }
            }
        }
        return d;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 5, 7, 12 };
        int n = arr.length;
        int res = findFourElements(arr, n);
        if (res == Integer.MIN_VALUE)
            System.out.println("No Solution");
        else
            System.out.println(res);
    }
}
 
// This code is contributed by Vivekkumar Singh


Python3
# A hashing based Python3 program to find
# largest d, such that a + b + c = d.
 
# The function finds four elements
# with given sum X
def findFourElements(arr, n):
 
    # Store sums (a+b) of all pairs (a,b) in a
    # hash table
    mp = dict()
    for i in range(n - 1):
        for j in range(i + 1, n):
            mp[arr[i] + arr[j]] =(i, j)
 
    # Traverse through all pairs and find (d -c)
    # is present in hash table
    d = -10**9
    for i in range(n - 1):
        for j in range(i + 1, n):
            abs_diff = abs(arr[i] - arr[j])
 
            # If d - c is present in hash table,
            if abs_diff in mp.keys():
 
                # Making sure that all elements are
                # distinct array elements and an element
                # is not considered more than once.
                p = mp[abs_diff]
                if (p[0] != i and p[0] != j and
                    p[1] != i and p[1] != j):
                    d = max(d, max(arr[i], arr[j]))
 
    return d
 
# Driver Code
arr = [2, 3, 5, 7, 12]
n = len(arr)
res = findFourElements(arr, n)
if (res == -10**9):
    print("No Solution.")
else:
    print(res)
 
# This code is contributed by Mohit Kumar


C#
// A hashing based C# program to find
// largest d such that a + b + c = d.
using System;
using System.Collections.Generic;
 
// To store and retrieve
// indices pair i & j
public class Indexes
{
    int i, j;
 
    public Indexes(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
 
    public int getI()
    {
        return i;
    }
 
    public int getJ()
    {
        return j;
    }
}
 
public class GFG
{
 
    // The function finds four elements
    // with given sum X
    static int findFourElements(int[] arr, int n)
    {
        Dictionary map =
                    new Dictionary();
 
        // Store sums (a+b) of all pairs
        // (a,b) in a hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                map.Add(arr[i] + arr[j],
                        new Indexes(i, j));
            }
        }
 
        int d = int.MinValue;
         
        // Traverse through all pairs and
        // find (d -c) is present in hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int abs_diff = Math.Abs(arr[i] - arr[j]);
 
                // If d - c is present in hash table,
                if (map.ContainsKey(abs_diff))
                {
                    Indexes indexes = map[abs_diff];
                     
                    // Making sure that all elements are
                    // distinct array elements and an element
                    // is not considered more than once.
                    if (indexes.getI() != i &&
                        indexes.getI() != j &&
                        indexes.getJ() != i &&
                        indexes.getJ() != j)
                    {
                        d = Math.Max(d, Math.Max(arr[i],
                                                 arr[j]));
                    }
                }
            }
        }
        return d;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 2, 3, 5, 7, 12 };
        int n = arr.Length;
        int res = findFourElements(arr, n);
        if (res == int.MinValue)
            Console.WriteLine("No Solution");
        else
            Console.WriteLine(res);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出 :
Largest d such that a + b + c = d is 12

这个蛮力解决方案的时间复杂度为 O((Set size of Set) 4 )。
方法二(高效方法——使用哈希)
上面的问题陈述 (a + b + c = d) 可以重新表述为找到 a, b, c, d 使得 a + b = d – c。所以这个问题可以使用散列有效地解决。

  1. 将所有对 (a + b) 的总和存储在哈希表中
  2. 再次遍历所有对 (c, d) 并在哈希表中搜索 (d – c)。
  3. 如果找到具有所需总和的对,则确保所有元素都是不同的数组元素,并且不会多次考虑一个元素。

下面是上述方法的实现。

C++

// A hashing based CPP program to find largest d
// such that a + b + c = d.
#include 
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 > 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 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;
}

Java

// A hashing based Java program to find largest d
// such that a + b + c = d.
import java.util.HashMap;
import java.lang.Math;
 
// To store and retrieve indices pair i & j
class Indexes
{
    int i, j;
 
    Indexes(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
 
    int getI()
    {
        return i;
    }
 
    int getJ()
    {
        return j;
    }
}
 
class GFG {
 
    // The function finds four elements with given sum X
    static int findFourElements(int[] arr, int n)
    {
        HashMap map = new HashMap<>();
 
        // Store sums (a+b) of all pairs (a,b) in a
        // hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                map.put(arr[i] + arr[j], new Indexes(i, j));
            }
        }
 
        int d = Integer.MIN_VALUE;
         
        // Traverse through all pairs and find (d -c)
        // is present in hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int abs_diff = Math.abs(arr[i] - arr[j]);
 
                // If d - c is present in hash table,
                if (map.containsKey(abs_diff))
                {
                    Indexes indexes = map.get(abs_diff);
                     
                    // Making sure that all elements are
                    // distinct array elements and an element
                    // is not considered more than once.
                    if (indexes.getI() != i && indexes.getI() != j &&
                    indexes.getJ() != i && indexes.getJ() != j)
                    {
                        d = Math.max(d, Math.max(arr[i], arr[j]));
                    }
                }
            }
        }
        return d;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 5, 7, 12 };
        int n = arr.length;
        int res = findFourElements(arr, n);
        if (res == Integer.MIN_VALUE)
            System.out.println("No Solution");
        else
            System.out.println(res);
    }
}
 
// This code is contributed by Vivekkumar Singh

蟒蛇3

# A hashing based Python3 program to find
# largest d, such that a + b + c = d.
 
# The function finds four elements
# with given sum X
def findFourElements(arr, n):
 
    # Store sums (a+b) of all pairs (a,b) in a
    # hash table
    mp = dict()
    for i in range(n - 1):
        for j in range(i + 1, n):
            mp[arr[i] + arr[j]] =(i, j)
 
    # Traverse through all pairs and find (d -c)
    # is present in hash table
    d = -10**9
    for i in range(n - 1):
        for j in range(i + 1, n):
            abs_diff = abs(arr[i] - arr[j])
 
            # If d - c is present in hash table,
            if abs_diff in mp.keys():
 
                # Making sure that all elements are
                # distinct array elements and an element
                # is not considered more than once.
                p = mp[abs_diff]
                if (p[0] != i and p[0] != j and
                    p[1] != i and p[1] != j):
                    d = max(d, max(arr[i], arr[j]))
 
    return d
 
# Driver Code
arr = [2, 3, 5, 7, 12]
n = len(arr)
res = findFourElements(arr, n)
if (res == -10**9):
    print("No Solution.")
else:
    print(res)
 
# This code is contributed by Mohit Kumar

C#

// A hashing based C# program to find
// largest d such that a + b + c = d.
using System;
using System.Collections.Generic;
 
// To store and retrieve
// indices pair i & j
public class Indexes
{
    int i, j;
 
    public Indexes(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
 
    public int getI()
    {
        return i;
    }
 
    public int getJ()
    {
        return j;
    }
}
 
public class GFG
{
 
    // The function finds four elements
    // with given sum X
    static int findFourElements(int[] arr, int n)
    {
        Dictionary map =
                    new Dictionary();
 
        // Store sums (a+b) of all pairs
        // (a,b) in a hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                map.Add(arr[i] + arr[j],
                        new Indexes(i, j));
            }
        }
 
        int d = int.MinValue;
         
        // Traverse through all pairs and
        // find (d -c) is present in hash table
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int abs_diff = Math.Abs(arr[i] - arr[j]);
 
                // If d - c is present in hash table,
                if (map.ContainsKey(abs_diff))
                {
                    Indexes indexes = map[abs_diff];
                     
                    // Making sure that all elements are
                    // distinct array elements and an element
                    // is not considered more than once.
                    if (indexes.getI() != i &&
                        indexes.getI() != j &&
                        indexes.getJ() != i &&
                        indexes.getJ() != j)
                    {
                        d = Math.Max(d, Math.Max(arr[i],
                                                 arr[j]));
                    }
                }
            }
        }
        return d;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 2, 3, 5, 7, 12 };
        int n = arr.Length;
        int res = findFourElements(arr, n);
        if (res == int.MinValue)
            Console.WriteLine("No Solution");
        else
            Console.WriteLine(res);
    }
}
 
// This code is contributed by 29AjayKumar

Javascript


输出:
12

这种有效方法的总时间复杂度为O(N 2 ) (其中 N 是集合的大小)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程