Find smallest pair sum in an array

Here given code implementation process.

// C Program 
// Find smallest pair sum in an array
#include <stdio.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");
}
//Find the pair of smallest sum in given array
void smallest_pair(int arr[], int size)
{
	if (size < 2)
	{
		//When array contains less than 2 elements
		return;
	}
	//First pair sum
	int sum = arr[0] + arr[1];
	//Get the first pair indexes
	int first = 0;
	int second = 1;
	for (int i = 0; i < size; ++i)
	{
		for (int j = i + 1; j < size; ++j)
		{
			if (arr[i] + arr[j] < sum)
			{
				//When get a new smallest  element pair
				//Get the sum of new small pair
				sum = arr[i] + arr[j];
				//Get the location of new smallest pair
				first = i;
				second = j;
			}
		}
	}
	printf("\n Array elements : ");
	display(arr, size);
	//Display result
	printf("\n Smallest pair [%d, %d] : %d\n", arr[first], arr[second], sum);
}
int main()
{
	int a1[] = {
		10 , 15 , -7 , 31 , 33
	};
	//Get the size of array
	int size = sizeof(a1) / sizeof(a1[0]);
	smallest_pair(a1, size);
	int a2[] = {
		9 , 6 , 1 , 5 , 8 , 3
	};
	//Get the size of array
	size = sizeof(a2) / sizeof(a2[0]);
	smallest_pair(a2, size);
	return 0;
}

Output

 Array elements : 10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements : 9 6 1 5 8 3

 Smallest pair [1, 3] : 4
// Java Program
// Find median of two sorted arrays
class MyArray
{
	//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");
	}
	//Find the pair of smallest sum in given array
	public void smallest_pair(int[] arr, int size)
	{
		if (size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		int sum = arr[0] + arr[1];
		//Get the first pair indexes
		int first = 0;
		int second = 1;
		for (int i = 0; i < size; ++i)
		{
			for (int j = i + 1; j < size; ++j)
			{
				if (arr[i] + arr[j] < sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					sum = arr[i] + arr[j];
					//Get the location of new smallest pair
					first = i;
					second = j;
				}
			}
		}
		System.out.print("\n Array elements : ");
		display(arr, size);
		System.out.print("\n Smallest pair [" + arr[first] + ", " + arr[second] + "] : " + sum + "\n");
	}
	public static void main(String[] args)
	{
		MyArray obj = new MyArray();
		int[] a1 = {
			10,
			15,
			-7,
			31,
			33
		};
		//Get the size of array
		int size = a1.length;
		obj.smallest_pair(a1, size);
		int[] a2 = {
			9,
			6,
			1,
			5,
			8,
			3
		};
		//Get the size of array
		size = a2.length;
		obj.smallest_pair(a2, size);
	}
}

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Find median of two sorted arrays
class MyArray
{
	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";
		}
	//Find the pair of smallest sum in given array
	void smallest_pair(int arr[], int size)
	{
		if (size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		int sum = arr[0] + arr[1];
		//Get the first pair indexes
		int first = 0;
		int second = 1;
		for (int i = 0; i < size; ++i)
		{
			for (int j = i + 1; j < size; ++j)
			{
				if (arr[i] + arr[j] < sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					sum = arr[i] + arr[j];
					//Get the location of new smallest pair
					first = i;
					second = j;
				}
			}
		}
		cout << "\n Array elements : ";
		this->display(arr, size);
		cout << "\n Smallest pair [" << arr[first] << ", " << arr[second] << "] : " << sum << "\n";
	}
};
int main()
{
	MyArray obj = MyArray();
	int a1[] = {
		10 , 15 , -7 , 31 , 33
	};
	//Get the size of array
	int size = sizeof(a1) / sizeof(a1[0]);
	obj.smallest_pair(a1, size);
	int a2[] = {
		9 , 6 , 1 , 5 , 8 , 3
	};
	//Get the size of array
	size = sizeof(a2) / sizeof(a2[0]);
	obj.smallest_pair(a2, size);
	return 0;
}

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
//Include namespace system
using System;

