Find all pairs whose sum exists in the array

Here given code implementation process.

// C Program 
// Find all pairs whose sum exists in the array
#include <stdio.h>
 //Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// Check that given element exists in array or not
int findElement(int arr[], int size, int element)
{
	// Boundary location
	int i = 0;
	int j = size - 1;
	// Find element	
	while (i <= j)
	{
		if (arr[i] == element || arr[j] == element)
		{
			// When element exists
			return 1;
		}
		// modified element location
		i++;
		j--;
	}
	// When element is not exist
	return 0;
}
void findPairSum(int arr[], int size)
{
	printf("\n Given Elements \n");
	// print given array
	printArray(arr, size);
	// Loop controlling variables
	int i = 0;
	int j = 0;
	// result counter
	int result = 0;
	printf(" Resultant pairs \n ");
	// Execute loop through by size
	for (i = 0; i < size; ++i)
	{
		for (j = i + 1; j < size; ++j)
		{
			if (findElement(arr, size, arr[i] + arr[j]))
			{
				// When sum exists in array
				printf(" (%d,%d) ", arr[i], arr[j]);
				result++;
			}
		}
	}
	if (result == 0)
	{
		// When no pair sum exists
		printf("\n None \n");
	}
	else
	{
		printf("\n Total Pairs %d\n", result);
	}
}
int main(int argc, char
	const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		6 , 2 , 9 , 4 , -2 , 10
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	findPairSum(arr, size);
	return 0;
}

Output

 Given Elements
  6  2  9  4  -2  10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
/*
  Java Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	// Check that given element exists in array or not
	public boolean findElement(int[] arr, int size, int element)
	{
		// Boundary location
		int i = 0;
		int j = size - 1;
		// Find element 
		while (i <= j)
		{
			if (arr[i] == element || arr[j] == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i++;
			j--;
		}
		// When element is not exist
		return false;
	}
	public void findPairSum(int[] arr, int size)
	{
		System.out.print("  Given Elements \n");
		// print given array
		printArray(arr, size);
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// result counter
		int result = 0;
		System.out.print(" Resultant pairs \n ");
		// Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			for (j = i + 1; j < size; ++j)
			{
				if (findElement(arr, size, arr[i] + arr[j]))
				{
					// When sum exists in array
					System.out.print(" (" + arr[i] + "," + arr[j] + ") ");
					result++;
				}
			}
		}
		if (result == 0)
		{
			// When no pair sum exists
			System.out.print("\n None \n");
		}
      	else
        {
        	
			System.out.print("\n Total Pairs "+result+"\n");
        }
	}
	public static void main(String[] args)
	{
		Pairs task = new Pairs();
		// Define array of integer elements
		int[] arr = {
			6 , 2 , 9 , 4 , -2 , 10
		};
		// Get the size
		int size = arr.length;
		task.findPairSum(arr, size);
	}
}

Output

  Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	// Check that given element exists in array or not
	bool findElement(int arr[], int size, int element)
	{
		// When element is not exist
		// Boundary location
		int i = 0;
		int j = size - 1;
		// Find element
		while (i <= j)
		{
			if (arr[i] == element || arr[j] == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i++;
			j--;
		}
		return false;
	}
	void findPairSum(int arr[], int size)
	{
		cout << "  Given Elements \n";
		// print given array
		this->printArray(arr, size);
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// result counter
		int result = 0;
		cout << " Resultant pairs \n ";
		// Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			for (j = i + 1; j < size; ++j)
			{
				if (this->findElement(arr, size, arr[i] + arr[j]))
				{
					// When sum exists in array
					cout << " (" << arr[i] << "," << arr[j] << ") ";
					result++;
				}
			}
		}
		if (result == 0)
		{
			// When no pair sum exists
			cout << "\n None \n";
		}
		else
		{
			cout << "\n Total Pairs " << result << "\n";
		}
	}
};
int main()
{
	Pairs task = Pairs();
	// Define array of integer elements
	int arr[] = {
		6 , 2 , 9 , 4 , -2 , 10
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	task.findPairSum(arr, size);
	return 0;
}

Output

  Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
// Include namespace system
using System;
/*
  C# Program for
  Find all pairs whose sum exists in the array
*/
public class Pairs
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	// Check that given element exists in array or not
	public Boolean findElement(int[] arr, int size, int element)
	{
		// When element is not exist
		// Boundary location
		int i = 0;
		int j = size - 1;
		// Find element
		while (i <= j)
		{
			if (arr[i] == element || arr[j] == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i++;
			j--;
		}
		return false;
	}
	public void findPairSum(int[] arr, int size)
	{
		Console.Write(" Given Elements \n");
		// print given array
		printArray(arr, size);
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// result counter
		int result = 0;
		Console.Write(" Resultant pairs \n ");
		// Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			for (j = i + 1; j < size; ++j)
			{
				if (findElement(arr, size, arr[i] + arr[j]))
				{
					// When sum exists in array
					Console.Write(" (" + arr[i] + "," + arr[j] + ") ");
					result++;
				}
			}
		}
		if (result == 0)
		{
			// When no pair sum exists
			Console.Write("\n None \n");
		}
		else
		{
			Console.Write("\n Total Pairs " + result + "\n");
		}
	}
	public static void Main(String[] args)
	{
		Pairs task = new Pairs();
		// Define array of integer elements
		int[] arr = {
			6 , 2 , 9 , 4 , -2 , 10
		};
		// Get the size
		int size = arr.Length;
		task.findPairSum(arr, size);
	}
}

