Posted on by Kalkicode
Code Array

# Find all pairs whose sum exists in the array

Here given code implementation process.

``````// C Program
// Find pairs in array whose sum does not exist in 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 findPair(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]) == 0)
{
// When pair sum element is not present
printf(" (%d,%d) ", arr[i], arr[j]);
result++;
}
}
}
if (result == 0)
{
// When no result
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 , 3
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
findPair(arr, size);
return 0;
}``````

#### Output

`````` Given Elements
6  2  9  4  -2  3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````
``````/*
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;
}
// Find pairs in array whose sum does not exist in array
public void findPair(int[] arr, int size)
{
System.out.print("\n 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]) == false)
{
// When pair sum element is not present
System.out.print(" (" + arr[i] + "," + arr[j] + ") ");
result++;
}
}
}
if (result == 0)
{
// When no result
System.out.print("\n None \n");
}
else
{
System.out.print("\n Total Pairs " + result + "\n");
}
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr = {
6 , 2 , 9 , 4 , -2 , 3
};
// Get the size
int size = arr.length;
}
}``````

#### Output

`````` Given Elements
6 2 9 4 -2 3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````
``````// 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;
}
// Find pairs in array whose sum does not exist in array
void findPair(int arr[], int size)
{
cout << "\n 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]) == false)
{
// When pair sum element is not present
cout << " (" << arr[i] << "," << arr[j] << ") ";
result++;
}
}
}
if (result == 0)
{
// When no result
cout << "\n None \n";
}
else
{
cout << "\n Total Pairs " << result << "\n";
}
}
};
int main()
{
// Define array of integer elements
int arr[] = {
6 , 2 , 9 , 4 , -2 , 3
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}``````

#### Output

`````` Given Elements
6 2 9 4 -2 3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````
``````// 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;
}
// Find pairs in array whose sum does not exist in array
public void findPair(int[] arr, int size)
{
Console.Write("\n 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]) == false)
{
// When pair sum element is not present
Console.Write(" (" + arr[i] + "," + arr[j] + ") ");
result++;
}
}
}
if (result == 0)
{
// When no result
Console.Write("\n None \n");
}
else
{
Console.Write("\n Total Pairs " + result + "\n");
}
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr = {
6 , 2 , 9 , 4 , -2 , 3
};
// Get the size
int size = arr.Length;
}
}``````

#### Output

`````` Given Elements
6 2 9 4 -2 3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````
``````<?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;
}
// Find pairs in array whose sum does not exist in array
public	function findPair( & \$arr, \$size)
{
echo "\n 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]) == false)
{
// When pair sum element is not present
echo " (". \$arr[\$i] .",". \$arr[\$j] .") ";
\$result++;
}
}
}
if (\$result == 0)
{
// When no result
echo "\n None \n";
}
else
{
echo "\n Total Pairs ". \$result ."\n";
}
}
}

function main()
{
// Define array of integer elements
\$arr = array(6, 2, 9, 4, -2, 3);
// Get the size
\$size = count(\$arr);
}
main();``````

#### Output

`````` Given Elements
6 2 9 4 -2 3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````
``````/*
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;
}
// Find pairs in array whose sum does not exist in array
findPair(arr, size)
{
process.stdout.write("\n 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]) == false)
{
// When pair sum element is not present
process.stdout.write(" (" + arr[i] + "," + arr[j] + ") ");
result++;
}
}
}
if (result == 0)
{
// When no result
process.stdout.write("\n None \n");
}
else
{
process.stdout.write("\n Total Pairs " + result + "\n");
}
}
}

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

#### Output

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

#  Find pairs in array whose sum does not exist in array
def findPair(arr, size)
print("\n 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]) == false)
#  When pair sum element is not present
print(" (", arr[i] ,",", arr[j] ,") ")
result += 1
end

j += 1
end

i += 1
end

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

end

end

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

main()``````

#### Output

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

#  Find pairs in array whose sum does not exist in array
def findPair(self, arr, size) :
print("\n 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]) == False) :
#  When pair sum element is not present
print(" (", arr[i] ,",", arr[j] ,") ", end = "")
result += 1

j += 1

i += 1

if (result == 0) :
#  When no result
print("\n None ")
else :
print("\n Total Pairs ", result )

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

if __name__ == "__main__": main()``````

#### Output

`````` Given Elements
6  2  9  4  -2  3
Resultant pairs
( 6 , 2 )  ( 6 , 9 )  ( 6 , 4 )  ( 2 , 9 )  ( 2 , -2 )  ( 2 , 3 )  ( 9 , 4 )  ( 9 , -2 )  ( 9 , 3 )  ( 4 , 3 )  ( -2 , 3 )
Total Pairs  11``````
``````/*
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;
}
// Find pairs in array whose sum does not exist in array
def findPair(arr: Array[Int], size: Int): Unit = {
print("\n 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)) == false)
{
// When pair sum element is not present
print(" (" + arr(i) + "," + arr(j) + ") ");
result += 1;
}
j += 1;
}
i += 1;
}
if (result == 0)
{
// When no result
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, 3);
// Get the size
var size: Int = arr.length;
}
}``````

#### Output

`````` Given Elements
6 2 9 4 -2 3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````
``````/*
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;
}
// Find pairs in array whose sum does not exist in array
func findPair(_ arr: [Int], _ size: Int)
{
print("\n 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]) == false)
{
// When pair sum element is not present
print(" (", arr[i] ,",", arr[j] ,") ", terminator: "");
result += 1;
}
j += 1;
}
i += 1;
}
if (result == 0)
{
// When no result
print("\n None ");
}
else
{
print("\n Total Pairs ", result );
}
}
}
func main()
{
// Define array of integer elements
let arr: [Int] = [6, 2, 9, 4, -2, 3];
// Get the size
let size: Int = arr.count;
}
main();``````

#### Output

`````` Given Elements
6  2  9  4  -2  3
Resultant pairs
( 6 , 2 )  ( 6 , 9 )  ( 6 , 4 )  ( 2 , 9 )  ( 2 , -2 )  ( 2 , 3 )  ( 9 , 4 )  ( 9 , -2 )  ( 9 , 3 )  ( 4 , 3 )  ( -2 , 3 )
Total Pairs  11``````
``````/*
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;
}
// Find pairs in array whose sum does not exist in array
fun findPair(arr: Array<Int> , size: Int): Unit
{
print("\n 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]) == false)
{
// When pair sum element is not present
print(" (" + arr[i] + "," + arr[j] + ") ");
result += 1;
}
j += 1;
}
i += 1;
}
if (result == 0)
{
// When no result
print("\n None \n");
}
else
{
print("\n Total Pairs " + result + "\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Define array of integer elements
var arr: Array < Int > = arrayOf(6, 2, 9, 4, -2, 3);
// Get the size
var size: Int = arr.count();
}``````

#### Output

`````` Given Elements
6 2 9 4 -2 3
Resultant pairs
(6,2)  (6,9)  (6,4)  (2,9)  (2,-2)  (2,3)  (9,4)  (9,-2)  (9,3)  (4,3)  (-2,3)
Total Pairs 11``````

## 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.

Categories
Relative Post