// C# Program
// Find median of two sorted arrays
class MyArray
{
	//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");
	}
	//Find the pair of smallest sum in given array
	public void smallest_pair(int[] arr, int size)
	{
		if (size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		int sum = arr[0] + arr[1];
		//Get the first pair indexes
		int first = 0;
		int second = 1;
		for (int i = 0; i < size; ++i)
		{
			for (int j = i + 1; j < size; ++j)
			{
				if (arr[i] + arr[j] < sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					sum = arr[i] + arr[j];
					//Get the location of new smallest pair
					first = i;
					second = j;
				}
			}
		}
		Console.Write("\n Array elements : ");
		display(arr, size);
		Console.Write("\n Smallest pair [" + arr[first] + ", " + arr[second] + "] : " + sum + "\n");
	}
	public static void Main(String[] args)
	{
		MyArray obj = new MyArray();
		int[] a1 = {
			10 , 15 , -7 , 31 , 33
		};
		//Get the size of array
		int size = a1.Length;
		obj.smallest_pair(a1, size);
		int[] a2 = {
			9 , 6 , 1 , 5 , 8 , 3
		};
		//Get the size of array
		size = a2.Length;
		obj.smallest_pair(a2, size);
	}
}

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
<?php
// Php Program
// Find median of two sorted arrays
class MyArray
{
	//Function which is display array elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	//Find the pair of smallest sum in given array
	public	function smallest_pair( & $arr, $size)
	{
		if ($size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		$sum = $arr[0] + $arr[1];
		//Get the first pair indexes
		$first = 0;
		$second = 1;
		for ($i = 0; $i < $size; ++$i)
		{
			for ($j = $i + 1; $j < $size; ++$j)
			{
				if ($arr[$i] + $arr[$j] < $sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					$sum = $arr[$i] + $arr[$j];
					//Get the location of new smallest pair
					$first = $i;
					$second = $j;
				}
			}
		}
		echo "\n Array elements : ";
		$this->display($arr, $size);
		echo "\n Smallest pair [". $arr[$first] .", ". $arr[$second] ."] : ". $sum ."\n";
	}
}

function main()
{
	$obj = new MyArray();
	$a1 = array(10, 15, -7, 31, 33);
	//Get the size of array
	$size = count($a1);
	$obj->smallest_pair($a1, $size);
	$a2 = array(9, 6, 1, 5, 8, 3);
	//Get the size of array
	$size = count($a2);
	$obj->smallest_pair($a2, $size);
}
main();

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
// Node Js Program
// Find median of two sorted arrays
class MyArray
{
	//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");
	}
	//Find the pair of smallest sum in given array
	smallest_pair(arr, size)
	{
		if (size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		var sum = arr[0] + arr[1];
		//Get the first pair indexes
		var first = 0;
		var second = 1;
		for (var i = 0; i < size; ++i)
		{
			for (var j = i + 1; j < size; ++j)
			{
				if (arr[i] + arr[j] < sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					sum = arr[i] + arr[j];
					//Get the location of new smallest pair
					first = i;
					second = j;
				}
			}
		}
		process.stdout.write("\n Array elements : ");
		this.display(arr, size);
		process.stdout.write("\n Smallest pair [" + arr[first] + ", " + arr[second] + "] : " + sum + "\n");
	}
}

function main()
{
	var obj = new MyArray();
	var a1 = [10, 15, -7, 31, 33];
	//Get the size of array
	var size = a1.length;
	obj.smallest_pair(a1, size);
	var a2 = [9, 6, 1, 5, 8, 3];
	//Get the size of array
	size = a2.length;
	obj.smallest_pair(a2, size);
}
main();

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
#  Python 3 Program
#  Find median of two sorted arrays
class MyArray :
	# Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print("\n", end = "")
	
	# Find the pair of smallest sum in given array
	def smallest_pair(self, arr, size) :
		if (size < 2) :
			# When array contains less than 2 elements
			return
		
		# First pair sum
		sum = arr[0] + arr[1]
		# Get the first pair indexes
		first = 0
		second = 1
		i = 0
		while (i < size) :
			j = i + 1
			while (j < size) :
				if (arr[i] + arr[j] < sum) :
					# When get a new smallest  element pair
					# Get the sum of new small pair
					sum = arr[i] + arr[j]
					# Get the location of new smallest pair
					first = i
					second = j
				
				j += 1
			
			i += 1
		
		print("\n Array elements : ", end = "")
		self.display(arr, size)
		print("\n Smallest pair [", arr[first] ,", ", arr[second] ,"] : ", sum ,"\n", end = "")
	

def main() :
	obj = MyArray()
	a1 = [10, 15, -7, 31, 33]
	# Get the size of array
	size = len(a1)
	obj.smallest_pair(a1, size)
	a2 = [9, 6, 1, 5, 8, 3]
	# Get the size of array
	size = len(a2)
	obj.smallest_pair(a2, size)

if __name__ == "__main__": main()

Output

 Array elements :   10  15  -7  31  33

