Find pairs with given sum in an array
Here given code implementation process.
/*
Java Program
Find pairs with given sum in an array
*/
import java.util.HashMap;
public class Counting
{
// Display given matrix element
public void display(int[] arr, int n)
{
// iterate the loop through by n
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
public void countPair(int[] data, int n, int sum)
{
HashMap <Integer, Integer> record = new HashMap <Integer, Integer> ();
// Define some auxiliary variable
int count = 0;
int i = 0;
// Display given array element
System.out.print(" Given Array : ");
display(data, n);
System.out.print(" Given Sum : " + sum);
// Execute loop through by array size
// Count element frequency
for (i = 0; i < n; ++i)
{
if (record.containsKey(data[i]))
{
record.put(data[i], record.get(data[i]) + 1);
}
else
{
record.put(data[i], 1);
}
}
for (i = 0; i < n; ++i)
{
if (record.containsKey(sum - data[i]))
{
// Count pairs by frequency
count += record.get(sum - data[i]);
}
if (data[i] == sum - data[i])
{
count--;
}
}
if (count > 0)
{
count = count / 2;
}
// Display calculated result
System.out.print("\n Total pair : " + count);
}
public static void main(String[] arg)
{
Counting task = new Counting();
// Define the array of integer element
int[] data = {
5 , 2 , -2 , 4 , -1 , 6 , 1 , -4 , 2
};
// Get number of element
int n = data.length;
// Resultant sum
int sum = 3;
// Test
task.countPair(data, n, sum);
}
}
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
class Counting
{
public:
// Display given matrix element
void display(int arr[], int n)
{
// iterate the loop through by n
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
void countPair(int data[], int n, int sum)
{
unordered_map < int, int > record;
// Define some auxiliary variable
int count = 0;
int i = 0;
// Display given array element
cout << " Given Array : ";
this->display(data, n);
cout << " Given Sum : " << sum;
// Execute loop through by array size
// Count element frequency
for (i = 0; i < n; ++i)
{
if (record.find(data[i]) != record.end())
{
record[data[i]] = record[data[i]] + 1;
}
else
{
record[data[i]] = 1;
}
}
// Count resultant pair
for (i = 0; i < n; ++i)
{
if (record.find(sum - data[i]) != record.end())
{
// Count pairs by frequency
count += record[sum - data[i]];
}
if (data[i] == sum - data[i])
{
count--;
}
}
if (count > 0)
{
count = count / 2;
}
// Display calculated result
cout << "\n Total pair : " << count;
}
};
int main()
{
Counting task = Counting();
// Define the array of integer element
int data[] = {
5 , 2 , -2 , 4 , -1 , 6 , 1 , -4 , 2
};
// Get number of element
int n = sizeof(data) / sizeof(data[0]);
// Resultant sum
int sum = 3;
// Test
task.countPair(data, n, sum);
return 0;
}
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Find pairs with given sum in an array
*/
public class Counting
{
// Display given matrix element
public void display(int[] arr, int n)
{
// iterate the loop through by n
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
public void countPair(int[] data, int n, int sum)
{
Dictionary <int, int> record = new Dictionary < int, int > ();
// Define some auxiliary variable
int count = 0;
int i = 0;
// Display given array element
Console.Write(" Given Array : ");
display(data, n);
Console.Write(" Given Sum : " + sum);
// Execute loop through by array size
// Count element frequency
for (i = 0; i < n; ++i)
{
if (record.ContainsKey(data[i]))
{
record[data[i]] = record[data[i]] + 1;
}
else
{
record.Add(data[i], 1);
}
}
// Count resultant pair
for (i = 0; i < n; ++i)
{
if (record.ContainsKey(sum - data[i]))
{
// Count pairs by frequency
count += record[sum - data[i]];
}
if (data[i] == sum - data[i])
{
count--;
}
}
if (count > 0)
{
count = count / 2;
}
// Display calculated result
Console.Write("\n Total pair : " + count);
}
public static void Main(String[] arg)
{
Counting task = new Counting();
// Define the array of integer element
int[] data = {
5 , 2 , -2 , 4 , -1 , 6 , 1 , -4 , 2
};
// Get number of element
int n = data.Length;
// Resultant sum
int sum = 3;
// Test
task.countPair(data, n, sum);
}
}
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
/*
Node Js Program
Find pairs with given sum in an array
*/
class Counting
{
// Display given matrix element
display(arr, n)
{
// iterate the loop through by n
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
countPair(data, n, sum)
{
var record = new Map();
// Define some auxiliary variable
var count = 0;
var i = 0;
// Display given array element
process.stdout.write(" Given Array : ");
this.display(data, n);
process.stdout.write(" Given Sum : " + sum);
// Execute loop through by array size
// Count element frequency
for (i = 0; i < n; ++i)
{
if (record.has(data[i]))
{
record.set(data[i], record.get(data[i]) + 1);
}
else
{
record.set(data[i], 1);
}
}
// Count resultant pair
for (i = 0; i < n; ++i)
{
if (record.has(sum - data[i]))
{
// Count pairs by frequency
count += record.get(sum - data[i]);
}
if (data[i] == sum - data[i])
{
count--;
}
}
if (count > 0)
{
count = parseInt(count / 2);
}
// Display calculated result
process.stdout.write("\n Total pair : " + count);
}
}
function main()
{
var task = new Counting();
// Define the array of integer element
var data = [5, 2, -2, 4, -1, 6, 1, -4, 2];
// Get number of element
var n = data.length;
// Resultant sum
var sum = 3;
// Test
task.countPair(data, n, sum);
}
main();
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
# Python 3 Program
# Find pairs with given sum in an array
class Counting :
# Display given matrix element
def display(self, arr, n) :
i = 0
# iterate the loop through by n
while (i < n) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
def countPair(self, data, n, sum) :
record = dict()
# Define some auxiliary variable
count = 0
i = 0
# Display given list element
print(" Given Array : ", end = "")
self.display(data, n)
print(" Given Sum : ", sum, end = "")
# Execute loop through by list size
# Count element frequency
while (i < n) :
if (data[i] in record.keys()) :
record[data[i]] = record.get(data[i]) + 1
else :
record[data[i]] = 1
i += 1
i = 0
# Count resultant pair
while (i < n) :
if (sum - data[i] in record.keys()) :
# Count pairs by frequency
count += record.get(sum - data[i])
if (data[i] == sum - data[i]) :
count -= 1
i += 1
if (count > 0) :
count = int(count / 2)
# Display calculated result
print("\n Total pair : ", count, end = "")
def main() :
task = Counting()
# Define the list of integer element
data = [5, 2, -2, 4, -1, 6, 1, -4, 2]
# Get number of element
n = len(data)
# Resultant sum
sum = 3
# Test
task.countPair(data, n, sum)
if __name__ == "__main__": main()
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
# Ruby Program
# Find pairs with given sum in an array
class Counting
# Display given matrix element
def display(arr, n)
i = 0
# iterate the loop through by n
while (i < n)
print(" ", arr[i])
i += 1
end
print("\n")
end
def countPair(data, n, sum)
record = Hash.new
# Define some auxiliary variable
count = 0
i = 0
# Display given array element
print(" Given Array : ")
self.display(data, n)
print(" Given Sum : ", sum)
# Execute loop through by array size
# Count element frequency
while (i < n)
if (record.key?(data[i]))
record[data[i]] = record[data[i]] + 1
else
record[data[i]] = 1
end
i += 1
end
i = 0
# Count resultant pair
while (i < n)
if (record.key?(sum - data[i]))
# Count pairs by frequency
count += record[sum - data[i]]
end
if (data[i] == sum - data[i])
count -= 1
end
i += 1
end
if (count > 0)
count = count / 2
end
# Display calculated result
print("\n Total pair : ", count)
end
end
def main()
task = Counting.new()
# Define the array of integer element
data = [5, 2, -2, 4, -1, 6, 1, -4, 2]
# Get number of element
n = data.length
# Resultant sum
sum = 3
# Test
task.countPair(data, n, sum)
end
main()
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
import scala.collection.mutable._;
/*
Scala Program
Find pairs with given sum in an array
*/
class Counting
{
// Display given matrix element
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
// iterate the loop through by n
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
def countPair(data: Array[Int], n: Int, sum: Int): Unit = {
var record: Map[Int, Int] = Map();
// Define some auxiliary variable
var count: Int = 0;
var i: Int = 0;
// Display given array element
print(" Given Array : ");
this.display(data, n);
print(" Given Sum : " + sum);
// Execute loop through by array size
// Count element frequency
while (i < n)
{
if (record.contains(data(i)))
{
record.addOne(data(i), record.get(data(i)).get + 1);
}
else
{
record.addOne(data(i), 1);
}
i += 1;
}
i = 0;
// Count resultant pair
while (i < n)
{
if (record.contains(sum - data(i)))
{
// Count pairs by frequency
count += record.get(sum - data(i)).get;
}
if (data(i) == sum - data(i))
{
count -= 1;
}
i += 1;
}
if (count > 0)
{
count = (count / 2).toInt;
}
// Display calculated result
print("\n Total pair : " + count);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
// Define the array of integer element
var data: Array[Int] = Array(5, 2, -2, 4, -1, 6, 1, -4, 2);
// Get number of element
var n: Int = data.length;
// Resultant sum
var sum: Int = 3;
// Test
task.countPair(data, n, sum);
}
}
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
import Foundation
/*
Swift 4 Program
Find pairs with given sum in an array
*/
class Counting
{
// Display given matrix element
func display(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
// iterate the loop through by n
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func countPair(_ data: [Int], _ n: Int, _ sum: Int)
{
var record = [Int: Int]();
// Define some auxiliary variable
var count: Int = 0;
var i: Int = 0;
// Display given array element
print(" Given Array : ", terminator: "");
self.display(data, n);
print(" Given Sum : ", sum, terminator: "");
// Execute loop through by array size
// Count element frequency
while (i < n)
{
if (record.keys.contains(data[i]))
{
record[data[i]] = record[data[i]]! + 1;
}
else
{
record[data[i]] = 1;
}
i += 1;
}
i = 0;
// Count resultant pair
while (i < n)
{
if (record.keys.contains(sum - data[i]))
{
// Count pairs by frequency
count += record[sum - data[i]]!;
}
if (data[i] == sum - data[i])
{
count -= 1;
}
i += 1;
}
if (count > 0)
{
count = count / 2;
}
// Display calculated result
print("\n Total pair : ", count, terminator: "");
}
}
func main()
{
let task: Counting = Counting();
// Define the array of integer element
let data: [Int] = [5, 2, -2, 4, -1, 6, 1, -4, 2];
// Get number of element
let n: Int = data.count;
// Resultant sum
let sum: Int = 3;
// Test
task.countPair(data, n, sum);
}
main();
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
/*
Kotlin Program
Find pairs with given sum in an array
*/
class Counting
{
// Display given matrix element
fun display(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
// iterate the loop through by n
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
fun countPair(data: Array < Int > , n: Int, sum: Int): Unit
{
var record = mutableMapOf<Int, Int>();
// Define some auxiliary variable
var count: Int = 0;
var i: Int = 0;
// Display given array element
print(" Given Array : ");
this.display(data, n);
print(" Given Sum : " + sum);
// Execute loop through by array size
// Count element frequency
while (i < n)
{
if (record.containsKey(data[i]))
{
record.put(data[i], record.getValue(data[i]) + 1);
}
else
{
record.put(data[i], 1);
}
i += 1;
}
i = 0;
// Count resultant pair
while (i < n)
{
if (record.containsKey(sum - data[i]))
{
// Count pairs by frequency
count += record.getValue(sum - data[i]);
}
if (data[i] == sum - data[i])
{
count -= 1;
}
i += 1;
}
if (count > 0)
{
count = count / 2;
}
// Display calculated result
print("\n Total pair : " + count);
}
}
fun main(args: Array < String > ): Unit
{
var task: Counting = Counting();
// Define the array of integer element
var data: Array < Int > = arrayOf(5, 2, -2, 4, -1, 6, 1, -4, 2);
// Get number of element
var n: Int = data.count();
// Resultant sum
var sum: Int = 3;
// Test
task.countPair(data, n, sum);
}
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 4
<?php
/*
Php Program
Find pairs with given sum in an array
*/
class Counting
{
// Display given matrix element
public function display( & $arr, $n)
{
$i = 0;
// iterate the loop through by n
while ($i < $n)
{
echo " ". $arr[$i];
++$i;
}
echo "\n";
}
public function countPair( & $data, $n, $sum)
{
$record = array();
// Define some auxiliary variable
$count = 0;
$i = 0;
// Display given array element
echo " Given Array : ";
$this->display($data, $n);
echo " Given Sum : ". $sum;
// Execute loop through by array size
// Count element frequency
while ($i < $n)
{
if (array_key_exists($data[$i], $record))
{
$record[$data[$i]] = $record[$data[$i]] + 1;
}
else
{
$record[$data[$i]] = 1;
}++$i;
}
$i = 0;
// Count resultant pair
while ($i < $n)
{
if (array_key_exists($sum - $data[$i], $record))
{
// Count pairs by frequency
$count += $record[$sum - $data[$i]];
}
if ($data[$i] == $sum - $data[$i])
{
$count--;
}++$i;
}
if ($count > 0)
{
$count = intval($count / 2);
}
// Display calculated result
echo "\n Total pair : ". $count;
}
}
function main()
{
$task = new Counting();
// Define the array of integer element
$data = array(5, 2, -2, 4, -1, 6, 1, -4, 2);
// Get number of element
$n = count($data);
// Resultant sum
$sum = 3;
// Test
$task->countPair($data, $n, $sum);
}
main();
Output
Given Array : 5 2 -2 4 -1 6 1 -4 2
Given Sum : 3
Total pair : 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