📜  用最小的总和找到三元组

📅  最后修改于: 2021-05-06 10:02:29             🧑  作者: Mango

给定一个由不同整数组成的数组arr [] 。任务是找到一个具有最小总和的三元组(一组3个元素)。

注意:数组的大小始终大于2。

例子:

Input : arr[] = {1, 2, 3, 4, -1, 5, -2}
Output : -2
1 - 1 - 2 = -2
Input : arr[] = {5, 6, 0, 0, 1}
Output : 1
0 + 0 + 1.

天真的方法:想法是生成数组中所有可能的三元组,然后将一个三元组的总和与其他三元组进行比较,然后找到最小和。

下面是上述方法的实现:

C++
// C++ Program to find triplet with minimum sum
#include 
using namespace std;
  
// Function to find triplet with minimum sum
int getMinimumSum(int arr[] , int n)
{
    int ans = INT_MAX;
      
    // Generate all possible triplets
    for (int i = 0; i < n - 2; i++) {
        for (int j = i + 1; j < n - 1; j++) {
            for (int k = j + 1; k < n; k++) {
                // Calculate sum of each triplet
                // and update minimum
                ans = min(ans, arr[i] + arr[j] + arr[k]);
            }
        }
    }
      
    return ans;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << getMinimumSum(arr, n) << endl;
      
    return 0;
}


Java
// Java Program to find triplet with minimum sum
class GFG
{
  
// Function to find triplet with minimum sum
static int getMinimumSum(int arr[] , int n)
{
    int ans = Integer.MAX_VALUE;
      
    // Generate all possible triplets
    for (int i = 0; i < n - 2; i++) 
    {
        for (int j = i + 1; j < n - 1; j++) 
        {
            for (int k = j + 1; k < n; k++)
            {
                // Calculate sum of each triplet
                // and update minimum
                ans = Math.min(ans, arr[i] + 
                                arr[j] + arr[k]);
            }
        }
    }
      
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = arr.length;
  
    System.out.print(getMinimumSum(arr, n) + "\n");
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 Program to find triplet with minimum sum
import sys
  
# Function to find triplet with minimum sum
def getMinimumSum(arr, n):
    ans = sys.maxsize;
  
    # Generate all possible triplets
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            for k in range(j + 1, n):
                  
                # Calculate sum of each triplet
                # and update minimum
                ans = min(ans, arr[i] + arr[j] + arr[k]);
    return ans;
  
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 3, 4, 5, -1, 5, -2 ];
    n = len(arr);
  
    print(getMinimumSum(arr, n));
      
# This code is contributed by PrinciRaj1992


C#
// C# Program to find triplet with minimum sum
using System;
  
class GFG
{
      
    // Function to find triplet with minimum sum
    static int getMinimumSum(int []arr, int n)
    {
        int ans = int.MaxValue;
          
        // Generate all possible triplets
        for (int i = 0; i < n - 2; i++) 
        {
            for (int j = i + 1; j < n - 1; j++) 
            {
                for (int k = j + 1; k < n; k++)
                {
                    // Calculate sum of each triplet
                    // and update minimum
                    ans = Math.Min(ans, arr[i] + 
                                    arr[j] + arr[k]);
                }
            }
        }
          
        return ans;
    }
      
    // Driver Code
    public static void Main()
    {
        int []arr = { 1, 2, 3, 4, 5, -1, 5, -2 };
        int n = arr.Length;
      
        Console.WriteLine(getMinimumSum(arr, n));
    }
}
  
// This code is contributed by AnkitRai01


CPP
// C++ Program to find triplet with a minimum sum
  
#include 
using namespace std;
  
// Function to find triplet with minimum sum
int getMinimumSum(int arr[] , int n)
{
    // fMin: First minimum
    // sMin: Second minimum
    // tMin: Third minimum
    int fMin = INT_MAX, sMin = INT_MAX, tMin = INT_MAX;
      
    for (int i = 0; i < n; i++) {
        // Update the first, second and third minimum
        if (arr[i] < fMin) {
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
        }
        // update second and third minimum
        else if (arr[i] < sMin) {
            tMin = sMin;
            sMin = arr[i];
        }
        else if (arr[i] < tMin) {
            tMin = arr[i];
        }
    }
      
    return (fMin + sMin + tMin);
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << getMinimumSum(arr, n) << endl;
      
    return 0;
}


Java
// Java Program to find triplet with a minimum sum
  
class GFG
{
  
// Function to find triplet with minimum sum
static int getMinimumSum(int arr[] , int n)
{
    // fMin: First minimum
    // sMin: Second minimum
    // tMin: Third minimum
    int fMin = Integer.MAX_VALUE,
            sMin = Integer.MAX_VALUE, 
            tMin = Integer.MAX_VALUE;
      
    for (int i = 0; i < n; i++) 
    {
          
        // Update the first, second and third minimum
        if (arr[i] < fMin) 
        {
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
        }
          
        // update second and third minimum
        else if (arr[i] < sMin)
        {
            tMin = sMin;
            sMin = arr[i];
        }
        else if (arr[i] < tMin)
        {
            tMin = arr[i];
        }
    }
    return (fMin + sMin + tMin);
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = arr.length;
  
    System.out.print(getMinimumSum(arr, n) +"\n");
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 Program to find triplet with a minimum sum
import sys
  
# Function to find triplet with minimum sum
def getMinimumSum(arr, n):
  
    # fMin: First minimum
    # sMin: Second minimum
    # tMin: Third minimum
    fMin = sys.maxsize; 
    sMin = sys.maxsize;
    tMin = sys.maxsize;
  
    for i in range(n):
  
        # Update the first, second and third minimum
        if (arr[i] < fMin):
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
          
        # update second and third minimum
        elif(arr[i] < sMin):
            tMin = sMin;
            sMin = arr[i];
        elif(arr[i] < tMin):
            tMin = arr[i];
          
    return (fMin + sMin + tMin);
  
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 3, 4, 5, -1, 5, -2];
    n = len(arr);
  