 Smallest pair [ 10 ,  -7 ] :  3

 Array elements :   9  6  1  5  8  3

 Smallest pair [ 1 ,  3 ] :  4
#  Ruby Program
#  Find median of two sorted arrays
class MyArray

	# Function which is display array elements
	def display(arr, size)
	
		i = 0
		while (i < size)
		
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
	# Find the pair of smallest sum in given array
	def smallest_pair(arr, size)
	
		if (size < 2)
		
			# When array contains less than 2 elements
			return
		end
		# First pair sum
		sum = arr[0] + arr[1]
		# Get the first pair indexes
		first = 0
		second = 1
		i = 0
		while (i < size)
		
			j = i + 1
			while (j < size)
			
				if (arr[i] + arr[j] < sum)
				
					# When get a new smallest  element pair
					# Get the sum of new small pair
					sum = arr[i] + arr[j]
					# Get the location of new smallest pair
					first = i
					second = j
				end
				j += 1
			end
			i += 1
		end
		print("\n Array elements : ")
		self.display(arr, size)
		print("\n Smallest pair [", arr[first] ,", ", arr[second] ,"] : ", sum ,"\n")
	end
end
def main()

	obj = MyArray.new()
	a1 = [10, 15, -7, 31, 33]
	# Get the size of array
	size = a1.length
	obj.smallest_pair(a1, size)
	a2 = [9, 6, 1, 5, 8, 3]
	# Get the size of array
	size = a2.length
	obj.smallest_pair(a2, size)
end
main()

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
// Scala Program
// Find median of two sorted arrays
class MyArray
{
	//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");
	}
	//Find the pair of smallest sum in given array
	def smallest_pair(arr: Array[Int], size: Int): Unit = {
		if (size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		var sum: Int = arr(0) + arr(1);
		//Get the first pair indexes
		var first: Int = 0;
		var second: Int = 1;
		var i: Int = 0;
		while (i < size)
		{
			var j: Int = i + 1;
			while (j < size)
			{
				if (arr(i) + arr(j) < sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					sum = arr(i) + arr(j);
					//Get the location of new smallest pair
					first = i;
					second = j;
				}
				j += 1;
			}
			i += 1;
		}
		print("\n Array elements : ");
		display(arr, size);
		print("\n Smallest pair [" + arr(first) + ", " + arr(second) + "] : " + sum + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyArray = new MyArray();
		var a1: Array[Int] = Array(10, 15, -7, 31, 33);
		//Get the size of array
		var size: Int = a1.length;
		obj.smallest_pair(a1, size);
		var a2: Array[Int] = Array(9, 6, 1, 5, 8, 3);
		//Get the size of array
		size = a2.length;
		obj.smallest_pair(a2, size);
	}
}

Output

 Array elements :  10 15 -7 31 33

 Smallest pair [10, -7] : 3

 Array elements :  9 6 1 5 8 3

 Smallest pair [1, 3] : 4
// Swift Program
// Find median of two sorted arrays
class MyArray
{
	//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("\n", terminator: "");
	}
	//Find the pair of smallest sum in given array
	func smallest_pair(_ arr: [Int], _ size: Int)
	{
		if (size < 2)
		{
			//When array contains less than 2 elements
			return;
		}
		//First pair sum
		var sum: Int = arr[0] + arr[1];
		//Get the first pair indexes
		var first: Int = 0;
		var second: Int = 1;
		var i: Int = 0;
		while (i < size)
		{
			var j: Int = i + 1;
			while (j < size)
			{
				if (arr[i] + arr[j] < sum)
				{
					//When get a new smallest  element pair
					//Get the sum of new small pair
					sum = arr[i] + arr[j];
					//Get the location of new smallest pair
					first = i;
					second = j;
				}
				j += 1;
			}
			i += 1;
		}
		print("\n Array elements : ", terminator: "");
		self.display(arr, size);
		print("\n Smallest pair [", arr[first] ,", ", arr[second] ,"] : ", sum ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyArray = MyArray();
	let a1: [Int] = [10, 15, -7, 31, 33];
	//Get the size of array
	var size: Int = a1.count;
	obj.smallest_pair(a1, size);
	let a2: [Int] = [9, 6, 1, 5, 8, 3];
	//Get the size of array
	size = a2.count;
	obj.smallest_pair(a2, size);
}
main();

Output

 Array elements :   10  15  -7  31  33

 Smallest pair [ 10 ,  -7 ] :  3

 Array elements :   9  6  1  5  8  3

 Smallest pair [ 1 ,  3 ] :  4


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