Skip to main content

Find a triplet that sum to a given value using sorting

Here given code implementation process.

import java.util.Arrays;
// Java program for
// Find a triplet that sum to a given value using sorting
public class Triplet
{
    // Display array elements
    public void printArray(int[] arr, int n)
    {
        for (int i = 0; i < n; ++i)
        {
            System.out.print(" " + arr[i]);
        }
    }
    public void tripletWithSum(int[] arr, int n, int x)
    {
        if (n > 3)
        {
            int []temp = new int[n];

            // Collect given array elements
            for (int i = 0; i < n ; ++i) 
            {
                temp[i] = arr[i];     
            } 
            // Sort temparary array
            Arrays.sort(temp);
            
            int a = 0;
            int b = a+1;
            int c = n-1;

            while(a < c)
            {
                while( b < c) 
               { 
                  if((temp[a] + temp[b] + temp[c]) > x)
                  {
                    // When triplet sum is greater than x
                    // Reduce last boundary element
                    c--;
                  }
                 
                  if( (b < c) && (temp[a] + temp[b] + temp[c]) == x) 
                  {
              
                    // When resultant triplet get
                    System.out.print("\n (" + 
                        temp[a] + " + " + 
                        temp[b] + " + " + 
                        temp[c] + ") = " + x);

                      return;
                  } 
                  b++; 
               } 
               a++; 
               // Reset the value
               b=a+1; 
               c=n-1; 
             }


        }
        System.out.print("\n Triplet of sum are " + x + " not exists");
    }
    public static void main(String[] args)
    {
        Triplet task = new Triplet();
        int[] arr =  {
            -2 , 5 , 4 , 8 , 3 , 1 , 7 , -1
        };
        int n = arr.length;
        task.printArray(arr, n);
        // Test A
        int x = 5;
        /*
            arr = [-2, 5, 4, 8, 3, 1, 7, -1]
            sum x = 5
            -------------------------------------------
            (-2 + -1 + 8) = 5
            -------------------------------------------
          
        */
        task.tripletWithSum(arr, n, x);
        // Test B
        x = 15;
        /*
            arr = [-2, 5, 4, 8, 3, 1, 7, -1]
            sum x = 5
            -------------------------------------------
            (3 + 4 + 8) = 15
            -------------------------------------------
        */
        task.tripletWithSum(arr, n, x);
        // Test C
        x = 19;
        /*
            arr = [-2, 5, 4, 8, 3, 1, 7, -1]
            sum x = 19
            -------------------------------------------
            (4 + 7 + 8) = 19
            ------------------------------------------- 
        */
        task.tripletWithSum(arr, n, x);
        // Test D
        x = 32;
        /*
            arr = [-2, 5, 4, 8, 3, 1, 7, -1]
            sum x = 32
            -------------------------------------------
            None
            -------------------------------------------
        */
        task.tripletWithSum(arr, n, x);
    }
}

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
// Include header file
#include <iostream>

#include <algorithm>

