Posted on by Kalkicode
Code Hash

# Count pairs whose products exist in array

Here given code implementation process.

``````/*
Java Program
Count pairs whose products exist in array
*/
import java.util.HashSet;
public class Products
{
// 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");
}
// Count all pairs whose product exists in given array
public void productPair(int[] data, int n)
{
// Use to collect unique element
HashSet < Integer > record = new HashSet < Integer > ();
// Define some auxiliary variables
int i = 0;
int j = 0;
// Resultant variable
int count = 0;
// Display given array element
System.out.print(" Given Array  : ");
display(data, n);
// Execute loop through by array size
for (i = 0; i < n; ++i)
{
}
// Outer loop
for (i = 0; i < n; ++i)
{
// Inner loop
for (j = i + 1; j < n; j++)
{
if (record.contains(data[i] * data[j]))
{
count++;
}
}
}
// Display calculated result
System.out.print(" Total Product  : " + count);
}
public static void main(String[] arg)
{
Products task = new Products();
// Define array of integer elements
int[] data = {
7 , 3 , 9 , 4 , 8 , 21 , 6 , 2 , 16 , 5
};
// Get the number of elements
int n = data.length;
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
}
}``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````// Include header file
#include <iostream>
#include <set>
using namespace std;
/*
C++ Program
Count pairs whose products exist in array
*/
class Products
{
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";
}
// Count all pairs whose product exists in given array
void productPair(int data[], int n)
{
// Use to collect unique element
set < int > record;
// Define some auxiliary variables
int i = 0;
int j = 0;
// Resultant variable
int count = 0;
// Display given array element
cout << " Given Array  : ";
this->display(data, n);
// Execute loop through by array size
for (i = 0; i < n; ++i)
{
record.insert(data[i]);
}
// Outer loop
for (i = 0; i < n; ++i)
{
// Inner loop
for (j = i + 1; j < n; j++)
{
if (record.find(data[i] *data[j]) != record.end())
{
count++;
}
}
}
// Display calculated result
cout << " Total Product  : " << count;
}
};
int main()
{
Products task = Products();
// Define array of integer elements
int data[] = {
7 , 3 , 9 , 4 , 8 , 21 , 6 , 2 , 16 , 5
};
// Get the number of elements
int n = sizeof(data) / sizeof(data[0]);
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
return 0;
}``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Count pairs whose products exist in array
*/
public class Products
{
// 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");
}
// Count all pairs whose product exists in given array
public void productPair(int[] data, int n)
{
// Use to collect unique element
HashSet < int > record = new HashSet < int > ();
// Define some auxiliary variables
int i = 0;
int j = 0;
// Resultant variable
int count = 0;
// Display given array element
Console.Write(" Given Array  : ");
display(data, n);
// Execute loop through by array size
for (i = 0; i < n; ++i)
{
}
// Outer loop
for (i = 0; i < n; ++i)
{
// Inner loop
for (j = i + 1; j < n; j++)
{
if (record.Contains(data[i] * data[j]))
{
count++;
}
}
}
// Display calculated result
Console.Write(" Total Product  : " + count);
}
public static void Main(String[] arg)
{
Products task = new Products();
// Define array of integer elements
int[] data = {
7 , 3 , 9 , 4 , 8 , 21 , 6 , 2 , 16 , 5
};
// Get the number of elements
int n = data.Length;
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
}
}``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````<?php
/*
Php Program
Count pairs whose products exist in array
*/
class Products
{
// Display given matrix element
public	function display( & \$arr, \$n)
{
// iterate the loop through by n
for (\$i = 0; \$i < \$n; ++\$i)
{
echo " ". \$arr[\$i];
}
echo "\n";
}
// Count all pairs whose product exists in given array
public	function productPair( & \$data, \$n)
{
// Use to collect unique element
\$record = array();
// Define some auxiliary variables
\$i = 0;
\$j = 0;
// Resultant variable
\$count = 0;
// Display given array element
echo " Given Array  : ";
\$this->display(\$data, \$n);
// Execute loop through by array size
for (\$i = 0; \$i < \$n; ++\$i)
{
\$record[] = \$data[\$i];
}
// Outer loop
for (\$i = 0; \$i < \$n; ++\$i)
{
// Inner loop
for (\$j = \$i + 1; \$j < \$n; \$j++)
{
if (in_array(\$data[\$i] * \$data[\$j], \$record, TRUE))
{
\$count++;
}
}
}
// Display calculated result
echo " Total Product  : ". \$count;
}
}

