Check pair with given product in an array
Here given code implementation process.
/*
Java Program
Check pair with given product in an array
*/
import java.util.HashSet;
public class Product
{
//Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
// Find pair of given product in an array
public void pairProduct(int[] arr, int n, int k)
{
// Result indicator
boolean result = false;
// Display given array
System.out.print(" Given Array : ");
display(arr, n);
// Display given product
System.out.print(" Given Product : " + k);
// Provides pair info when product exist
int a = 0;
int b = 0;
if (n > 1)
{
// Collection of all unique elements
HashSet < Integer > pairs = new HashSet < Integer > ();
// Get the distinct pair combination
for (int i = 0; i < n && result == false; ++i)
{
if (arr[i] == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr[i + 1];
}
else
{
b = arr[i - 1];
}
}
}
else if ((k % arr[i] == 0) && pairs.contains(k / arr[i]))
{
// When given product is generated by pair of two elements
a = arr[i];
b = (k / arr[i]);
result = true;
}
else if (arr[i] != 0)
{
pairs.add(arr[i]);
}
}
}
if (result == false)
{
// When no resultant pair exists
System.out.print("\n Output : No \n");
}
else
{
System.out.print("\n Pair (" + a + "," + (b) + ")");
System.out.print("\n Output : Yes \n");
}
}
public static void main(String[] arg)
{
Product task = new Product();
int[] arr = {
8 , 2 , 7 , 4 , 3 , 9 , 5 , 0
};
// Get number of element
int n = arr.length;
// product
int k = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
}
}
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
// Include header file
#include <iostream>
#include <set>
using namespace std;
class Product
{
public:
//Function which is display array elements
void display(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
// Find pair of given product in an array
void pairProduct(int arr[], int n, int k)
{
// Result indicator
bool result = false;
// Display given array
cout << " Given Array : ";
this->display(arr, n);
// Display given product
cout << " Given Product : " << k;
// Provides pair info when product exist
int a = 0;
int b = 0;
if (n > 1)
{
// Collection of all unique elements
set <int> pairs ;
// Get the distinct pair combination
for (int i = 0; i < n && result == false; ++i)
{
if (arr[i] == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr[i + 1];
}
else
{
b = arr[i - 1];
}
}
}
else if ((k % arr[i] == 0) && pairs.find(k / arr[i]) != pairs.end())
{
// When given product is generated by pair of two elements
a = arr[i];
b = (k / arr[i]);
result = true;
}
else if (arr[i] != 0)
{
pairs.insert(arr[i]);
}
}
}
if (result == false)
{
// When no resultant pair exists
cout << "\n Output : No \n";
}
else
{
cout << "\n Pair (" << a << "," << (b) << ")";
cout << "\n Output : Yes \n";
}
}
};
int main()
{
Product task = Product();
int arr[] = {
8 , 2 , 7 , 4 , 3 , 9 , 5 , 0
};
// Get number of element
int n = sizeof(arr) / sizeof(arr[0]);
// product
int k = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
return 0;
}
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Check pair with given product in an array
*/
public class Product
{
//Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
// Find pair of given product in an array
public void pairProduct(int[] arr, int n, int k)
{
// Result indicator
Boolean result = false;
// Display given array
Console.Write(" Given Array : ");
display(arr, n);
// Display given product
Console.Write(" Given Product : " + k);
// Provides pair info when product exist
int a = 0;
int b = 0;
if (n > 1)
{
// Collection of all unique elements
HashSet < int > pairs = new HashSet < int > ();
// Get the distinct pair combination
for (int i = 0; i < n && result == false; ++i)
{
if (arr[i] == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr[i + 1];
}
else
{
b = arr[i - 1];
}
}
}
else if ((k % arr[i] == 0) && pairs.Contains(k / arr[i]))
{
// When given product is generated by pair of two elements
a = arr[i];
b = (k / arr[i]);
result = true;
}
else if (arr[i] != 0)
{
pairs.Add(arr[i]);
}
}
}
if (result == false)
{
// When no resultant pair exists
Console.Write("\n Output : No \n");
}
else
{
Console.Write("\n Pair (" + a + "," + b + ")");
Console.Write("\n Output : Yes \n");
}
}
public static void Main(String[] arg)
{
Product task = new Product();
int[] arr = {
8 , 2 , 7 , 4 , 3 , 9 , 5 , 0
};
// Get number of element
int n = arr.Length;
// product
int k = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
}
}
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
<?php
/*
Php Program
Check pair with given product in an array
*/
class Product
{
//Function which is display array elements
public function display( & $arr, $n)
{
for ($i = 0; $i < $n; ++$i)
{
echo " ". $arr[$i];
}
echo "\n";
}
// Find pair of given product in an array
public function pairProduct( & $arr, $n, $k)
{
// Result indicator
$result = false;
// Display given array
echo " Given Array : ";
$this->display($arr, $n);
// Display given product
echo " Given Product : ". $k;
// Provides pair info when product exist
$a = 0;
$b = 0;
if ($n > 1)
{
// Collection of all unique elements
$pairs = array();
// Get the distinct pair combination
for ($i = 0; $i < $n && $result == false; ++$i)
{
if ($arr[$i] == 0)
{
if ($k == 0)
{
$result = true;
$a = 0;
if ($i == 0)
{
$b = $arr[$i + 1];
}
else
{
$b = $arr[$i - 1];
}
}
}
else if (($k % $arr[$i] == 0)
&& in_array(($k / $arr[$i]), $pairs, TRUE))
{
// When given product is generated by pair of two elements
$a = $arr[$i];
$b = (intval($k / $arr[$i]));
$result = true;
}
else if ($arr[$i] != 0
&& in_array(($k / $arr[$i]), $pairs, TRUE) == false)
{
$pairs[] = $arr[$i];
}
}
}
if ($result == false)
{
// When no resultant pair exists
echo "\n Output : No \n";
}
else
{
echo "\n Pair (". $a .",". $b .")";
echo "\n Output : Yes \n";
}
}
}
function main()
{
$task = new Product();
$arr = array(8, 2, 7, 4, 3, 9, 5, 0);
// Get number of element
$n = count($arr);
// product
$k = 21;
// Case A
$task->pairProduct($arr, $n, $k);
// product
$k = 29;
// Case B
$task->pairProduct($arr, $n, $k);
// product
$k = 0;
// Case C
$task->pairProduct($arr, $n, $k);
}
main();
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
/*
Node Js Program
Check pair with given product in an array
*/
class Product
{
//Function which is display array elements
display(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
// Find pair of given product in an array
pairProduct(arr, n, k)
{
// Result indicator
var result = false;
// Display given array
process.stdout.write(" Given Array : ");
this.display(arr, n);
// Display given product
process.stdout.write(" Given Product : " + k);
// Provides pair info when product exist
var a = 0;
var b = 0;
if (n > 1)
{
// Collection of all unique elements
var pairs = new Set();
// Get the distinct pair combination
for (var i = 0; i < n && result == false; ++i)
{
if (arr[i] == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr[i + 1];
}
else
{
b = arr[i - 1];
}
}
}
else if ((k % arr[i] == 0) && pairs.has(parseInt(k / arr[i])))
{
// When given product is generated by pair of two elements
a = arr[i];
b = (parseInt(k / arr[i]));
result = true;
}
else if (arr[i] != 0)
{
pairs.add(arr[i]);
}
}
}
if (result == false)
{
// When no resultant pair exists
process.stdout.write("\n Output : No \n");
}
else
{
process.stdout.write("\n Pair (" + a + "," + b + ")");
process.stdout.write("\n Output : Yes \n");
}
}
}
function main()
{
var task = new Product();
var arr = [8, 2, 7, 4, 3, 9, 5, 0];
// Get number of element
var n = arr.length;
// product
var k = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
}
main();
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
# Python 3 Program
# Check pair with given product in an array
class Product :
# Function which is display list elements
def display(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
# Find pair of given product in an list
def pairProduct(self, arr, n, k) :
# Result indicator
result = False
# Display given list
print(" Given Array : ", end = "")
self.display(arr, n)
# Display given product
print(" Given Product : ", k, end = "")
# Provides pair info when product exist
a = 0
b = 0
if (n > 1) :
# Collection of all unique elements
pairs = set()
# Get the distinct pair combination
i = 0
while (i < n and result == False) :
if (arr[i] == 0) :
if (k == 0) :
result = True
a = 0
if (i == 0) :
b = arr[i + 1]
else :
b = arr[i - 1]
elif((k % arr[i] == 0) and int(k / arr[i]) in pairs) :
# When given product is generated by pair of two elements
a = arr[i]
b = int(k / arr[i])
result = True
elif(arr[i] != 0) :
pairs.add(arr[i])
i += 1
if (result == False) :
# When no resultant pair exists
print("\n Output : No ")
else :
print("\n Pair (", a ,",", b ,")", end = "")
print("\n Output : Yes ")
def main() :
task = Product()
arr = [8, 2, 7, 4, 3, 9, 5, 0]
# Get number of element
n = len(arr)
# product
k = 21
# Case A
task.pairProduct(arr, n, k)
# product
k = 29
# Case B
task.pairProduct(arr, n, k)
# product
k = 0
# Case C
task.pairProduct(arr, n, k)
if __name__ == "__main__": main()
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair ( 3 , 7 )
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair ( 0 , 5 )
Output : Yes
# Ruby Program
# Check pair with given product in an array
require "set"
class Product
# Function which is display array elements
def display(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end
print("\n")
end
# Find pair of given product in an array
def pairProduct(arr, n, k)
# Result indicator
result = false
# Display given array
print(" Given Array : ")
self.display(arr, n)
# Display given product
print(" Given Product : ", k)
# Provides pair info when product exist
a = 0
b = 0
if (n > 1)
# Collection of all unique elements
pairs = Set[]
# Get the distinct pair combination
i = 0
while (i < n && result == false)
if (arr[i] == 0)
if (k == 0)
result = true
a = 0
if (i == 0)
b = arr[i + 1]
else
b = arr[i - 1]
end
end
elsif((k % arr[i] == 0) && pairs.include?(k / arr[i]))
# When given product is generated by pair of two elements
a = arr[i]
b = (k / arr[i])
result = true
elsif(arr[i] != 0 )
pairs.add(arr[i])
end
i += 1
end
end
if (result == false)
# When no resultant pair exists
print("\n Output : No \n")
else
print("\n Pair (", a ,",", b ,")")
print("\n Output : Yes \n")
end
end
end
def main()
task = Product.new()
arr = [8, 2, 7, 4, 3, 9, 5, 0]
# Get number of element
n = arr.length
# product
k = 21
# Case A
task.pairProduct(arr, n, k)
# product
k = 29
# Case B
task.pairProduct(arr, n, k)
# product
k = 0
# Case C
task.pairProduct(arr, n, k)
end
main()
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
import scala.collection.mutable._;
/*
Scala Program
Check pair with given product in an array
*/
class Product
{
//Function which is display array elements
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// Find pair of given product in an array
def pairProduct(arr: Array[Int], n: Int, k: Int): Unit = {
// Result indicator
var result: Boolean = false;
// Display given array
print(" Given Array : ");
this.display(arr, n);
// Display given product
print(" Given Product : " + k);
// Provides pair info when product exist
var a: Int = 0;
var b: Int = 0;
if (n > 1)
{
// Collection of all unique elements
var pairs: Set[Int] = Set();
// Get the distinct pair combination
var i: Int = 0;
while (i < n && result == false)
{
if (arr(i) == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr(i + 1);
}
else
{
b = arr(i - 1);
}
}
}
else if ((k % arr(i) == 0) && pairs.contains((k / arr(i))))
{
// When given product is generated by pair of two elements
a = arr(i);
b = ((k / arr(i)).toInt);
result = true;
}
else if (arr(i) != 0)
{
pairs.add(arr(i));
}
i += 1;
}
}
if (result == false)
{
// When no resultant pair exists
print("\n Output : No \n");
}
else
{
print("\n Pair (" + a + "," + b + ")");
print("\n Output : Yes \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Product = new Product();
var arr: Array[Int] = Array(8, 2, 7, 4, 3, 9, 5, 0);
// Get number of element
var n: Int = arr.length;
// product
var k: Int = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
}
}
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
import Foundation
/*
Swift 4 Program
Check pair with given product in an array
*/
class Product
{
//Function which is display array elements
func display(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Find pair of given product in an array
func pairProduct(_ arr: [Int], _ n: Int, _ k: Int)
{
// Result indicator
var result: Bool = false;
// Display given array
print(" Given Array : ", terminator: "");
self.display(arr, n);
// Display given product
print(" Given Product : ", k, terminator: "");
// Provides pair info when product exist
var a: Int = 0;
var b: Int = 0;
if (n > 1)
{
// Collection of all unique elements
var pairs = Set<Int>();
// Get the distinct pair combination
var i: Int = 0;
while (i < n && result == false)
{
if (arr[i] == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr[i + 1];
}
else
{
b = arr[i - 1];
}
}
}
else if ((k % arr[i] == 0) && pairs.contains(k / arr[i]))
{
// When given product is generated by pair of two elements
a = arr[i];
b = (k / arr[i]);
result = true;
}
else if (arr[i] != 0)
{
pairs.insert(arr[i]);
}
i += 1;
}
}
if (result == false)
{
// When no resultant pair exists
print("\n Output : No ");
}
else
{
print("\n Pair (", a ,",", b ,")", terminator: "");
print("\n Output : Yes ");
}
}
}
func main()
{
let task: Product = Product();
let arr: [Int] = [8, 2, 7, 4, 3, 9, 5, 0];
// Get number of element
let n: Int = arr.count;
// product
var k: Int = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
}
main();
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair ( 3 , 7 )
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair ( 0 , 5 )
Output : Yes
/*
Kotlin Program
Check pair with given product in an array
*/
class Product
{
//Function which is display array elements
fun display(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
// Find pair of given product in an array
fun pairProduct(arr: Array < Int > , n: Int, k: Int): Unit
{
// Result indicator
var result: Boolean = false;
// Display given array
print(" Given Array : ");
this.display(arr, n);
// Display given product
print(" Given Product : " + k);
// Provides pair info when product exist
var a: Int = 0;
var b: Int = 0;
if (n > 1)
{
// Collection of all unique elements
var pairs: MutableSet <Int> = mutableSetOf <Int> ();
// Get the distinct pair combination
var i: Int = 0;
while (i < n && result == false)
{
if (arr[i] == 0)
{
if (k == 0)
{
result = true;
a = 0;
if (i == 0)
{
b = arr[i + 1];
}
else
{
b = arr[i - 1];
}
}
}
else if ((k % arr[i] == 0) && pairs.contains(k / arr[i]))
{
// When given product is generated by pair of two elements
a = arr[i];
b = (k / arr[i]);
result = true;
}
else if (arr[i] != 0)
{
pairs.add(arr[i]);
}
i += 1;
}
}
if (result == false)
{
// When no resultant pair exists
print("\n Output : No \n");
}
else
{
print("\n Pair (" + a + "," + b + ")");
print("\n Output : Yes \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Product = Product();
var arr: Array < Int > = arrayOf(8, 2, 7, 4, 3, 9, 5, 0);
// Get number of element
var n: Int = arr.count();
// product
var k: Int = 21;
// Case A
task.pairProduct(arr, n, k);
// product
k = 29;
// Case B
task.pairProduct(arr, n, k);
// product
k = 0;
// Case C
task.pairProduct(arr, n, k);
}
Output
Given Array : 8 2 7 4 3 9 5 0
Given Product : 21
Pair (3,7)
Output : Yes
Given Array : 8 2 7 4 3 9 5 0
Given Product : 29
Output : No
Given Array : 8 2 7 4 3 9 5 0
Given Product : 0
Pair (0,5)
Output : Yes
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