using namespace std;
// C++ program for
// Find a triplet that sum to a given value using sorting
class Triplet
{
	public:
		// Display array elements
		void printArray(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	void tripletWithSum(int arr[], int n, int x)
	{
		if (n > 3)
		{
			int temp[n];
			// Collect given array elements
			for (int i = 0; i < n; ++i)
			{
				temp[i] = arr[i];
			}
			// Sort temparary array
			sort(temp, temp + n);
			int a = 0;
			int b = a + 1;
			int c = n - 1;
			while (a < c)
			{
				while (b < c)
				{
					if ((temp[a] + temp[b] + temp[c]) > x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						c--;
					}
					if ((b < c) && (temp[a] + temp[b] + temp[c]) == x)
					{
						// When resultant triplet get
						cout << "\n (" 
                             << temp[a] << " + " << temp[b] 
                             << " + " << temp[c] << ") = " << x;
						return;
					}
					b++;
				}
				a++;
				// Reset the value
				b = a + 1;
				c = n - 1;
			}
		}
		cout << "\n Triplet of sum are " << x << " not exists";
	}
};
int main()
{
	Triplet *task = new Triplet();
	int arr[] = {
		-2 , 5 , 4 , 8 , 3 , 1 , 7 , -1
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	task->printArray(arr, n);
	// Test A
	int x = 5;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + -1 + 8) = 5
	    -------------------------------------------
	  
	*/
	task->tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (3 + 4 + 8) = 15
	    -------------------------------------------
	*/
	task->tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 7 + 8) = 19
	    ------------------------------------------- 
	*/
	task->tripletWithSum(arr, n, x);
	// Test D
	x = 32;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 32
	    -------------------------------------------
	    None
	    -------------------------------------------
	*/
	task->tripletWithSum(arr, n, x);
	return 0;
}

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
// Include namespace system
using System;
// Csharp program for
// Find a triplet that sum to a given value using sorting
public class Triplet
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public void tripletWithSum(int[] arr, int n, int x)
	{
		if (n > 3)
		{
			int[] temp = new int[n];
			// Collect given array elements
			for (int i = 0; i < n; ++i)
			{
				temp[i] = arr[i];
			}
			// Sort temparary array
			Array.Sort(temp);
			int a = 0;
			int b = a + 1;
			int c = n - 1;
			while (a < c)
			{
				while (b < c)
				{
					if ((temp[a] + temp[b] + temp[c]) > x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						c--;
					}
					if ((b < c) && (temp[a] + temp[b] + temp[c]) == x)
					{
						// When resultant triplet get
						Console.Write("\n (" + temp[a] + " + " + 
                                      temp[b] + " + " + temp[c] + ") = " + x);
						return;
					}
					b++;
				}
				a++;
				// Reset the value
				b = a + 1;
				c = n - 1;
			}
		}
		Console.Write("\n Triplet of sum are " + x + " not exists");
	}
	public static void Main(String[] args)
	{
		Triplet task = new Triplet();
		int[] arr = {
			-2 , 5 , 4 , 8 , 3 , 1 , 7 , -1
		};
		int n = arr.Length;
		task.printArray(arr, n);
		// Test A
		int x = 5;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (-2 + -1 + 8) = 5
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test B
		x = 15;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (3 + 4 + 8) = 15
		    -------------------------------------------
		*/
		task.tripletWithSum(arr, n, x);
		// Test C
		x = 19;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 19
		    -------------------------------------------
		    (4 + 7 + 8) = 19
		    ------------------------------------------- 
		*/
		task.tripletWithSum(arr, n, x);
		// Test D
		x = 32;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 32
		    -------------------------------------------
		    None
		    -------------------------------------------
		*/
		task.tripletWithSum(arr, n, x);
	}
}

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
package main
import "sort"
import "fmt"
// Go program for
// Find a triplet that sum to a given value using sorting

// Display array elements
func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func tripletWithSum(arr[] int, n int, x int) {
	if n > 3 {
		var temp = make([] int, n)
		// Collect given array elements
		for i := 0 ; i < n ; i++ {
			temp[i] = arr[i]
		}
		// Sort temparary array
		sort.Ints(temp)
		var a int = 0
		var b int = a + 1
		var c int = n - 1
		for (a < c) {
			for (b < c) {
				if (temp[a] + temp[b] + temp[c]) > x {
					// When triplet sum is greater than x
					// Reduce last boundary element
					c--
				}
				if (b < c) && (temp[a] + temp[b] + temp[c]) == x {
					// When resultant triplet get
					fmt.Print("\n (", temp[a], " + ", 
						temp[b], " + ", 
						temp[c], ") = ", x)
					return
				}
				b++
			}
			a++
			// Reset the value
			b = a + 1
			c = n - 1
		}
	}
	fmt.Print("\n Triplet of sum are ", x, " not exists")
}
func main() {
	
	var arr = [] int { -2, 5, 4, 8, 3, 1, 7, -1 }
	var n int = len(arr)
	printArray(arr, n)
	// Test A
	var x int = 5
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + -1 + 8) = 5
	    -------------------------------------------
	  
	*/
	tripletWithSum(arr, n, x)
	// Test B
	x = 15
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (3 + 4 + 8) = 15
	    -------------------------------------------
	*/
	tripletWithSum(arr, n, x)
	// Test C
	x = 19
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 7 + 8) = 19
	    ------------------------------------------- 
	*/
	tripletWithSum(arr, n, x)
	// Test D
	x = 32
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 32
	    -------------------------------------------
	    None
	    -------------------------------------------
	*/
	tripletWithSum(arr, n, x)
}

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
<?php
// Php program for
// Find a triplet that sum to a given value using sorting
class Triplet
{
	// Display array elements
	public	function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public	function tripletWithSum($arr, $n, $x)
	{
		if ($n > 3)
		{
			$temp = array_fill(0, $n, 0);
			// Collect given array elements
			for ($i = 0; $i < $n; ++$i)
			{
				$temp[$i] = $arr[$i];
			}
			// Sort temparary array
			sort($temp);
			$a = 0;
			$b = $a + 1;
			$c = $n - 1;
			while ($a < $c)
			{
				while ($b < $c)
				{
					if (($temp[$a] + $temp[$b] + $temp[$c]) > $x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						$c--;
					}
					if (($b < $c) && ($temp[$a] + $temp[$b] + $temp[$c]) == $x)
					{
						// When resultant triplet get
						echo("\n (".$temp[$a].
							" + ".$temp[$b].
							" + ".$temp[$c].
							") = ".$x);
						return;
					}
					$b++;
				}
				$a++;
				// Reset the value
				$b = $a + 1;
				$c = $n - 1;
			}
		}
		echo("\n Triplet of sum are ".$x.
			" not exists");
	}
}

