Find a triplet that sum to a given value

Here given code implementation process.

/*
    C program for
    Find a triplet that sum to a given value
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", arr[i]);
	}
}
void tripletWithSum(int arr[], int n, int x)
{
	if (n > 3)
	{
		for (int i = 0; i < n; ++i)
		{
			for (int j = i + 1; j < n; ++j)
			{
				for (int k = j + 1; k < n; ++k)
				{
					if (arr[i] + arr[j] + arr[k] == x)
					{
						printf("\n (%d + %d + %d) = %d", 
                               arr[i], arr[j], arr[k], x);
						return;
					}
				}
			}
		}
	}
	printf("\n Triplet of sum are %d not exists", x);
}
int main(int argc, char
	const *argv[])
{
	int arr[] = {
		-2 , 5 , 4 , 8 , 3 , 1 , 7 , -1
	};
	int n = sizeof(arr) / sizeof(arr[0]);
	printArray(arr, n);
	// Test A
	int x = 5;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (-2 + 4 + 3) = 5
	    -----------------------
	  
	*/
	tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -----------------------
	  
	*/
	tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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);
	return 0;
}

Output

  -2  5  4  8  3  1  7  -1
 (-2 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
// Java program for
// Find a triplet that sum to a given value
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)
		{
			for (int i = 0; i < n; ++i)
			{
				for (int j = i + 1; j < n; ++j)
				{
					for (int k = j + 1; k < n; ++k)
					{
						if (arr[i] + arr[j] + arr[k] == x)
						{
							System.out.print("\n (" + 
                                             arr[i] + " + " + 
                                             arr[j] + " + " + 
                                             arr[k] + ") = " + x);
							return;
						}
					}
				}
			}
		}
		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 + 4 + 3) = 5
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test B
		x = 15;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (5 + 3 + 7) = 15
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test C
		x = 19;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 19
		    -------------------------------------------
		    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Find a triplet that sum to a given value
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)
		{
			for (int i = 0; i < n; ++i)
			{
				for (int j = i + 1; j < n; ++j)
				{
					for (int k = j + 1; k < n; ++k)
					{
						if (arr[i] + arr[j] + arr[k] == x)
						{
							cout << "\n (" 
                                 << arr[i] 
                                 << " + " 
                                 << arr[j] << " + " 
                                 << arr[k] << ") = " << x;
							return;
						}
					}
				}
			}
		}
		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 + 4 + 3) = 5
	    -------------------------------------------
	  
	*/
	task->tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -------------------------------------------
	  
	*/
	task->tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 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
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)
		{
			for (int i = 0; i < n; ++i)
			{
				for (int j = i + 1; j < n; ++j)
				{
					for (int k = j + 1; k < n; ++k)
					{
						if (arr[i] + arr[j] + arr[k] == x)
						{
							Console.Write("\n (" + 
                                          arr[i] + " + " + 
                                          arr[j] + " + " + 
                                          arr[k] + ") = " + x);
							return;
						}
					}
				}
			}
		}
		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 + 4 + 3) = 5
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test B
		x = 15;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (5 + 3 + 7) = 15
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test C
		x = 19;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 19
		    -------------------------------------------
		    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
package main
import "fmt"
// Go program for
// Find a triplet that sum to a given value

// 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 {
		for i := 0 ; i < n ; i++ {
			for j := i + 1 ; j < n ; j++ {
				for k := j + 1 ; k < n ; k++ {
					if arr[i] + arr[j] + arr[k] == x {
						fmt.Print("\n (", 
							arr[i], " + ", arr[j], 
							" + ", arr[k], ") = ", x)
						return
					}
				}
			}
		}
	}
	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 + 4 + 3) = 5
	    -------------------------------------------
	  
	*/
	tripletWithSum(arr, n, x)
	// Test B
	x = 15
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -------------------------------------------
	  
	*/
	tripletWithSum(arr, n, x)
	// Test C
	x = 19
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
<?php
// Php program for
// Find a triplet that sum to a given value
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)
		{
			for ($i = 0; $i < $n; ++$i)
			{
				for ($j = $i + 1; $j < $n; ++$j)
				{
					for ($k = $j + 1; $k < $n; ++$k)
					{
						if ($arr[$i] + $arr[$j] + $arr[$k] == $x)
						{
							echo("\n (".$arr[$i].
								" + ".$arr[$j].
								" + ".$arr[$k].
								") = ".$x);
							return;
						}
					}
				}
			}
		}
		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 + 4 + 3) = 5
	    -------------------------------------------
	  
	*/
	$task->tripletWithSum($arr, $n, $x);
	// Test B
	$x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -------------------------------------------
	  
	*/
	$task->tripletWithSum($arr, $n, $x);
	// Test C
	$x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
