All unique triplets that sum up to a given value - GeeksforGeeks

25 May, 2021
Given an array and a sum value, find all possible unique triplets in that array whose sum is equal to the given sum value. If no such triplets can be formed from the array, then print “No triplets can be formed”, else print all the unique triplets. For example, if the given array is {12, 3, 6, 1, 6, 9} and the given sum is 24, then the unique triplets are (3, 9, 12) and (6, 6, 12) whose sum is 24.
Examples: 
Input : array = {12, 3, 6, 1, 6, 9} sum = 24
Output : [[3, 9, 12], [6, 6, 12]]

Input : array = {-2, 0, 1, 1, 2} sum = 0
Output : [[-2, 0, 2], [-2, 1, 1]]

Input : array = {-2, 0, 1, 1, 2} sum = 10
Output : No triplets can be formed
Method 1In a previous post, Find a triplet that sum to a given value we have discussed whether the triplets can be formed from the array or not.Here we need to print all unique set of triplets that sum up to a given value
Sort the input array.Find three indexes from the array i, j and k where A[i]+A[j]+A[k] = given sum value.Fix the first element as A[i] and iterate i from 0 to array size – 2.For each iteration of i, take j to be index of the first element in the remaining elements and k to be the index of the last element.Check for the triplet combination A[i]+A[j]+A[k] = given sum value.If triplet is obtained (ie., A[i]+A[j]+A[k] = given sum value)Add all the triplet in a TreeSet with “:” separated value to get the unique triplets.Increment the second value indexDecrement the third value index.Repeat step 4 & 5 till j < kElse if, A[i]+A[j]+A[k] < given sum value, increment the second value index Else if, A[i]+A[j]+A[k] > given sum value, decrement the third value index
Sort the input array.
Find three indexes from the array i, j and k where A[i]+A[j]+A[k] = given sum value.
Fix the first element as A[i] and iterate i from 0 to array size – 2.
For each iteration of i, take j to be index of the first element in the remaining elements and k to be the index of the last element.
Check for the triplet combination A[i]+A[j]+A[k] = given sum value.
If triplet is obtained (ie., A[i]+A[j]+A[k] = given sum value)Add all the triplet in a TreeSet with “:” separated value to get the unique triplets.Increment the second value indexDecrement the third value index.Repeat step 4 & 5 till j < k
Add all the triplet in a TreeSet with “:” separated value to get the unique triplets.Increment the second value indexDecrement the third value index.Repeat step 4 & 5 till j < k
Add all the triplet in a TreeSet with “:” separated value to get the unique triplets.
Increment the second value index
Decrement the third value index.
Repeat step 4 & 5 till j < k
Else if, A[i]+A[j]+A[k] < given sum value, increment the second value index Else if, A[i]+A[j]+A[k] > given sum value, decrement the third value index
 Below is the implementation of the above idea:
C++
Java
Python3
C#
Javascript
// C++ program to find unique triplets// that sum up to a given value.#include <bits/stdc++.h>using namespace std; // Structure to define a triplet.struct triplet{    int first, second, third;}; // Function to find unique triplets that// sum up to a given value.int findTriplets(int nums[], int n, int sum){    int i, j, k;     // Vector to store all unique triplets.    vector<triplet> triplets;     // Set to store already found triplets    // to avoid duplication.    unordered_set<string> uniqTriplets;     // Variable used to hold triplet    // converted to string form.    string temp;     // Variable used to store current    // triplet which is stored in vector    // if it is unique.    triplet newTriplet;     // Sort the input array.    sort(nums, nums + n);     // Iterate over the array from the    // start and consider it as the    // first element.    for (i = 0; i < n - 2; i++)    {        // index of the first element in        // the remaining elements.        j = i + 1;         // index of the last element.        k = n - 1;         while (j < k) {             // If sum of triplet is equal to            // given value, then check if            // this triplet is unique or not.            // To check uniqueness, convert            // triplet to string form and            // then check if this string is            // present in set or not. If            // triplet is unique, then store            // it in vector.            if (nums[i] + nums[j] + nums[k] == sum)            {                temp = to_string(nums[i]) + " : "                       + to_string(nums[j]) + " : "                       + to_string(nums[k]);                if (uniqTriplets.find(temp)                    == uniqTriplets.end()) {                    uniqTriplets.insert(temp);                    newTriplet.first = nums[i];                    newTriplet.second = nums[j];                    newTriplet.third = nums[k];                    triplets.push_back(newTriplet);                }                 // Increment the first index                // and decrement the last                // index of remaining elements.                j++;                k--;            }             // If sum is greater than given            // value then to reduce sum            // decrement the last index.            else if (nums[i] + nums[j] + nums[k] > sum)                k--;             // If sum is less than given value            // then to increase sum increment            // the first index of remaining            // elements.            else                j++;        }    }     // If no unique triplet is found, then    // return 0.    if (triplets.size() == 0)        return 0;     // Print all unique triplets stored in    // vector.    for (i = 0; i < triplets.size(); i++) {        cout << "[" << triplets[i].first << ", "             << triplets[i].second << ", "             << triplets[i].third << "], ";    }} // Driver Codeint main(){    int nums[] = { 12, 3, 6, 1, 6, 9 };    int n = sizeof(nums) / sizeof(nums[0]);    int sum = 24;       // Function call    if (!findTriplets(nums, n, sum))        cout << "No triplets can be formed.";     return 0;} // This code is contributed by NIKHIL JINDAL.
// Java program to find all triplets with given sumimport java.util.*; public class triplets {     // returns all triplets whose sum is    //  equal to sum value    public static List<List<Integer> >    findTriplets(int[] nums, int sum)    {         /* Sort the elements */        Arrays.sort(nums);         List<List<Integer> > pair          = new ArrayList<>();        TreeSet<String> set          = new TreeSet<String>();        List<Integer> triplets          = new ArrayList<>();         /* Iterate over the array from the start        and consider it as the first element*/        for (int i = 0;             i < nums.length - 2;             i++) {             // index of the first element in the            // remaining elements            int j = i + 1;             // index of the last element            int k = nums.length - 1;             while (j < k) {                 if (nums[i] + nums[j]                    + nums[k] == sum) {                     String str                      = nums[i] + ":" + nums[j]                                 + ":" + nums[k];                     if (!set.contains(str))                    {                         // To check for the unique triplet                        triplets.add(nums[i]);                        triplets.add(nums[j]);                        triplets.add(nums[k]);                        pair.add(triplets);                        triplets = new ArrayList<>();                        set.add(str);                    }                      // increment the second value index                    j++;                                         // decrement the third value index                    k--;                }                else if (nums[i]                         + nums[j]                         + nums[k] < sum)                    j++;                 else                    k--;            }        }        return pair;    }     // Driver code    public static void main(String[] args)    {        int[] nums = { 12, 3, 6, 1, 6, 9 };        int sum = 24;         List<List<Integer> > triplets            = findTriplets(nums, sum);         // Function call        if (!triplets.isEmpty()) {            System.out.println(triplets);        }        else {            System.out.println("No triplets can be formed");        }    }}
# Python program to find unique triplets# that sum up to a given value. # Function to find unique triplets that# sum up to a given value.def findTriplets(nums, n, Sum):    i = 0    j = 0    k = 0     # list to store all unique triplets.    triplet = []     # list to store already found triplets    # to avoid duplication.    uniqTriplets = []     # Variable used to hold triplet    # converted to string form.    temp = ""     # Variable used to store current    # triplet which is stored in vector    # if it is unique.    newTriplet = [0, 0, 0]     # Sort the input array.    nums.sort()     # Iterate over the array from the    # start and consider it as the    # first element.    for i in range(n - 2):                 # index of the first element in        # the remaining elements.        j = i + 1         # index of the last element.        k = n - 1         while(j < k):                       # If sum of triplet is equal to            # given value, then check if            # this triplet is unique or not.            # To check uniqueness, convert            # triplet to string form and            # then check if this string is            # present in set or not. If            # triplet is unique, then store            # it in list.            if(nums[i] + nums[j] + nums[k] == Sum):                temp = str(nums[i]) + ":" + str(nums[j]) + ":" + str(nums[k])                if temp not in uniqTriplets:                    uniqTriplets.append(temp)                    newTriplet[0] = nums[i]                    newTriplet[1] = nums[j]                    newTriplet[2] = nums[k]                    triplet.append(newTriplet)                    newTriplet = [0, 0, 0]                 # Increment the first index                # and decrement the last                # index of remaining elements.                j += 1                k -= 1                             # If sum is greater than given            # value then to reduce sum            # decrement the last index.            elif(nums[i] + nums[j] + nums[k] > Sum):                k -= 1                             # If sum is less than given value            # then to increase sum increment            # the first index of remaining            # elements.            else:                j += 1     # If no unique triplet is found, then       # return 0.    if(len(triplet) == 0):        return 0         # Print all unique triplets stored in    # list.    for i in range(len(triplet)):        print(triplet[i], end = ", ")    return 1 # Driver Codenums = [12, 3, 6, 1, 6, 9]n = len(nums)Sum = 24 # Function callif(not findTriplets(nums, n, Sum)):    print("No triplets can be formed.") # This code is contributed by rag2127
// C# program to find all triplets with given sumusing System;using System.Collections.Generic; class GFG {     // returns all triplets whose sum is    // equal to sum value    public static List<List<int>>      findTriplets(int[] nums,int sum)    {         /* Sort the elements */        Array.Sort(nums);         List<List<int> > pair          = new List<List<int> >();        SortedSet<String> set          = new SortedSet<String>();        List<int> triplets          = new List<int>();         /* Iterate over the array from the start and        consider it as the first element*/        for (int i = 0;             i < nums.Length - 2;             i++) {             // index of the first element in the            // remaining elements            int j = i + 1;             // index of the last element            int k = nums.Length - 1;             while (j < k) {                if (nums[i]                    + nums[j]                    + nums[k] == sum) {                    String str                      = nums[i] + ":" + nums[j]                                 + ":" + nums[k];                     if (!set.Contains(str))                    {                                               // To check for the unique triplet                        triplets.Add(nums[i]);                        triplets.Add(nums[j]);                        triplets.Add(nums[k]);                        pair.Add(triplets);                        triplets = new List<int>();                        set.Add(str);                    }                     j++; // increment the second value index                    k--; // decrement the third value index                }                else if (nums[i]                         + nums[j]                         + nums[k] < sum)                    j++;                 else                    k--;            }        }        return pair;    }     // Driver Code    public static void Main(String[] args)    {        int[] nums = { 12, 3, 6, 1, 6, 9 };        int sum = 24;         List<List<int> > triplets          = findTriplets(nums, sum);               // Function call        if (triplets.Count != 0) {            Console.Write("[");            for (int i = 0; i < triplets.Count; i++)            {                List<int> l = triplets[i];                Console.Write("[");                for (int j = 0; j < l.Count; j++)                {                    Console.Write(l[j]);                    if (l.Count != j + 1)                        Console.Write(", ");                }                Console.Write("]");                if (triplets.Count != i + 1)                    Console.Write(",");            }            Console.Write("]");        }        else        {            Console.WriteLine("No triplets can be formed");        }    }} // This code is contributed by 29AjayKumar
<script>// Javascript program to find all triplets with given sum         // returns all triplets whose sum is    //  equal to sum value    function findTriplets(nums,sum)    {        /* Sort the elements */        nums.sort(function(a,b){return a-b;});                 let pair = [];                 let set = new Set();                 let  triplets= [];                 //let newTriplet = [0, 0, 0];                 for (let i = 0;             i < nums.length - 2;             i++)        {            // index of the first element in the            // remaining elements            let j = i + 1;              // index of the last element            let k = nums.length - 1;              while (j < k) {                  if (nums[i] + nums[j]                    + nums[k] == sum) {                                            let str= nums[i] + ":" + nums[j]                                 + ":" + nums[k];                      if (!set.has(str))                    {                          // To check for the unique triplet                        triplets.push(nums[i]);                        triplets.push(nums[j]);                        triplets.push(nums[k]);                        pair.push(triplets);                        triplets =[];                        set.add(str);                    }                       // increment the second value index                    j++;                                          // decrement the third value index                    k--;                }                else if (nums[i]                         + nums[j]                         + nums[k] < sum)                    j++;                  else                    k--;            }        }        return pair;                      }         // Driver code    let nums=[12, 3, 6, 1, 6, 9];    let sum = 24;    let triplets            = findTriplets(nums, sum);    // Function call    if (triplets.length!=0) {        document.write("[");            for (let i = 0; i < triplets.length; i++)            {                let l = triplets[i];                document.write("[");                for (let j = 0; j < l.length; j++)                {                    document.write(l[j]);                    if (l.length != j + 1)                        document.write(",  ");                }                document.write("]");                if (triplets.length != i + 1)                    document.write(", ");            }            document.write("]");    }    else {        document.write("No triplets can be formed");    }         //This code is contributed by avanitrachhadiya2155     </script>
[3, 9, 12], [6, 6, 12],
Time Complexity: O(n2)Space Complexity: O(n)
Method 2
In this method, we will see another way to solve this problem which will use only a constant amount of space. Here in this method, we would check if the current element is the same as the previous one then we will not consider it and just simply skip that element. By using this we will be able to find only unique triplets only.
Algorithm
Sort the input array.For the first element iterate i from 0 to n-2.For each iteration we will have two indexes starts and end where the start would be equal to i+1 and end would be equal to n-1.Now we will look for a target whose value is sum-a[i] in the range from start to end using two pointer techniques.In this we will look for the previous elements if they are the same then we would not work for them to find all unique duplicates.If we found target==a[start]+a[end] then we would print it and increment start and decrement end.If target>a[start]+a[end] then we would increment start.Else we would decrement end
Sort the input array.
For the first element iterate i from 0 to n-2.
For each iteration we will have two indexes starts and end where the start would be equal to i+1 and end would be equal to n-1.
Now we will look for a target whose value is sum-a[i] in the range from start to end using two pointer techniques.
In this we will look for the previous elements if they are the same then we would not work for them to find all unique duplicates.
If we found target==a[start]+a[end] then we would print it and increment start and decrement end.
If target>a[start]+a[end] then we would increment start.
Else we would decrement end
Below is the implementation of the above idea:
C++
Java
Python3
C#
Javascript
// C++ program to find all unique triplets// without using any extra space.#include <bits/stdc++.h>using namespace std; // Function to all find unique triplets// without using extra spacevoid findTriplets(int a[], int n, int sum){    int i;     // Sort the input array    sort(a, a + n);     // For handling the cases when no such    // triplets exits.    bool flag = false;     // Iterate over the array from start to n-2.    for (i = 0; i < n - 2; i++)    {        if (i == 0 || a[i] > a[i - 1])        {            // Index of the first element in            // remaining range.            int start = i + 1;             // Index of the last element            int end = n - 1;             // Setting our new target            int target = sum - a[i];             while (start < end)            {                // Checking if current element                // is same as previous                if (start > i + 1                    && a[start] == a[start - 1])                {                    start++;                    continue;                }                 // Checking if current element is                // same as previous                if (end < n - 1                    && a[end] == a[end + 1])                {                    end--;                    continue;                }                 // If we found the triplets then print it                // and set the flag                if (target == a[start] + a[end])                {                    cout << "[" << a[i]                         << "," << a[start]                         << "," << a[end] << "] ";                    flag = true;                    start++;                    end--;                }                // If target is greater then                //  increment the start index                else if (target > (a[start] + a[end]))                {                    start++;                }                // If target is smaller than                // decrement the end index                else {                    end--;                }            }        }    }     // If no such triplets found    if (flag == false) {        cout << "No Such Triplets Exist"             << "\n";    }} // Driver codeint main(){    int a[] = { 12, 3, 6, 1, 6, 9 };    int n = sizeof(a) / sizeof(a[0]);    int sum = 24;     // Function call    findTriplets(a, n, sum);    return 0;} // This code is contributed by Rohit
// Java program to find all unique triplets// without using any extra space.import java.util.*;public class Main{    // Function to all find unique triplets    // without using extra space    public static void findTriplets(int a[], int n, int sum)    {        int i;               // Sort the input array        Arrays.sort(a);               // For handling the cases when no such        // triplets exits.        boolean flag = false;               // Iterate over the array from start to n-2.        for (i = 0; i < n - 2; i++)        {            if (i == 0 || a[i] > a[i - 1])            {                // Index of the first element in                // remaining range.                int start = i + 1;                       // Index of the last element                int end = n - 1;                       // Setting our new target                int target = sum - a[i];                       while (start < end)                {                    // Checking if current element                    // is same as previous                    if (start > i + 1                        && a[start] == a[start - 1])                    {                        start++;                        continue;                    }                           // Checking if current element is                    // same as previous                    if (end < n - 1                        && a[end] == a[end + 1])                    {                        end--;                        continue;                    }                           // If we found the triplets then print it                    // and set the flag                    if (target == a[start] + a[end])                    {                        System.out.print("[" + a[i]                             + "," + a[start]                             + "," + a[end] + "] ");                        flag = true;                        start++;                        end--;                    }                                        // If target is greater then                    //  increment the start index                    else if (target > (a[start] + a[end]))                    {                        start++;                    }                                        // If target is smaller than                    // decrement the end index                    else {                        end--;                    }                }            }        }               // If no such triplets found        if (flag == false) {            System.out.print("No Such Triplets Exist");        }    }         public static void main(String[] args) {        int a[] = { 12, 3, 6, 1, 6, 9 };        int n = a.length;        int sum = 24;               // Function call        findTriplets(a, n, sum);    }} // This code is contributed by divyeshrabadiya07
# Python3 program to find all# unique triplets without using# any extra space. # Function to all find unique# triplets without using extra# spacedef findTriplets(a, n, sum):     # Sort the input array    a.sort()     # For handling the cases    # when no such triplets exits.    flag = False     # Iterate over the array from    # start to n-2.    for i in range(n - 2):        if (i == 0 or            a[i] > a[i - 1]):             # Index of the first            # element in remaining            # range.            start = i + 1             # Index of the last            # element            end = n - 1             # Setting our new target            target = sum - a[i]             while (start < end):                 # Checking if current element                # is same as previous                if (start > i + 1 and                    a[start] == a[start - 1]):                     start += 1                    continue                 # Checking if current                # element is same as                # previous                if (end < n - 1 and                    a[end] == a[end + 1]):                    end -= 1                    continue                 # If we found the triplets                # then print it and set the                # flag                if (target == a[start] + a[end]):                    print("[", a[i], ",",                          a[start], ",",                          a[end], "]",                          end = " ")                    flag = True                    start += 1                    end -= 1                 # If target is greater then                #  increment the start index                elif (target >                     (a[start] + a[end])):                    start += 1                 # If target is smaller than                # decrement the end index                else:                    end -= 1     # If no such triplets found    if (flag == False):        print("No Such Triplets Exist") # Driver codeif __name__ == "__main__":     a = [12, 3, 6, 1, 6, 9]    n = len(a)    sum = 24     # Function call    findTriplets(a, n, sum) # This code is contributed by Chitranayal
// C# program to find all unique triplets// without using any extra space.using System;using System.Collections.Generic;class GFG {         // Function to all find unique triplets    // without using extra space    static void findTriplets(int[] a, int n, int sum)    {        int i;              // Sort the input array        Array.Sort(a);              // For handling the cases when no such        // triplets exits.        bool flag = false;              // Iterate over the array from start to n-2.        for (i = 0; i < n - 2; i++)        {            if (i == 0 || a[i] > a[i - 1])            {                // Index of the first element in                // remaining range.                int start = i + 1;                      // Index of the last element                int end = n - 1;                      // Setting our new target                int target = sum - a[i];                      while (start < end)                {                    // Checking if current element                    // is same as previous                    if (start > i + 1                        && a[start] == a[start - 1])                    {                        start++;                        continue;                    }                          // Checking if current element is                    // same as previous                    if (end < n - 1                        && a[end] == a[end + 1])                    {                        end--;                        continue;                    }                          // If we found the triplets then print it                    // and set the flag                    if (target == a[start] + a[end])                    {                        Console.Write("[" + a[i]                             + "," + a[start]                             + "," + a[end] + "] ");                        flag = true;                        start++;                        end--;                    }                                       // If target is greater then                    //  increment the start index                    else if (target > (a[start] + a[end]))                    {                        start++;                    }                                       // If target is smaller than                    // decrement the end index                    else {                        end--;                    }                }            }        }              // If no such triplets found        if (flag == false) {            Console.WriteLine("No Such Triplets Exist");        }    }   static void Main() {    int[] a = { 12, 3, 6, 1, 6, 9 };    int n = a.Length;    int sum = 24;      // Function call    findTriplets(a, n, sum);  }} // This code is contributed by divyesh072019
<script>    // Javascript program to find all unique triplets    // without using any extra space.         // Function to all find unique triplets    // without using extra space    function findTriplets(a, n, sum)    {        let i;         // Sort the input array        a.sort(function(a, b){return a - b});         // For handling the cases when no such        // triplets exits.        let flag = false;         // Iterate over the array from start to n-2.        for (i = 0; i < n - 2; i++)        {            if (i == 0 || a[i] > a[i - 1])            {                // Index of the first element in                // remaining range.                let start = i + 1;                 // Index of the last element                let end = n - 1;                 // Setting our new target                let target = sum - a[i];                 while (start < end)                {                    // Checking if current element                    // is same as previous                    if (start > i + 1                        && a[start] == a[start - 1])                    {                        start++;                        continue;                    }                     // Checking if current element is                    // same as previous                    if (end < n - 1                        && a[end] == a[end + 1])                    {                        end--;                        continue;                    }                     // If we found the triplets then print it                    // and set the flag                    if (target == a[start] + a[end])                    {                        document.write("[" + a[i]                             + "," + a[start]                             + "," + a[end] + "] ");                        flag = true;                        start++;                        end--;                    }                    // If target is greater then                    //  increment the start index                    else if (target > (a[start] + a[end]))                    {                        start++;                    }                    // If target is smaller than                    // decrement the end index                    else {                        end--;                    }                }            }        }         // If no such triplets found        if (flag == false) {            document.write("No Such Triplets Exist");        }    }         let a = [ 12, 3, 6, 1, 6, 9 ];    let n = a.length;    let sum = 24;      // Function call    a.sort();    findTriplets(a, n, sum);         // This code is contributed by suresh07.</script>
  
[3,9,12] [6,6,12]
 
Complexity Analysis:
 
Time Complexity: O(n2).Since two nested loops is required, so the time complexity is O(n2).
Auxiliary Space: O(1).Since we need no extra space for solving this.
 
This article is contributed by Aarthi C. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
 
nik1996
29AjayKumar
cueball
rohit101020samnit
ukasp
divyesh072019
divyeshrabadiya07
rag2127
suresh07
IllishaSingh
avanitrachhadiya2155
akshaysingh98088
Arrays
Hash
Searching
Arrays
Searching
Hash
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
Linear Search
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Hashing | Set 2 (Separate Chaining)
Most frequent element in an array