function main()
{
	$task = new Triplet();
	$arr = array(-2, 5, 4, 8, 3, 1, 7, -1);
	$n = count($arr);
	$task->printArray($arr, $n);
	// Test A
	$x = 5;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + -1 + 8) = 5
	    -------------------------------------------
	  
	*/
	$task->tripletWithSum($arr, $n, $x);
	// Test B
	$x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (3 + 4 + 8) = 15
	    -------------------------------------------
	*/
	$task->tripletWithSum($arr, $n, $x);
	// Test C
	$x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 7 + 8) = 19
	    ------------------------------------------- 
	*/
	$task->tripletWithSum($arr, $n, $x);
	// Test D
	$x = 32;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 32
	    -------------------------------------------
	    None
	    -------------------------------------------
	*/
	$task->tripletWithSum($arr, $n, $x);
}
main();

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
// Node JS program for
// Find a triplet that sum to a given value using sorting
class Triplet
{
	// Display array elements
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	tripletWithSum(arr, n, x)
	{
		if (n > 3)
		{
			var temp = Array(n).fill(0);
			// Collect given array elements
			for (var i = 0; i < n; ++i)
			{
				temp[i] = arr[i];
			}
			// Sort temparary array
			temp.sort(function(a, b)
			{
				return a - b;
			});
			var a = 0;
			var b = a + 1;
			var c = n - 1;
			while (a < c)
			{
				while (b < c)
				{
					if ((temp[a] + temp[b] + temp[c]) > x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						c--;
					}
					if ((b < c) && (temp[a] + temp[b] + temp[c]) == x)
					{
						// When resultant triplet get
						process.stdout.write("\n (" + 
                                             temp[a] + " + " + 
                                             temp[b] + " + " + 
                                             temp[c] + ") = " + x);
						return;
					}
					b++;
				}
				a++;
				// Reset the value
				b = a + 1;
				c = n - 1;
			}
		}
		process.stdout.write("\n Triplet of sum are " + x + " not exists");
	}
}

function main()
{
	var task = new Triplet();
	var arr = [-2, 5, 4, 8, 3, 1, 7, -1];
	var n = arr.length;
	task.printArray(arr, n);
	// Test A
	var x = 5;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + -1 + 8) = 5
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (3 + 4 + 8) = 15
	    -------------------------------------------
	*/
	task.tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 7 + 8) = 19
	    ------------------------------------------- 
	*/
	task.tripletWithSum(arr, n, x);
	// Test D
	x = 32;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 32
	    -------------------------------------------
	    None
	    -------------------------------------------
	*/
	task.tripletWithSum(arr, n, x);
}
main();

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
#  Python 3 program for
#  Find a triplet that sum to a given value using sorting
class Triplet :
	#  Display list elements
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def tripletWithSum(self, arr, n, x) :
		if (n > 3) :
			temp = [0] * (n)
			i = 0
			#  Collect given list elements
			while (i < n) :
				temp[i] = arr[i]
				i += 1
			
			#  Sort temparary list
			temp.sort()
			a = 0
			b = a + 1
			c = n - 1
			while (a < c) :
				while (b < c) :
					if ((temp[a] + temp[b] + temp[c]) > x) :
						#  When triplet sum is greater than x
						#  Reduce last boundary element
						c -= 1
					
					if ((b < c) and(temp[a] + temp[b] + temp[c]) == x) :
						#  When resultant triplet get
						print("\n (", temp[a] ,"+", 
                              temp[b] ,"+", temp[c] ,
                              ") = ", x, end = "")
						return
					
					b += 1
				
				a += 1
				#  Reset the value
				b = a + 1
				c = n - 1
			
		
		print("\n Triplet of sum are ", x ," not exists", end = "")
	