function main()
{
\$task = new Products();
// Define array of integer elements
\$data = array(7, 3, 9, 4, 8, 21, 6, 2, 16, 5);
// Get the number of elements
\$n = count(\$data);
}
main();``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````/*
Node Js Program
Count pairs whose products exist in array
*/
class Products
{
// 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");
}
// Count all pairs whose product exists in given array
productPair(data, n)
{
// Use to collect unique element
var record = new Set();
// Define some auxiliary variables
var i = 0;
var j = 0;
// Resultant variable
var count = 0;
// Display given array element
process.stdout.write(" Given Array  : ");
this.display(data, n);
// Execute loop through by array size
for (i = 0; i < n; ++i)
{
}
// Outer loop
for (i = 0; i < n; ++i)
{
// Inner loop
for (j = i + 1; j < n; j++)
{
if (record.has(data[i] * data[j]))
{
count++;
}
}
}
// Display calculated result
process.stdout.write(" Total Product  : " + count);
}
}

function main()
{
var task = new Products();
// Define array of integer elements
var data = [7, 3, 9, 4, 8, 21, 6, 2, 16, 5];
// Get the number of elements
var n = data.length;
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
}
main();``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````#  Python 3 Program
#  Count pairs whose products exist in array

class Products :
#  Display given matrix element
def display(self, arr, n) :
#  iterate the loop through by n
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

#  Count all pairs whose product exists in given list
def productPair(self, data, n) :
#  Use to collect unique element
record = set()
#  Define some auxiliary variables
i = 0
j = 0
#  Resultant variable
count = 0
#  Display given list element
print(" Given Array  : ", end = "")
self.display(data, n)
#  Execute loop through by list size
i = 0
while (i < n) :
i += 1

#  Outer loop
i = 0
while (i < n) :
#  Inner loop
j = i + 1
while (j < n) :
if (data[i] * data[j] in record) :
count += 1

j += 1

i += 1

#  Display calculated result
print(" Total Product  : ", count, end = "")

def main() :
#  Define list of integer elements
data = [7, 3, 9, 4, 8, 21, 6, 2, 16, 5]
#  Get the number of elements
n = len(data)
#  (7, 3) = 21
#  (8, 2) = 16
#  (4, 2) = 8
#  (4, 3) = 12
#  O/p = 4

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

#### Output

`````` Given Array  :   7  3  9  4  8  21  6  2  16  5
Total Product  :  4``````
``````require 'set'
#    Ruby Program
#    Count pairs whose products exist in array

class Products
#  Display given matrix element
def display(arr, n)
#  iterate the loop through by n
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

print("\n")
end

#  Count all pairs whose product exists in given array
def productPair(data, n)
#  Use to collect unique element
record = Set[]
#  Define some auxiliary variables
i = 0
j = 0
#  Resultant variable
count = 0
#  Display given array element
print(" Given Array  : ")
self.display(data, n)
#  Execute loop through by array size
i = 0
while (i < n)
i += 1
end

#  Outer loop
i = 0
while (i < n)
#  Inner loop
j = i + 1
while (j < n)
if (record.include?(data[i] * data[j]))
count += 1
end

j += 1
end

i += 1
end

#  Display calculated result
print(" Total Product  : ", count)
end

end

def main()
#  Define array of integer elements
data = [7, 3, 9, 4, 8, 21, 6, 2, 16, 5]
#  Get the number of elements
n = data.length
#  (7, 3) = 21
#  (8, 2) = 16
#  (4, 2) = 8
#  (4, 3) = 12
#  O/p = 4
end