    print(getMinimumSum(arr, n));
  
# This code is contributed by 29AjayKumar


C#
// C# Program to find triplet with a minimum sum
using System;
  
class GFG
{
  
// Function to find triplet with minimum sum
static int getMinimumSum(int []arr, int n)
{
    // fMin: First minimum
    // sMin: Second minimum
    // tMin: Third minimum
    int fMin = int.MaxValue,
        sMin = int.MaxValue, 
        tMin = int.MaxValue;
      
    for (int i = 0; i < n; i++) 
    {
          
        // Update the first, second and third minimum
        if (arr[i] < fMin) 
        {
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
        }
          
        // update second and third minimum
        else if (arr[i] < sMin)
        {
            tMin = sMin;
            sMin = arr[i];
        }
        else if (arr[i] < tMin)
        {
            tMin = arr[i];
        }
    }
    return (fMin + sMin + tMin);
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = arr.Length;
  
    Console.Write(getMinimumSum(arr, n) +"\n");
}
}
  
// This code is contributed by 29AjayKumar


输出:
-2

时间复杂度: 0(n ^ 3)
辅助空间: 0(1)

有效方法:想法是遍历数组并计算数组中存在的最小值,第二最小值和第三最小值,并打印这三个元素的总和。

下面是上述方法的实现:

CPP

// C++ Program to find triplet with a minimum sum
  
#include 
using namespace std;
  
// Function to find triplet with minimum sum
int getMinimumSum(int arr[] , int n)
{
    // fMin: First minimum
    // sMin: Second minimum
    // tMin: Third minimum
    int fMin = INT_MAX, sMin = INT_MAX, tMin = INT_MAX;
      
    for (int i = 0; i < n; i++) {
        // Update the first, second and third minimum
        if (arr[i] < fMin) {
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
        }
        // update second and third minimum
        else if (arr[i] < sMin) {
            tMin = sMin;
            sMin = arr[i];
        }
        else if (arr[i] < tMin) {
            tMin = arr[i];
        }
    }
      
    return (fMin + sMin + tMin);
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << getMinimumSum(arr, n) << endl;
      
    return 0;
}

Java

// Java Program to find triplet with a minimum sum
  
class GFG
{
  
// Function to find triplet with minimum sum
static int getMinimumSum(int arr[] , int n)
{
    // fMin: First minimum
    // sMin: Second minimum
    // tMin: Third minimum
    int fMin = Integer.MAX_VALUE,
            sMin = Integer.MAX_VALUE, 
            tMin = Integer.MAX_VALUE;
      
    for (int i = 0; i < n; i++) 
    {
          
        // Update the first, second and third minimum
        if (arr[i] < fMin) 
        {
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
        }
          
        // update second and third minimum
        else if (arr[i] < sMin)
        {
            tMin = sMin;
            sMin = arr[i];
        }
        else if (arr[i] < tMin)
        {
            tMin = arr[i];
        }
    }
    return (fMin + sMin + tMin);
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = arr.length;
  
    System.out.print(getMinimumSum(arr, n) +"\n");
}
}
  
// This code is contributed by PrinciRaj1992

Python3

# Python3 Program to find triplet with a minimum sum
import sys
  
# Function to find triplet with minimum sum
def getMinimumSum(arr, n):
  
    # fMin: First minimum
    # sMin: Second minimum
    # tMin: Third minimum
    fMin = sys.maxsize; 
    sMin = sys.maxsize;
    tMin = sys.maxsize;
  
    for i in range(n):
  
        # Update the first, second and third minimum
        if (arr[i] < fMin):
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
          
        # update second and third minimum
        elif(arr[i] < sMin):
            tMin = sMin;
            sMin = arr[i];
        elif(arr[i] < tMin):
            tMin = arr[i];
          
    return (fMin + sMin + tMin);
  
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 3, 4, 5, -1, 5, -2];
    n = len(arr);
  
    print(getMinimumSum(arr, n));
  
# This code is contributed by 29AjayKumar

C#

// C# Program to find triplet with a minimum sum
using System;
  
class GFG
{
  
// Function to find triplet with minimum sum
static int getMinimumSum(int []arr, int n)
{
    // fMin: First minimum
    // sMin: Second minimum
    // tMin: Third minimum
    int fMin = int.MaxValue,
        sMin = int.MaxValue, 
        tMin = int.MaxValue;
      
    for (int i = 0; i < n; i++) 
    {
          
        // Update the first, second and third minimum
        if (arr[i] < fMin) 
        {
            tMin = sMin;
            sMin = fMin;
            fMin = arr[i];
        }
          
        // update second and third minimum
        else if (arr[i] < sMin)
        {
            tMin = sMin;
            sMin = arr[i];
        }
        else if (arr[i] < tMin)
        {
            tMin = arr[i];
        }
    }
    return (fMin + sMin + tMin);
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5, -1, 5, -2 };
    int n = arr.Length;
  
    Console.Write(getMinimumSum(arr, n) +"\n");
}
}
  
// This code is contributed by 29AjayKumar
输出:
-2

时间复杂度: 0(n)
辅助空间: 0(1)