Output

 Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
<?php
/*
  Php Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	//Display elements of given array
	public	function printArray( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	// Check that given element exists in array or not
	public	function findElement( $arr, $size, $element)
	{
		// When element is not exist
		// Boundary location
		$i = 0;
		$j = $size - 1;
		// Find element
		while ($i <= $j)
		{
			if ($arr[$i] == $element || $arr[$j] == $element)
			{
				// When element exists
				return true;
			}
			// modified element location
			$i++;
			$j--;
		}
		return false;
	}
	public	function findPairSum( & $arr, $size)
	{
		echo " Given Elements \n";
		// print given array
		$this->printArray($arr, $size);
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// result counter
		$result = 0;
		echo " Resultant pairs \n ";
		// Execute loop through by size
		for ($i = 0; $i < $size; ++$i)
		{
			for ($j = $i + 1; $j < $size; ++$j)
			{
				if ($this->findElement($arr, $size, $arr[$i] + $arr[$j]))
				{
					// When sum exists in array
					echo " (". $arr[$i] .",". $arr[$j] .") ";
					$result++;
				}
			}
		}
		if ($result == 0)
		{
			// When no pair sum exists
			echo "\n None \n";
		}
		else
		{
			echo "\n Total Pairs ". $result ."\n";
		}
	}
}

function main()
{
	$task = new Pairs();
	// Define array of integer elements
	$arr = array(6, 2, 9, 4, -2, 10);
	// Get the size
	$size = count($arr);
	$task->findPairSum($arr, $size);
}
main();

Output

 Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
/*
  Node Js Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Check that given element exists in array or not
	findElement(arr, size, element)
	{
		// When element is not exist
		// Boundary location
		var i = 0;
		var j = size - 1;
		// Find element
		while (i <= j)
		{
			if (arr[i] == element || arr[j] == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i++;
			j--;
		}
		return false;
	}
	findPairSum(arr, size)
	{
		process.stdout.write(" Given Elements \n");
		// print given array
		this.printArray(arr, size);
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// result counter
		var result = 0;
		process.stdout.write(" Resultant pairs \n ");
		// Execute loop through by size
		for (i = 0; i < size; ++i)
		{
			for (j = i + 1; j < size; ++j)
			{
				if (this.findElement(arr, size, arr[i] + arr[j]))
				{
					// When sum exists in array
					process.stdout.write(" (" + arr[i] + "," + arr[j] + ") ");
					result++;
				}
			}
		}
		if (result == 0)
		{
			// When no pair sum exists
			process.stdout.write("\n None \n");
		}
		else
		{
			process.stdout.write("\n Total Pairs " + result + "\n");
		}
	}
}

function main()
{
	var task = new Pairs();
	// Define array of integer elements
	var arr = [6, 2, 9, 4, -2, 10];
	// Get the size
	var size = arr.length;
	task.findPairSum(arr, size);
}
main();

Output

 Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
#   Python 3 Program for
#   Find all pairs whose sum exists in the array

class Pairs :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Check that given element exists in array or not
	def findElement(self, arr, size, element) :
		#  When element is not exist
		#  Boundary location
		i = 0
		j = size - 1
		#  Find element
		while (i <= j) :
			if (arr[i] == element or arr[j] == element) :
				#  When element exists
				return True
			
			#  modified element location
			i += 1
			j -= 1
		
		return False
	
	def findPairSum(self, arr, size) :
		print(" Given Elements ")
		#  print given array
		self.printArray(arr, size)
		#  Loop controlling variables
		i = 0
		j = 0
		#  result counter
		result = 0
		print(" Resultant pairs \n ", end = "")
		#  Execute loop through by size
		while (i < size) :
			j = i + 1
			while (j < size) :
				if (self.findElement(arr, size, arr[i] + arr[j])) :
					#  When sum exists in array
					print(" (", arr[i] ,",", arr[j] ,") ", end = "")
					result += 1
				
				j += 1
			
			i += 1
		
		if (result == 0) :
			#  When no pair sum exists
			print("\n None ")
		else :
			print("\n Total Pairs ", result )
		
	

def main() :
	task = Pairs()
	#  Define array of integer elements
	arr = [6, 2, 9, 4, -2, 10]
	#  Get the size
	size = len(arr)
	task.findPairSum(arr, size)

if __name__ == "__main__": main()

Output

 Given Elements
  6  2  9  4  -2  10
 Resultant pairs
  ( 6 , 4 )  ( 6 , -2 )  ( 2 , 4 )  ( 4 , -2 )
 Total Pairs  4
#   Ruby Program for
#   Find all pairs whose sum exists in the array

class Pairs 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Check that given element exists in array or not
	def findElement(arr, size, element) 
		#  When element is not exist
		#  Boundary location
		i = 0
		j = size - 1
		#  Find element
		while (i <= j) 
			if (arr[i] == element || arr[j] == element) 
				#  When element exists
				return true
			end

			#  modified element location
			i += 1
			j -= 1
		end

		return false
	end

	def findPairSum(arr, size) 
		print(" Given Elements \n")
		#  print given array
		self.printArray(arr, size)
		#  Loop controlling variables
		i = 0
		j = 0
		#  result counter
		result = 0
		print(" Resultant pairs \n ")
		#  Execute loop through by size
		while (i < size) 
			j = i + 1
			while (j < size) 
				if (self.findElement(arr, size, arr[i] + arr[j])) 
					#  When sum exists in array
					print(" (", arr[i] ,",", arr[j] ,") ")
					result += 1
				end

				j += 1
			end

			i += 1
		end

		if (result == 0) 
			#  When no pair sum exists
			print("\n None \n")
		else 
			print("\n Total Pairs ", result ,"\n")
		end

	end

end

def main() 
	task = Pairs.new()
	#  Define array of integer elements
	arr = [6, 2, 9, 4, -2, 10]
	#  Get the size
	size = arr.length
	task.findPairSum(arr, size)
end

main()

Output

 Given Elements 
 6 2 9 4 -2 10
 Resultant pairs 
  (6,4)  (6,-2)  (2,4)  (4,-2) 
 Total Pairs 4
/*
  Scala Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Check that given element exists in array or not
	def findElement(arr: Array[Int], size: Int, element: Int): Boolean = {
		// When element is not exist
		// Boundary location
		var i: Int = 0;
		var j: Int = size - 1;
		// Find element
		while (i <= j)
		{
			if (arr(i) == element || arr(j) == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i += 1;
			j -= 1;
		}
		return false;
	}
	def findPairSum(arr: Array[Int], size: Int): Unit = {
		print(" Given Elements \n");
		// print given array
		this.printArray(arr, size);
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// result counter
		var result: Int = 0;
		print(" Resultant pairs \n ");
		// Execute loop through by size
		while (i < size)
		{
			j = i + 1;
			while (j < size)
			{
				if (this.findElement(arr, size, arr(i) + arr(j)))
				{
					// When sum exists in array
					print(" (" + arr(i) + "," + arr(j) + ") ");
					result += 1;
				}
				j += 1;
			}
			i += 1;
		}
		if (result == 0)
		{
			// When no pair sum exists
			print("\n None \n");
		}
		else
		{
			print("\n Total Pairs " + result + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pairs = new Pairs();
		// Define array of integer elements
		var arr: Array[Int] = Array(6, 2, 9, 4, -2, 10);
		// Get the size
		var size: Int = arr.length;
		task.findPairSum(arr, size);
	}
}

Output

 Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 4
/*
  Swift 4 Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Check that given element exists in array or not
	func findElement(_ arr: [Int], _ size: Int, _ element: Int)->Bool
	{
		// When element is not exist
		// Boundary location
		var i: Int = 0;
		var j: Int = size - 1;
		// Find element
		while (i <= j)
		{
			if (arr[i] == element || arr[j] == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i += 1;
			j -= 1;
		}
		return false;
	}
	func findPairSum(_ arr: [Int], _ size: Int)
	{
		print(" Given Elements ");
		// print given array
		self.printArray(arr, size);
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// result counter
		var result: Int = 0;
		print(" Resultant pairs \n ", terminator: "");
		// Execute loop through by size
		while (i < size)
		{
			j = i + 1;
			while (j < size)
			{
				if (self.findElement(arr, size, arr[i] + arr[j]))
				{
					// When sum exists in array
					print(" (", arr[i] ,",", arr[j] ,") ", terminator: "");
					result += 1;
				}
				j += 1;
			}
			i += 1;
		}
		if (result == 0)
		{
			// When no pair sum exists
			print("\n None ");
		}
		else
		{
			print("\n Total Pairs ", result );
		}
	}
}
func main()
{
	let task: Pairs = Pairs();
	// Define array of integer elements
	let arr: [Int] = [6, 2, 9, 4, -2, 10];
	// Get the size
	let size: Int = arr.count;
	task.findPairSum(arr, size);
}
main();

Output

 Given Elements
  6  2  9  4  -2  10
 Resultant pairs
  ( 6 , 4 )  ( 6 , -2 )  ( 2 , 4 )  ( 4 , -2 )
 Total Pairs  4
/*
  Kotlin Program for
  Find all pairs whose sum exists in the array
*/
class Pairs
{
	//Display elements of given array
	fun printArray(arr: Array<Int> , size: Int): Unit
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Check that given element exists in array or not
	fun findElement(arr: Array<Int> , size: Int, element: Int): Boolean
	{
		// When element is not exist
		// Boundary location
		var i: Int = 0;
		var j: Int = size - 1;
		// Find element
		while (i <= j)
		{
			if (arr[i] == element || arr[j] == element)
			{
				// When element exists
				return true;
			}
			// modified element location
			i += 1;
			j -= 1;
		}
		return false;
	}
	fun findPairSum(arr: Array<Int> , size: Int): Unit
	{
		print(" Given Elements \n");
		// print given array
		this.printArray(arr, size);
		// Loop controlling variables
		var i: Int = 0;
		var j: Int ;
		// result counter
		var result: Int = 0;
		print(" Resultant pairs \n ");
		// Execute loop through by size
		while (i < size)
		{
			j = i + 1;
			while (j < size)
			{
				if (this.findElement(arr, size, arr[i] + arr[j]))
				{
					// When sum exists in array
					print(" (" + arr[i] + "," + arr[j] + ") ");
					result += 1;
				}
				j += 1;
			}
			i += 1;
		}
		if (result == 0)
		{
			// When no pair sum exists
			print("\n None \n");
		}
		else
		{
			print("\n Total Pairs " + result + "\n");
		}
	}
}
fun main(args: Array<String> ): Unit
{
	var task: Pairs = Pairs();
	// Define array of integer elements
	var arr: Array<Int> = arrayOf(6, 2, 9, 4, -2, 10);
	// Get the size
	var size: Int = arr.count();
	task.findPairSum(arr, size);
}

Output

 Given Elements
 6 2 9 4 -2 10
 Resultant pairs
  (6,4)  (6,-2)  (2,4)  (4,-2)
 Total Pairs 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