def main() :
	task = Triplet()
	arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	n = len(arr)
	task.printArray(arr, n)
	#  Test A
	x = 5
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 5
	#    -------------------------------------------
	#    (-2 + -1 + 8) = 5
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test B
	x = 15
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 5
	#    -------------------------------------------
	#    (3 + 4 + 8) = 15
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test C
	x = 19
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 19
	#    -------------------------------------------
	#    (4 + 7 + 8) = 19
	#    ------------------------------------------- 
	task.tripletWithSum(arr, n, x)
	#  Test D
	x = 32
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 32
	#    -------------------------------------------
	#    None
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)

if __name__ == "__main__": main()

Output

  -2  5  4  8  3  1  7  -1
 ( -2 + -1 + 8 ) =  5
 ( 3 + 4 + 8 ) =  15
 ( 4 + 7 + 8 ) =  19
 Triplet of sum are  32  not exists
#  Ruby program for
#  Find a triplet that sum to a given value using sorting
class Triplet 
	#  Display array elements
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def tripletWithSum(arr, n, x) 
		if (n > 3) 
			temp = arr.sort

			#  Sort temparary array
			temp.sort
			a = 0
			b = a + 1
			c = n - 1
			while (a < c) 
				while (b < c) 
					if ((temp[a] + temp[b] + temp[c]) > x) 
						#  When triplet sum is greater than x
						#  Reduce last boundary element
						c -= 1
					end

					if ((b < c) && (temp[a] + temp[b] + temp[c]) == x) 
						#  When resultant triplet get
						print("\n (", temp[a] ," + ", 
                              temp[b] ," + ", temp[c] ,") = ", x)
						return
					end

					b += 1
				end

				a += 1
				#  Reset the value
				b = a + 1
				c = n - 1
			end

		end

		print("\n Triplet of sum are ", x ," not exists")
	end

end

def main() 
	task = Triplet.new()
	arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	n = arr.length
	task.printArray(arr, n)
	#  Test A
	x = 5
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 5
	#    -------------------------------------------
	#    (-2 + -1 + 8) = 5
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test B
	x = 15
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 5
	#    -------------------------------------------
	#    (3 + 4 + 8) = 15
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test C
	x = 19
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 19
	#    -------------------------------------------
	#    (4 + 7 + 8) = 19
	#    ------------------------------------------- 
	task.tripletWithSum(arr, n, x)
	#  Test D
	x = 32
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 32
	#    -------------------------------------------
	#    None
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
end

