Find pair with greatest product in array
Here given code implementation process.
//C Program
//Find pair with greatest product in array
#include <stdio.h>
//Check that given product is exist or not in array
int is_exist(int arr[],int size,int element)
{
for (int i = 0; i < size; ++i)
{
if(arr[i]==element)
{
//when product is exist
return 1;
}
}
return 0;
}
void product(int arr[],int size)
{
if(size<=1)
{
return;
}
int result = 0, status = 0;
for (int i = 0; i < size; ++i)
{
for (int j = i+1; j < size; ++j)
{
if(is_exist(arr,size,arr[i]*arr[j])==1)
{
if(status==0)
{
//when get first product
status = 1;
result = arr[i]*arr[j];
}
else if(arr[i] * arr[j] > result)
{
result = arr[i]*arr[j];
}
}
}
}
if(status==0)
{
printf("\nThere are no pair product in given array\n");
}
else
{
printf("%d\n", result);
}
}
int main()
{
//Define the value of array elements
int arr[] ={2,3,6,10,5,24};
//Get the size of array
int size=(sizeof(arr)/sizeof(arr[0]));
product(arr,size);
return 0;
}
Output
10
/*
C++ Program
Find pair with greatest product in array
*/
#include<iostream>
using namespace std;
class MyArray {
public:
//Check that given product is exist or not in array
bool is_exist(int arr[], int size, int element) {
for (int i = 0; i < size; ++i) {
if (arr[i] == element) {
return true;
}
}
return false;
}
void product(int arr[], int size) {
if (size <= 1) {
return;
}
int result = 0, status = 0;
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
if (this->is_exist(arr, size, arr[i] *arr[j]) == true) {
if (status == 0) {
//when get first product
status = 1;
result = arr[i] *arr[j];
} else
if (arr[i] *arr[j] > result) {
result = arr[i] *arr[j];
}
}
}
}
if (status == 0) {
cout << "\nThere are no pair product in given array\n";
} else {
cout << " " << result << "\n";
}
}
};
int main() {
MyArray obj;
int arr[] = {
2,
3,
6,
10,
5,
24
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
obj.product(arr, size);
return 0;
}
Output
10
/*
Java Program
Find pair with greatest product in array
*/
public class MyArray
{
//Check that given product is exist or not in array
public boolean is_exist(int []arr,int size,int element)
{
for (int i = 0; i < size; ++i)
{
if(arr[i]==element)
{
//when product is exist
return true;
}
}
return false;
}
public void product(int []arr,int size)
{
if(size<=1)
{
return;
}
int result = 0, status = 0;
for (int i = 0; i < size; ++i)
{
for (int j = i+1; j < size; ++j)
{
if(is_exist(arr,size,arr[i]*arr[j])==true)
{
if(status==0)
{
//when get first product
status = 1;
result = arr[i]*arr[j];
}
else if(arr[i] * arr[j] > result)
{
result = arr[i]*arr[j];
}
}
}
}
if(status==0)
{
System.out.print("\nThere are no pair product in given array\n");
}
else
{
System.out.print(" "+result+"\n");
}
}
public static void main(String[] args) {
MyArray obj = new MyArray();
//Define the value of array elements
int []arr = {2, 3, 6, 10, 5, 24};
//Get the size of array
int size = arr.length;
obj.product(arr,size);
}
}
Output
10
/*
C# Program
Find pair with greatest product in array
*/
using System;
public class MyArray {
//Check that given product is exist or not in array
public Boolean is_exist(int[] arr, int size, int element) {
for (int i = 0; i < size; ++i) {
if (arr[i] == element) {
return true;
}
}
return false;
}
public void product(int[] arr, int size) {
if (size <= 1) {
return;
}
int result = 0, status = 0;
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
if (is_exist(arr, size, arr[i] * arr[j]) == true) {
if (status == 0) {
//when get first product
status = 1;
result = arr[i] * arr[j];
} else
if (arr[i] * arr[j] > result) {
result = arr[i] * arr[j];
}
}
}
}
if (status == 0) {
Console.Write("\nThere are no pair product in given array\n");
} else {
Console.Write(" " + result + "\n");
}
}
public static void Main(String[] args) {
MyArray obj = new MyArray();
//Define the value of array elements
int[] arr = {
2,
3,
6,
10,
5,
24
};
//Get the size of array
int size = arr.Length;
obj.product(arr, size);
}
}
Output
10
<?php
/*
Php Program
Find pair with greatest product in array
*/
class MyArray {
//Check that given product is exist or not in array
public function is_exist($arr, $size, $element) {
for ($i = 0; $i < $size; ++$i) {
if ($arr[$i] == $element) {
return true;
}
}
return false;
}
public function product($arr, $size) {
if ($size <= 1) {
return;
}
$result = 0;
$status = 0;
for ($i = 0; $i < $size; ++$i) {
for ($j = $i + 1; $j < $size; ++$j) {
if ($this->is_exist($arr, $size, $arr[$i] *$arr[$j]) == true) {
if ($status == 0) {
//when get first product
$status = 1;
$result = $arr[$i] *$arr[$j];
} else
if ($arr[$i] *$arr[$j] > $result) {
$result = $arr[$i] *$arr[$j];
}
}
}
}
if ($status == 0) {
echo("\nThere are no pair product in given array\n");
} else {
echo(" ". $result ."\n");
}
}
}
function main() {
$obj = new MyArray();
//Define the value of array elements
$arr = array(2, 3, 6, 10, 5, 24);
//Get the size of array
$size = count($arr);
$obj->product($arr, $size);
}
main();
Output
10
/*
Node Js Program
Find pair with greatest product in array
*/
class MyArray {
//Check that given product is exist or not in array
is_exist(arr, size, element) {
for (var i = 0; i < size; ++i) {
if (arr[i] == element) {
return true;
}
}
return false;
}
product(arr, size) {
if (size <= 1) {
return;
}
var result = 0;
var status = 0;
for (var i = 0; i < size; ++i) {
for (var j = i + 1; j < size; ++j) {
if (this.is_exist(arr, size, arr[i] *arr[j]) == true) {
if (status == 0) {
//when get first product
status = 1;
result = arr[i] *arr[j];
} else
if (arr[i] *arr[j] > result) {
result = arr[i] *arr[j];
}
}
}
}
if (status == 0) {
process.stdout.write("\nThere are no pair product in given array\n");
} else {
process.stdout.write(" " + result + "\n");
}
}
}
function main(args) {
var obj = new MyArray();
//Define the value of array elements
var arr = [2, 3, 6, 10, 5, 24];
//Get the size of array
var size = arr.length;
obj.product(arr, size);
}
main();
Output
10
# Python 3 Program
# Find pair with greatest product in array
class MyArray :
# Check that given product is exist or not in array
def is_exist(self, arr, size, element) :
i = 0
while (i < size) :
if (arr[i] == element) :
return True
i += 1
return False
def product(self, arr, size) :
if (size <= 1) :
return
result = 0
status = 0
i = 0
while (i < size) :
j = i + 1
while (j < size) :
if (self.is_exist(arr, size, arr[i] * arr[j]) == True) :
if (status == 0) :
# when get first product
status = 1
result = arr[i] * arr[j]
elif (arr[i] * arr[j] > result) :
result = arr[i] * arr[j]
j += 1
i += 1
if (status == 0) :
print("\nThere are no pair product in given array\n", end = "")
else :
print(" ", result ,"\n", end = "")
def main() :
obj = MyArray()
arr = [2, 3, 6, 10, 5, 24]
size = len(arr)
obj.product(arr, size)
if __name__ == "__main__":
main()
Output
10
# Ruby Program
# Find pair with greatest product in array
class MyArray
# Check that given product is exist or not in array
def is_exist(arr, size, element)
i = 0
while (i < size)
if (arr[i] == element)
return true
end
i += 1
end
return false
end
def product(arr, size)
if (size <= 1)
return
end
result = 0
status = 0
i = 0
while (i < size)
j = i + 1
while (j < size)
if (self.is_exist(arr, size, arr[i] * arr[j]) == true)
if (status == 0)
# when get first product
status = 1
result = arr[i] * arr[j]
elsif (arr[i] * arr[j] > result)
result = arr[i] * arr[j]
end
end
j += 1
end
i += 1
end
if (status == 0)
print("\nThere are no pair product in given array\n")
else
print(" ", result ,"\n")
end
end
end
def main()
obj = MyArray.new()
arr = [2, 3, 6, 10, 5, 24]
size = arr.length
obj.product(arr, size)
end
main()
Output
10
/*
Scala Program
Find pair with greatest product in array
*/
class MyArray {
//Check that given product is exist or not in array
def is_exist(arr: Array[Int], size: Int, element: Int): Boolean = {
var i: Int = 0;
while (i < size) {
if (arr(i) == element) {
return true;
}
i += 1;
}
return false;
}
def product(arr: Array[Int], size: Int): Unit = {
if (size <= 1) {
return;
}
var result: Int = 0;
var status: Int = 0;
var i: Int = 0;
while (i < size) {
var j: Int = i + 1;
while (j < size) {
if (this.is_exist(arr, size, arr(i) * arr(j)) == true) {
if (status == 0) {
//when get first product
status = 1;
result = arr(i) * arr(j);
} else
if (arr(i) * arr(j) > result) {
result = arr(i) * arr(j);
}
}
j += 1;
}
i += 1;
}
if (status == 0) {
print("\nThere are no pair product in given array\n");
} else {
print(" " + result + "\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyArray = new MyArray();
val arr: Array[Int] = Array(2, 3, 6, 10, 5, 24);
val size: Int = arr.length;
obj.product(arr, size);
}
}
Output
10
/*
Swift Program
Find pair with greatest product in array
*/
class MyArray {
//Check that given product is exist or not in array
func is_exist(_ arr: [Int], _ size: Int, _ element: Int) -> Bool {
var i: Int = 0;
while (i < size) {
if (arr[i] == element) {
return true;
}
i += 1;
}
return false;
}
func product(_ arr: [Int], _ size: Int) {
if (size <= 1) {
return;
}
var result: Int = 0;
var status: Int = 0;
var i: Int = 0;
while (i < size) {
var j: Int = i + 1;
while (j < size) {
if (self.is_exist(arr, size, arr[i] * arr[j]) == true) {
if (status == 0) {
//when get first product
status = 1;
result = arr[i] * arr[j];
} else
if (arr[i] * arr[j] > result) {
result = arr[i] * arr[j];
}
}
j += 1;
}
i += 1;
}
if (status == 0) {
print("\nThere are no pair product in given array\n", terminator: "");
} else {
print(" ", result ,"\n", terminator: "");
}
}
}
func main() {
let obj: MyArray = MyArray();
let arr: [Int] = [2, 3, 6, 10, 5, 24];
let size: Int = arr.count;
obj.product(arr, size);
}
main();
Output
10
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