// Node JS program for
// Find a triplet that sum to a given value
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)
		{
			for (var i = 0; i < n; ++i)
			{
				for (var j = i + 1; j < n; ++j)
				{
					for (var k = j + 1; k < n; ++k)
					{
						if (arr[i] + arr[j] + arr[k] == x)
						{
							process.stdout.write("\n (" + 
                                                 arr[i] + " + " +
                                                 arr[j] + " + " + 
                                                 arr[k] + ") = " + x);
							return;
						}
					}
				}
			}
		}
		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 + 4 + 3) = 5
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
#  Python 3 program for
#  Find a triplet that sum to a given value
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) :
			i = 0
			while (i < n) :
				j = i + 1
				while (j < n) :
					k = j + 1
					while (k < n) :
						if (arr[i] + arr[j] + arr[k] == x) :
							print("\n (", 
                                  arr[i] ," + ", 
                                  arr[j] ," + ", 
                                  arr[k] ,") = ", x, end = "")
							return
						
						k += 1
					
					j += 1
				
				i += 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 + 4 + 3) = 5
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test B
	x = 15
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 5
	#    -------------------------------------------
	#    (5 + 3 + 7) = 15
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test C
	x = 19
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 19
	#    -------------------------------------------
	#    (4 + 8 + 7) = 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  +  4  +  3 ) =  5
 ( 5  +  3  +  7 ) =  15
 ( 4  +  8  +  7 ) =  19
 Triplet of sum are  32  not exists
#  Ruby program for
#  Find a triplet that sum to a given value
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) 
			i = 0
			while (i < n) 
				j = i + 1
				while (j < n) 
					k = j + 1
					while (k < n) 
						if (arr[i] + arr[j] + arr[k] == x) 
							print("\n (", 
                                  arr[i] ," + ", 
                                  arr[j] ," + ", 
								  arr[k] ,") = ", x)
							return
						end

						k += 1
					end

					j += 1
				end

				i += 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 + 4 + 3) = 5
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test B
	x = 15
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 5
	#    -------------------------------------------
	#    (5 + 3 + 7) = 15
	#    -------------------------------------------
	task.tripletWithSum(arr, n, x)
	#  Test C
	x = 19
	#    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	#    sum x = 19
	#    -------------------------------------------
	#    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
// Scala program for
// Find a triplet that sum to a given value
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 i: Int = 0;
			while (i < n)
			{
				var j: Int = i + 1;
				while (j < n)
				{
					var k: Int = j + 1;
					while (k < n)
					{
						if (arr(i) + arr(j) + arr(k) == x)
						{
							print("\n (" + 
                                  arr(i) + " + " + 
                                  arr(j) + " + " + 
                                  arr(k) + ") = " + x);
							return;
						}
						k += 1;
					}
					j += 1;
				}
				i += 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 + 4 + 3) = 5
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test B
		x = 15;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 5
		    -------------------------------------------
		    (5 + 3 + 7) = 15
		    -------------------------------------------
		  
		*/
		task.tripletWithSum(arr, n, x);
		// Test C
		x = 19;
		/*
		    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
		    sum x = 19
		    -------------------------------------------
		    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists
import Foundation;
// Swift 4 program for
// Find a triplet that sum to a given value
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 i: Int = 0;
			while (i < n)
			{
				var j: Int = i + 1;
				while (j < n)
				{
					var k: Int = j + 1;
					while (k < n)
					{
						if (arr[i] + arr[j] + arr[k] == x)
						{
							print("\n (", 
                                  arr[i] ," + ", 
                                  arr[j] ," + ", 
                                  arr[k] ,") = ", x, terminator: "");
							return;
						}
						k += 1;
					}
					j += 1;
				}
				i += 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 + 4 + 3) = 5
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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  +  4  +  3 ) =  5
 ( 5  +  3  +  7 ) =  15
 ( 4  +  8  +  7 ) =  19
 Triplet of sum are  32  not exists
// Kotlin program for
// Find a triplet that sum to a given value
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 i: Int = 0;
			while (i < n)
			{
				var j: Int = i + 1;
				while (j < n)
				{
					var k: Int = j + 1;
					while (k < n)
					{
						if (arr[i] + arr[j] + arr[k] == x)
						{
							print("\n (" + 
                                  arr[i] + " + " + 
                                  arr[j] + " + " + 
                                  arr[k] + ") = " + x);
							return;
						}
						k += 1;
					}
					j += 1;
				}
				i += 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 + 4 + 3) = 5
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test B
	x = 15;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 5
	    -------------------------------------------
	    (5 + 3 + 7) = 15
	    -------------------------------------------
	  
	*/
	task.tripletWithSum(arr, n, x);
	// Test C
	x = 19;
	/*
	    arr = [-2, 5, 4, 8, 3, 1, 7, -1]
	    sum x = 19
	    -------------------------------------------
	    (4 + 8 + 7) = 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 + 4 + 3) = 5
 (5 + 3 + 7) = 15
 (4 + 8 + 7) = 19
 Triplet of sum are 32 not exists


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







© 2021, kalkicode.com, All rights reserved