main()

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
import scala.collection.mutable._;
// Scala program for
// Find a triplet that sum to a given value using sorting
class Triplet()
{
	// Display array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def tripletWithSum(arr: Array[Int], n: Int, x: Int): Unit = {
		if (n > 3)
		{
			var temp = 	arr.sorted;
			var a: Int = 0;
			var b: Int = a + 1;
			var c: Int = n - 1;
			while (a < c)
			{
				while (b < c)
				{
					if ((temp(a) + temp(b) + temp(c)) > x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						c -= 1;
					}
					if ((b < c) && (temp(a) + temp(b) + temp(c)) == x)
					{
						// When resultant triplet get
						print("\n (" + temp(a) + " + " + 
                             		   temp(b) + " + " + 
                                       temp(c) + ") = " + x);
						return;
					}
					b += 1;
				}
				a += 1;
				// Reset the value
				b = a + 1;
				c = n - 1;
			}
		}
		print("\n Triplet of sum are " + x + " not exists");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Triplet = new Triplet();
		var arr: Array[Int] = Array(-2, 5, 4, 8, 3, 1, 7, -1);
		var n: Int = arr.length;
		task.printArray(arr, n);
		// Test A
		var x: Int = 5;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (-2 + -1 + 8) = 5
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test B
		x = 15;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (3 + 4 + 8) = 15
		    -------------------------------------------
		*/
		task.tripletWithSum(arr, n, x);
		// Test C
		x = 19;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 19
		    -------------------------------------------
		    (4 + 7 + 8) = 19
		    ------------------------------------------- 
		*/
		task.tripletWithSum(arr, n, x);
		// Test D
		x = 32;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 32
		    -------------------------------------------
		    None
		    -------------------------------------------
		*/
		task.tripletWithSum(arr, n, x);
	}
}

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists
import Foundation;
// Swift 4 program for
// Find a triplet that sum to a given value using sorting
class Triplet
{
	// Display array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func tripletWithSum(_ arr: [Int], _ n: Int, _ x: Int)
	{
		if (n > 3)
		{
			var temp: [Int] = arr.sorted();
			
			var a: Int = 0;
			var b: Int = a + 1;
			var c: Int = n - 1;
			while (a < c)
			{
				while (b < c)
				{
					if ((temp[a] + temp[b] + temp[c]) > x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						c -= 1;
					}
					if ((b < c) && (temp[a] + temp[b] + temp[c]) == x)
					{
						// When resultant triplet get
						print("\n (", temp[a] ,"+", temp[b] ,"+", temp[c] ,") = ", x, terminator: "");
						return;
					}
					b += 1;
				}
				a += 1;
				// Reset the value
				b = a + 1;
				c = n - 1;
			}
		}
		print("\n Triplet of sum are ", x ," not exists", terminator: "");
	}
}
func main()
{
	let task: Triplet = Triplet();
	let arr: [Int] = [-2, 5, 4, 8, 3, 1, 7, -1];
	let n: Int = arr.count;
	task.printArray(arr, n);
	// Test A
	var x: Int = 5;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + -1 + 8) = 5
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (3 + 4 + 8) = 15
	    -------------------------------------------
	*/
	task.tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 7 + 8) = 19
	    ------------------------------------------- 
	*/
	task.tripletWithSum(arr, n, x);
	// Test D
	x = 32;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 32
	    -------------------------------------------
	    None
	    -------------------------------------------
	*/
	task.tripletWithSum(arr, n, x);
}
main();

Output

  -2  5  4  8  3  1  7  -1
 ( -2 + -1 + 8 ) =  5
 ( 3 + 4 + 8 ) =  15
 ( 4 + 7 + 8 ) =  19
 Triplet of sum are  32  not exists
// Kotlin program for
// Find a triplet that sum to a given value using sorting
class Triplet
{
	// Display array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun tripletWithSum(arr: Array < Int > , n: Int, x: Int): Unit
	{
		if (n > 3)
		{
			var temp: Array < Int > = Array(n)
			{
				0
			};
			var i: Int = 0;
			// Collect given array elements
			while (i < n)
			{
				temp[i] = arr[i];
				i += 1;
			}
			// Sort temparary array
			temp.sort();
			var a: Int = 0;
			var b: Int = a + 1;
			var c: Int = n - 1;
			while (a < c)
			{
				while (b < c)
				{
					if ((temp[a] + temp[b] + temp[c]) > x)
					{
						// When triplet sum is greater than x
						// Reduce last boundary element
						c -= 1;
					}
					if ((b < c) && (temp[a] + temp[b] + temp[c]) == x)
					{
						// When resultant triplet get
						print("\n (" + temp[a] + " + " + 
                              temp[b] + " + " + 
                              temp[c] + ") = " + x);
						return;
					}
					b += 1;
				}
				a += 1;
				// Reset the value
				b = a + 1;
				c = n - 1;
			}
		}
		print("\n Triplet of sum are " + x + " not exists");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Triplet = Triplet();
	val arr: Array < Int > = arrayOf(-2, 5, 4, 8, 3, 1, 7, -1);
	val n: Int = arr.count();
	task.printArray(arr, n);
	// Test A
	var x: Int = 5;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + -1 + 8) = 5
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (3 + 4 + 8) = 15
	    -------------------------------------------
	*/
	task.tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 7 + 8) = 19
	    ------------------------------------------- 
	*/
	task.tripletWithSum(arr, n, x);
	// Test D
	x = 32;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 32
	    -------------------------------------------
	    None
	    -------------------------------------------
	*/
	task.tripletWithSum(arr, n, x);
}

Output

 -2 5 4 8 3 1 7 -1
 (-2 + -1 + 8) = 5
 (3 + 4 + 8) = 15
 (4 + 7 + 8) = 19
 Triplet of sum are 32 not exists




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment