Find maximum triplet in array

Here given code implementation process.

// C Program
// Find maximum triplet in array
#include <stdio.h>
#include <limits.h>

//Function which is display array elements
void display(int arr[], int size)
{
    for (int i = 0; i < size; ++i)
    {
        printf("%d  ", arr[i]);
    }
    printf("\n");
}
void maximumTriplet(int arr[], int size)
{
    if (size < 3)
    {
        return;
    }
    //initial set the minimum value of variables
    int first = INT_MIN;
    int second = INT_MIN;
    int third = INT_MIN;
    for (int i = 0; i < size; ++i)
    {
        //Check if whether array element i value is largest than of variable first 
        if (arr[i] > first)
        {
            //modify the value of all three variables
            //like a swap operation of two variable
            third = second;
            second = first;
            //Assign a new big value
            first = arr[i];
        }
        else if (arr[i] > second)
        {
            //When get second largest element
            third = second;
            //Assign a new big value
            second = arr[i];
        }
        else if (arr[i] > third)
        {
            //When get third largest element
            third = arr[i];
        }
    }
    display(arr, size);
    printf("Maximum Triplet Sum [%d + %d + %d] : %d\n", first, second, third, first + second + third);
}
int main()
{
    //Define array elements
    int arr1[] = {
        6 , 11 , 1 , 3 , 6 , 19 , 8 , 4 , 2 , 5 , 7 , 12
    };
    int size = sizeof(arr1) / sizeof(arr1[0]);
    maximumTriplet(arr1, size);
    //Define array elements
    int arr2[] = {
        6 , 9 , 2 , 1 , 5 , 7 , 3 , 8 , 12 , 4
    };
    size = sizeof(arr2) / sizeof(arr2[0]);
    maximumTriplet(arr2, size);
    return 0;
}

Output

6  11  1  3  6  19  8  4  2  5  7  12
Maximum Triplet Sum [19 + 12 + 11] : 42
6  9  2  1  5  7  3  8  12  4
Maximum Triplet Sum [12 + 9 + 8] : 29
/*
  Java Program
  Find maximum triplet in array
*/
public class Triplets
{
	//Function which is display array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	public void maximumTriplet(int[] arr, int size)
	{
		if (size < 3)
		{
			return;
		}
		//initial set the minimum value of variables
		int first = Integer.MIN_VALUE;
		int second = Integer.MIN_VALUE;
		int third = Integer.MIN_VALUE;
		for (int i = 0; i < size; ++i)
		{
			//Check if whether array element i value is largest than of variable first 
			if (arr[i] > first)
			{
				//modify the value of all three variables
				//like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr[i];
			}
			else if (arr[i] > second)
			{
				//When get second largest element
				third = second;
				//Assign a new big value
				second = arr[i];
			}
			else if (arr[i] > third)
			{
				//When get third largest element
				third = arr[i];
			}
		}
		display(arr, size);
		System.out.print("  Maximum Triplet Sum [" + first + " + " + second + " + " + third + "] : " + (first + second + third) + "\n");
	}
	public static void main(String[] args)
	{
		Triplets obj = new Triplets();
		//Define array elements
		int[] arr1 = {
			6 , 11 , 1 , 3 , 6 , 19 , 8 , 4 , 2 , 5 , 7 , 12
		};
		int size = arr1.length;
		obj.maximumTriplet(arr1, size);
		//Define array elements
		int[] arr2 = {
			6 , 9 , 2 , 1 , 5 , 7 , 3 , 8 , 12 , 4
		};
		size = arr2.length;
		obj.maximumTriplet(arr2, size);
	}
}

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29
// Include header file
#include <iostream>
#include<limits.h>
using namespace std;
/*
  C++ Program
  Find maximum triplet in array
*/
class Triplets
{
	public:
		//Function which is display array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	void maximumTriplet(int arr[], int size)
	{
		if (size < 3)
		{
			return;
		}
		//initial set the minimum value of variables
		int first = INT_MIN;
		int second = INT_MIN;
		int third = INT_MIN;
		for (int i = 0; i < size; ++i)
		{
			//Check if whether array element i value is largest than of variable first
			if (arr[i] > first)
			{
				//modify the value of all three variables//like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr[i];
			}
			else if (arr[i] > second)
			{
				//When get second largest element
				third = second;
				//Assign a new big value
				second = arr[i];
			}
			else if (arr[i] > third)
			{
				//When get third largest element
				third = arr[i];
			}
		}
		this->display(arr, size);
		cout << "  Maximum Triplet Sum [" << first << " << " << second << " << " << third << "] : " << (first + second + third) << "\n";
	}
};
int main()
{
	Triplets obj = Triplets();
	//Define array elements
	int arr1[] = {
		6 , 11 , 1 , 3 , 6 , 19 , 8 , 4 , 2 , 5 , 7 , 12
	};
	int size = sizeof(arr1) / sizeof(arr1[0]);
	obj.maximumTriplet(arr1, size);
	//Define array elements
	int arr2[] = {
		6 , 9 , 2 , 1 , 5 , 7 , 3 , 8 , 12 , 4
	};
	size = sizeof(arr2) / sizeof(arr2[0]);
	obj.maximumTriplet(arr2, size);
	return 0;
}

Output

6  11  1  3  6  19  8  4  2  5  7  12
Maximum Triplet Sum [19 + 12 + 11] : 42
6  9  2  1  5  7  3  8  12  4
Maximum Triplet Sum [12 + 9 + 8] : 29
// Include namespace system
using System;
/*
  C# Program
  Find maximum triplet in array
*/
public class Triplets
{
	// Function which is display array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	public void maximumTriplet(int[] arr, int size)
	{
		if (size < 3)
		{
			return;
		}
		// initial set the minimum value of variables
		int first = int.MinValue;
		int second = int.MinValue;
		int third = int.MinValue;
		for (int i = 0; i < size; ++i)
		{
			// Check if whether array element i value is largest than of variable first
			if (arr[i] > first)
			{
				// modify the value of all three variables// like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr[i];
			}
			else if (arr[i] > second)
			{
				// When get second largest element
				third = second;
				// Assign a new big value
				second = arr[i];
			}
			else if (arr[i] > third)
			{
				// When get third largest element
				third = arr[i];
			}
		}
		display(arr, size);
		Console.Write("  Maximum Triplet Sum [" + first + " + " + second + " + " + third + "] : " + (first + second + third) + "\n");
	}
	public static void Main(String[] args)
	{
		Triplets obj = new Triplets();
		// Define array elements
		int[] arr1 = {
			6 , 11 , 1 , 3 , 6 , 19 , 8 , 4 , 2 , 5 , 7 , 12
		};
		int size = arr1.Length;
		obj.maximumTriplet(arr1, size);
		// Define array elements
		int[] arr2 = {
			6 , 9 , 2 , 1 , 5 , 7 , 3 , 8 , 12 , 4
		};
		size = arr2.Length;
		obj.maximumTriplet(arr2, size);
	}
}

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29
<?php
/*
  Php Program
  Find maximum triplet in array
*/
class Triplets
{
	// Function which is display array elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	public	function maximumTriplet( & $arr, $size)
	{
		if ($size < 3)
		{
			return;
		}
		// initial set the minimum value of variables
		$first = -PHP_INT_MAX;
		$second = -PHP_INT_MAX;
		$third = -PHP_INT_MAX;
		for ($i = 0; $i < $size; ++$i)
		{
			// Check if whether array element i value is largest than of variable first
			if ($arr[$i] > $first)
			{
				// modify the value of all three variables// like a swap operation of two variable
				$third = $second;
				$second = $first;
				//Assign a new big value
				$first = $arr[$i];
			}
			else if ($arr[$i] > $second)
			{
				// When get second largest element
				$third = $second;
				// Assign a new big value
				$second = $arr[$i];
			}
			else if ($arr[$i] > $third)
			{
				// When get third largest element
				$third = $arr[$i];
			}
		}
		$this->display($arr, $size);
		echo "  Maximum Triplet Sum [". $first ." + ". $second ." + ". $third ."] : ". ($first + $second + $third) ."\n";
	}
}

function main()
{
	$obj = new Triplets();
	// Define array elements
	$arr1 = array(6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12);
	$size = count($arr1);
	$obj->maximumTriplet($arr1, $size);
	// Define array elements
	$arr2 = array(6, 9, 2, 1, 5, 7, 3, 8, 12, 4);
	$size = count($arr2);
	$obj->maximumTriplet($arr2, $size);
}
main();

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29
/*
  Node Js Program
  Find maximum triplet in array
*/
class Triplets
{
	// Function which is display array elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	maximumTriplet(arr, size)
	{
		if (size < 3)
		{
			return;
		}
		// initial set the minimum value of variables
		var first = -Number.MAX_VALUE;
		var second = -Number.MAX_VALUE;
		var third = -Number.MAX_VALUE;
		for (var i = 0; i < size; ++i)
		{
			// Check if whether array element i value is largest than of variable first
			if (arr[i] > first)
			{
				// modify the value of all three variables// like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr[i];
			}
			else if (arr[i] > second)
			{
				// When get second largest element
				third = second;
				// Assign a new big value
				second = arr[i];
			}
			else if (arr[i] > third)
			{
				// When get third largest element
				third = arr[i];
			}
		}
		this.display(arr, size);
		process.stdout.write("  Maximum Triplet Sum [" + first + " + " + second + " + " + third + "] : " + (first + second + third) + "\n");
	}
}

function main()
{
	var obj = new Triplets();
	// Define array elements
	var arr1 = [6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12];
	var size = arr1.length;
	obj.maximumTriplet(arr1, size);
	// Define array elements
	var arr2 = [6, 9, 2, 1, 5, 7, 3, 8, 12, 4];
	size = arr2.length;
	obj.maximumTriplet(arr2, size);
}
main();

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29
import sys
#   Python 3 Program
#   Find maximum triplet in array

class Triplets :
	#  Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def maximumTriplet(self, arr, size) :
		if (size < 3) :
			return
		
		#  initial set the minimum value of variables
		first = -sys.maxsize
		second = -sys.maxsize
		third = -sys.maxsize
		i = 0
		while (i < size) :
			#  Check if whether array element i value is largest than of variable first
			if (arr[i] > first) :
				#  modify the value of all three variables// like a swap operation of two variable
				third = second
				second = first
				# Assign a new big value
				first = arr[i]
			
			elif(arr[i] > second) :
				#  When get second largest element
				third = second
				#  Assign a new big value
				second = arr[i]
			
			elif(arr[i] > third) :
				#  When get third largest element
				third = arr[i]
			
			i += 1
		
		self.display(arr, size)
		print("  Maximum Triplet Sum [", first ,"+", second ,"+", third ,"] : ", (first + second + third) )
	

def main() :
	obj = Triplets()
	#  Define array elements
	arr1 = [6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12]
	size = len(arr1)
	obj.maximumTriplet(arr1, size)
	#  Define array elements
	arr2 = [6, 9, 2, 1, 5, 7, 3, 8, 12, 4]
	size = len(arr2)
	obj.maximumTriplet(arr2, size)

if __name__ == "__main__": main()

Output

   6   11   1   3   6   19   8   4   2   5   7   12
  Maximum Triplet Sum [ 19 + 12 + 11 ] :  42
   6   9   2   1   5   7   3   8   12   4
  Maximum Triplet Sum [ 12 + 9 + 8 ] :  29
#   Ruby Program
#   Find maximum triplet in array

class Triplets 
	#  Function which is display array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	def maximumTriplet(arr, size) 
		if (size < 3) 
			return
		end

		#  initial set the minimum value of variables
		first = -(2 ** (0. size * 8 - 2))
		second = -(2 ** (0. size * 8 - 2))
		third = -(2 ** (0. size * 8 - 2))
		i = 0
		while (i < size) 
			#  Check if whether array element i value is largest than of variable first
			if (arr[i] > first) 
				#  modify the value of all three variables// like a swap operation of two variable
				third = second
				second = first
				# Assign a new big value
				first = arr[i]
			elsif(arr[i] > second) 
				#  When get second largest element
				third = second
				#  Assign a new big value
				second = arr[i]
			elsif(arr[i] > third) 
				#  When get third largest element
				third = arr[i]
			end

			i += 1
		end

		self.display(arr, size)
		print("  Maximum Triplet Sum [", first ," + ", second ," + ", third ,"] : ", (first + second + third) ,"\n")
	end

end

def main() 
	obj = Triplets.new()
	#  Define array elements
	arr1 = [6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12]
	size = arr1.length
	obj.maximumTriplet(arr1, size)
	#  Define array elements
	arr2 = [6, 9, 2, 1, 5, 7, 3, 8, 12, 4]
	size = arr2.length
	obj.maximumTriplet(arr2, size)
end

main()

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29
/*
  Scala Program
  Find maximum triplet in array
*/
class Triplets
{
	// Function which is display array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	def maximumTriplet(arr: Array[Int], size: Int): Unit = {
		if (size < 3)
		{
			return;
		}
		// initial set the minimum value of variables
		var first: Int = Int.MinValue;
		var second: Int = Int.MinValue;
		var third: Int = Int.MinValue;
		var i: Int = 0;
		while (i < size)
		{
			// Check if whether array element i value is largest than of variable first
			if (arr(i) > first)
			{
				// modify the value of all three variables// like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr(i);
			}
			else if (arr(i) > second)
			{
				// When get second largest element
				third = second;
				// Assign a new big value
				second = arr(i);
			}
			else if (arr(i) > third)
			{
				// When get third largest element
				third = arr(i);
			}
			i += 1;
		}
		this.display(arr, size);
		print("  Maximum Triplet Sum [" + first + " + " + second + " + " + third + "] : " + (first + second + third) + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Triplets = new Triplets();
		// Define array elements
		var arr1: Array[Int] = Array(6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12);
		var size: Int = arr1.length;
		obj.maximumTriplet(arr1, size);
		// Define array elements
		var arr2: Array[Int] = Array(6, 9, 2, 1, 5, 7, 3, 8, 12, 4);
		size = arr2.length;
		obj.maximumTriplet(arr2, size);
	}
}

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29
/*
  Swift 4 Program
  Find maximum triplet in array
*/
class Triplets
{
	// Function which is display array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func maximumTriplet(_ arr: [Int], _ size: Int)
	{
		if (size < 3)
		{
			return;
		}
		// initial set the minimum value of variables
		var first: Int = Int.min;
		var second: Int = Int.min;
		var third: Int = Int.min;
		var i: Int = 0;
		while (i < size)
		{
			// Check if whether array element i value is largest than of variable first
			if (arr[i] > first)
			{
				// modify the value of all three variables// like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr[i];
			}
			else if (arr[i] > second)
			{
				// When get second largest element
				third = second;
				// Assign a new big value
				second = arr[i];
			}
			else if (arr[i] > third)
			{
				// When get third largest element
				third = arr[i];
			}
			i += 1;
		}
		self.display(arr, size);
		print("  Maximum Triplet Sum [", first ,"+", second ,"+", third ,"]: ", (first + second + third) );
	}
}
func main()
{
	let obj: Triplets = Triplets();
	// Define array elements
	let arr1: [Int] = [6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12];
	var size: Int = arr1.count;
	obj.maximumTriplet(arr1, size);
	// Define array elements
	let arr2: [Int] = [6, 9, 2, 1, 5, 7, 3, 8, 12, 4];
	size = arr2.count;
	obj.maximumTriplet(arr2, size);
}
main();

Output

   6   11   1   3   6   19   8   4   2   5   7   12
  Maximum Triplet Sum [ 19 + 12 + 11 ]:  42
   6   9   2   1   5   7   3   8   12   4
  Maximum Triplet Sum [ 12 + 9 + 8 ]:  29
/*
  Kotlin Program
  Find maximum triplet in array
*/
class Triplets
{
	// Function which is display array elements
	fun display(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	fun maximumTriplet(arr: Array<Int>, size: Int): Unit
	{
		if (size<3)
		{
			return;
		}
		// initial set the minimum value of variables
		var first: Int = Int.MIN_VALUE;
		var second: Int = Int.MIN_VALUE;
		var third: Int = Int.MIN_VALUE;
		var i: Int = 0;
		while (i<size)
		{
			// Check if whether array element i value is largest than of variable first
			if (arr[i]>first)
			{
				// modify the value of all three variables// like a swap operation of two variable
				third = second;
				second = first;
				//Assign a new big value
				first = arr[i];
			}
			else
			if (arr[i]>second)
			{
				// When get second largest element
				third = second;
				// Assign a new big value
				second = arr[i];
			}
			else
			if (arr[i]>third)
			{
				// When get third largest element
				third = arr[i];
			}
			i += 1;
		}
		this.display(arr, size);
		print("  Maximum Triplet Sum [" + first + " + " + second + " + " + third + "] : " + (first + second + third) + "\n");
	}
}
fun main(args: Array<String>): Unit
{
	var obj: Triplets = Triplets();
	// Define array elements
	var arr1: Array<Int> = arrayOf(6, 11, 1, 3, 6, 19, 8, 4, 2, 5, 7, 12);
	var size: Int = arr1.count();
	obj.maximumTriplet(arr1, size);
	// Define array elements
	var arr2: Array<Int> = arrayOf(6, 9, 2, 1, 5, 7, 3, 8, 12, 4);
	size = arr2.count();
	obj.maximumTriplet(arr2, size);
}

Output

  6  11  1  3  6  19  8  4  2  5  7  12
  Maximum Triplet Sum [19 + 12 + 11] : 42
  6  9  2  1  5  7  3  8  12  4
  Maximum Triplet Sum [12 + 9 + 8] : 29


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