main()``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````import scala.collection.mutable._;
/*
Scala Program
Count pairs whose products exist in array
*/
class Products
{
// Display given matrix element
def display(arr: Array[Int], n: Int): Unit = {
// iterate the loop through by n
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// Count all pairs whose product exists in given array
def productPair(data: Array[Int], n: Int): Unit = {
// Use to collect unique element

var record: Set[Int] = Set();
// Define some auxiliary variables
var i: Int = 0;
var j: Int = 0;
// Resultant variable
var count: Int = 0;
// Display given array element
print(" Given Array  : ");
this.display(data, n);
// Execute loop through by array size
i = 0;
while (i < n)
{
i += 1;
}
// Outer loop
i = 0;
while (i < n)
{
// Inner loop
j = i + 1;
while (j < n)
{
if (record.contains(data(i) * data(j)))
{
count += 1;
}
j += 1;
}
i += 1;
}
// Display calculated result
print(" Total Product  : " + count);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Products = new Products();
// Define array of integer elements
var data: Array[Int] = Array(7, 3, 9, 4, 8, 21, 6, 2, 16, 5);
// Get the number of elements
var n: Int = data.length;
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
}
}``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````
``````import Foundation
/*
Swift 4 Program
Count pairs whose products exist in array
*/
class Products
{
// Display given matrix element
func display(_ arr: [Int], _ n: Int)
{
// iterate the loop through by n
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Count all pairs whose product exists in given array
func productPair(_ data: [Int], _ n: Int)
{
// Use to collect unique element
var record = Set<Int>();
// Define some auxiliary variables
var i: Int = 0;
var j: Int = 0;
// Resultant variable
var count: Int = 0;
// Display given array element
print(" Given Array  : ", terminator: "");
self.display(data, n);
// Execute loop through by array size
while (i < n)
{
record.insert(data[i]);
i += 1;
}
// Outer loop
i = 0;
while (i < n)
{
// Inner loop
j = i + 1;
while (j < n)
{
if (record.contains(data[i] * data[j]))
{
count += 1;
}
j += 1;
}
i += 1;
}
// Display calculated result
print(" Total Product  : ", count, terminator: "");
}
}
func main()
{
let task: Products = Products();
// Define array of integer elements
let data: [Int] = [7, 3, 9, 4, 8, 21, 6, 2, 16, 5];
// Get the number of elements
let n: Int = data.count;
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
}
main();``````

#### Output

`````` Given Array  :   7  3  9  4  8  21  6  2  16  5
Total Product  :  4``````
``````/*
Kotlin Program
Count pairs whose products exist in array
*/
class Products
{
// Display given matrix element
fun display(arr: Array < Int > , n: Int): Unit
{
// iterate the loop through by n
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
// Count all pairs whose product exists in given array
fun productPair(data: Array < Int > , n: Int): Unit
{
// Use to collect unique element

var record: MutableSet <Int> = mutableSetOf <Int> ();
// Define some auxiliary variables
var i: Int = 0;
var j: Int ;
// Resultant variable
var count: Int = 0;
// Display given array element
print(" Given Array  : ");
this.display(data, n);
// Execute loop through by array size
while (i < n)
{
i += 1;
}
// Outer loop
i = 0;
while (i < n)
{
// Inner loop
j = i + 1;
while (j < n)
{
if (record.contains(data[i] * data[j]))
{
count += 1;
}
j += 1;
}
i += 1;
}
// Display calculated result
print(" Total Product  : " + count);
}
}
fun main(args: Array < String > ): Unit
{
var task: Products = Products();
// Define array of integer elements
var data: Array < Int > = arrayOf(7, 3, 9, 4, 8, 21, 6, 2, 16, 5);
// Get the number of elements
var n: Int = data.count();
// (7, 3) = 21
// (8, 2) = 16
// (4, 2) = 8
// (4, 3) = 12
// O/p = 4
}``````

#### Output

`````` Given Array  :  7 3 9 4 8 21 6 2 16 5
Total Product  : 4``````

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