Find triplet with minimum sum
Last Updated :
06 Apr, 2023
Given an array of distinct integers arr[]. The task is to find a triplet(a group of 3 elements) that has the minimum sum.
Note: The size of the array is always greater than two.
Examples:
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.
Naive Approach: The idea is to generate all possible triplets in the array and then compare sum of one triplet with other triplets, then find the minimum sum.
Below is the implementation of the above approach:
C++
// C++ Program to find triplet with minimum sum
#include <bits/stdc++.h>
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
JavaScript
<script>
// JavaScript Program to find
// triplet with minimum sum
// Function to find triplet with minimum sum
function getMinimumSum(arr, n)
{
let ans = Number.MAX_SAFE_INTEGER;
// Generate all possible triplets
for (let i = 0; i < n - 2; i++) {
for (let j = i + 1; j < n - 1; j++)
{
for (let 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
let arr = [ 1, 2, 3, 4, 5, -1, 5, -2 ];
let n = arr.length;
document.write(getMinimumSum(arr, n) + "<br>");
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: 0(n^3)
Auxiliary Space: 0(1)
Efficient approach: The idea is to traverse the array and compute minimum, second minimum and third minimum element present in the array and print the sum of these three elements.
Below is the implementation of the above approach:
CPP
// C++ Program to find triplet with a minimum sum
#include <bits/stdc++.h>
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
JavaScript
<script>
// JavaScript Program to find triplet
// with a minimum sum
// Function to find triplet with minimum sum
function getMinimumSum(arr , n)
{
// fMin: First minimum
// sMin: Second minimum
// tMin: Third minimum
var fMin = 1000000000, sMin = 1000000000,
tMin = 1000000000;
for (var 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
var arr = [1, 2, 3, 4, 5, -1, 5, -2];
var n = arr.length;
document.write( getMinimumSum(arr, n));
</script>
Time Complexity: 0(n)
Auxiliary Space: 0(1)
Another Approach: Using Sorting
The idea is to sort the array given in the input. After that first three elements of the array will form a triplet that will give a minimum sum because those are the three minimum elements of the array.
Code-
C++
// C++ Program to find triplet with a minimum sum
#include <bits/stdc++.h>
using namespace std;
// Function to find triplet with minimum sum
int getMinimumSum(int arr[], int n)
{
sort(arr, arr + n);
return (arr[0] + arr[1] + arr[2]);
}
// 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;
}
// This code is contributed by user_dtewbxkn77n
Java
import java.util.*;
class Main
{
// Function to find triplet with minimum sum
static int getMinimumSum(int arr[], int n) {
Arrays.sort(arr);
return (arr[0] + arr[1] + arr[2]);
}
// Driver code
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5, -1, 5, -2 };
int n = arr.length;
System.out.println(getMinimumSum(arr, n));
}
}
Python3
# Python program to find triplet with a minimum sum
# Function to find triplet with minimum sum
def getMinimumSum(arr, n):
arr.sort()
return (arr[0] + arr[1] + arr[2])
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, -1, 5, -2]
n = len(arr)
print(getMinimumSum(arr, n))
C#
// C# Program to find triplet with a minimum sum
using System;
class Program {
// Function to find triplet with minimum sum
static int GetMinimumSum(int[] arr, int n) {
Array.Sort(arr);
return (arr[0] + arr[1] + arr[2]);
}
// Driver Code
static void Main(string[] args) {
int[] arr = { 1, 2, 3, 4, 5, -1, 5, -2 };
int n = arr.Length;
Console.WriteLine(GetMinimumSum(arr, n));
}
}
JavaScript
// Function to find triplet with minimum sum
function getMinimumSum(arr) {
arr.sort((a, b) => a - b);
return (arr[0] + arr[1] + arr[2]);
}
// Driver Code
const arr = [1, 2, 3, 4, 5, -1, 5, -2];
console.log(getMinimumSum(arr));
Output-
-2
Time Complexity: O(nlogn),in sorting
Auxiliary Space: O(1)
Similar Reads
Minimum Fibonacci terms with sum equal to K Given a number k, the task is to find the minimum number of Fibonacci terms (including repetitions) whose sum equals k. Note: You may use any Fibonacci number multiple times.Examples:Input: k = 7Output: 2Explanation: Possible ways to sum up to 7 using Fibonacci numbers:5 + 2 = 7 (2 terms)3 + 3 + 1 =
5 min read
Minimum Removals for Target Sum Given an array of positive integers arr[] and an integer k, you can remove either the leftmost or rightmost element from the array in one operation. After each operation, the size of arr[] is reduced by one. The task is to find the minimum number of operations required to make the total sum of the r
15 min read
Minimum total sum from the given two arrays Given two arrays A[] and B[] of N positive integers and a cost C. We can choose any one element from each index of the given arrays i.e., for any index i we can choose only element A[i] or B[i]. The task is to find the minimum total sum of selecting N elements from the given two arrays and if we are
11 min read
Find the Sub-array with sum closest to 0 Given an array of both positive and negative numbers, the task is to find out the subarray whose sum is closest to 0. There can be multiple such subarrays, we need to output just 1 of them. Examples: Input : arr[] = {-1, 3, 2, -5, 4} Output : 1, 3 Subarray from index 1 to 3 has sum closest to 0 i.e.
13 min read
Minimum number of power terms with sum equal to n Given two positive integer n and x. The task is to express n as sum of powers of x (xa1 + xa2 +.....+ xa3) such that the number of powers of x (xa1, xa2, ....., xa3) should be minimum. Print the minimum number of power of x used to make sum equal to n.Examples: Input : n = 5, x = 3 Output : 3 5 = 30
6 min read