# Find a pair with maximum product in array

Here given code implementation process.

``````// C Program
// Find a pair with maximum product in array
#include <stdio.h>

#include <limits.h>
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
printf("\n");
}
//Find maximum product of given unsorted array
void maximum_product(int arr[], int size)
{
if (size < 2)
{
return;
}
//Display array element
printf("\n Array  :");
display(arr, size);
// Variable which is used to store two smallest element
int small_a = INT_MAX;
int small_b = INT_MAX;
// Variable which is used to store two largest element
int big_a = INT_MIN;
int big_b = INT_MIN;
for (int i = 0; i < size ; i++)
{
//Find two large numbers
if (big_a < arr[i])
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr[i];
}
else if (big_b < arr[i])
{
big_b = arr[i];
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr[i])
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr[i];
}
else if (small_b > arr[i])
{
small_b = arr[i];
}
}
if (small_a *small_b > big_a *big_b)
{
printf(" Maximum product [(%d)*(%d)] : %d\n", small_a, small_b, small_a *small_b);
}
else
{
printf(" Maximum product [(%d)*(%d)] : %d\n", big_a, big_b, (big_a *big_b));
}
}
int main()
{
//Define array of integer elements
int arr1[] = {
1 , 4 , 8 , 3 , 5 , 2
};
//Get the size of array
int size = sizeof(arr1) / sizeof(arr1);
maximum_product(arr1, size);
int arr2[] = {
6 , 3 , -4 , -7 , 2 , 4
};
//Get the size of array
size = sizeof(arr2) / sizeof(arr2);
maximum_product(arr2, size);
int arr3[] = {
1 , 8 , 2 , -4 , -3
};
//Get the size of array
size = sizeof(arr3) / sizeof(arr3);
maximum_product(arr3, size);
return 0;
}``````

#### Output

`````` Array  :  1  4  8  3  5  2
Maximum product [(8)*(5)] : 40

Array  :  6  3  -4  -7  2  4
Maximum product [(-7)*(-4)] : 28

Array  :  1  8  2  -4  -3
Maximum product [(8)*(2)] : 16``````
``````/*
Java Program
Find a pair with maximum product in array
*/
class MyArray
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
//Find maximum product of given unsorted array
public void maximum_product(int[] arr, int size)
{
if (size < 2)
{
return;
}
System.out.print("\n Array :");
display(arr, size);
// Variable which is used to store two smallest element
int small_a = Integer.MAX_VALUE;
int small_b = Integer.MAX_VALUE;
// Variable which is used to store two largest element
int big_a = Integer.MIN_VALUE;
int big_b = Integer.MIN_VALUE;
for (int i = 0; i < size; i++)
{
//Find two large numbers
if (big_a < arr[i])
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr[i];
}
else if (big_b < arr[i])
{
big_b = arr[i];
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr[i])
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr[i];
}
else if (small_b > arr[i])
{
small_b = arr[i];
}
}
if (small_a * small_b > big_a * big_b)
{
System.out.print(" Maximum product [(" + small_a + ")*(" + small_b + ")] : " + small_a * small_b + "\n");
}
else
{
System.out.print(" Maximum product [(" + big_a + ")*(" + big_b + ")] : " + (big_a * big_b) + "\n");
}
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//Define array of integer elements
int[] arr1 = {
1,
4,
8,
3,
5,
2
};
//Get the size of array
int size = arr1.length;
obj.maximum_product(arr1, size);
int[] arr2 = {
6,
3,
-4,
-7,
2,
4
};
//Get the size of array
size = arr2.length;
obj.maximum_product(arr2, size);
int[] arr3 = {
1,
8,
2,
-4,
-3
};
//Get the size of array
size = arr3.length;
obj.maximum_product(arr3, size);
}
}``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16``````
``````//Include header file
#include <iostream>

#include<limits.h>

using namespace std;
/*
C++ Program
Find a pair with maximum product in array
*/
class MyArray
{
public:
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
//Find maximum product of given unsorted array
void maximum_product(int arr[], int size)
{
if (size < 2)
{
return;
}
cout << "\n Array :";
this->display(arr, size);
// Variable which is used to store two smallest element
int small_a = INT_MAX;
int small_b = INT_MAX;
// Variable which is used to store two largest element
int big_a = INT_MIN;
int big_b = INT_MIN;
for (int i = 0; i < size; i++)
{
//Find two large numbers
if (big_a < arr[i])
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr[i];
}
else if (big_b < arr[i])
{
big_b = arr[i];
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr[i])
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr[i];
}
else if (small_b > arr[i])
{
small_b = arr[i];
}
}
if (small_a * small_b > big_a * big_b)
{
cout << " Maximum product [(" << small_a << ")*(" << small_b << ")] : " << small_a * small_b << "\n";
}
else
{
cout << " Maximum product [(" << big_a << ")*(" << big_b << ")] : " << (big_a * big_b) << "\n";
}
}
};
int main()
{
MyArray obj = MyArray();
int arr1[] = {
1 , 4 , 8 , 3 , 5 , 2
};
//Get the size of array
int size = sizeof(arr1) / sizeof(arr1);
obj.maximum_product(arr1, size);
int arr2[] = {
6 , 3 , -4 , -7 , 2 , 4
};
//Get the size of array
size = sizeof(arr2) / sizeof(arr2);
obj.maximum_product(arr2, size);
int arr3[] = {
1 , 8 , 2 , -4 , -3
};
//Get the size of array
size = sizeof(arr3) / sizeof(arr3);
obj.maximum_product(arr3, size);
return 0;
}``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16``````
``````//Include namespace system
using System;
/*
C# Program
Find a pair with maximum product in array
*/
class MyArray
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
//Find maximum product of given unsorted array
public void maximum_product(int[] arr, int size)
{
if (size < 2)
{
return;
}
Console.Write("\n Array :");
display(arr, size);
// Variable which is used to store two smallest element
int small_a = int.MaxValue;
int small_b = int.MaxValue;
// Variable which is used to store two largest element
int big_a = int.MinValue;
int big_b = int.MinValue;
for (int i = 0; i < size; i++)
{
//Find two large numbers
if (big_a < arr[i])
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr[i];
}
else if (big_b < arr[i])
{
big_b = arr[i];
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr[i])
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr[i];
}
else if (small_b > arr[i])
{
small_b = arr[i];
}
}
if (small_a * small_b > big_a * big_b)
{
Console.Write(" Maximum product [(" + small_a + ")*(" + small_b + ")] : " + small_a * small_b + "\n");
}
else
{
Console.Write(" Maximum product [(" + big_a + ")*(" + big_b + ")] : " + (big_a * big_b) + "\n");
}
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] arr1 = {
1 , 4 , 8 , 3 , 5 , 2
};
//Get the size of array
int size = arr1.Length;
obj.maximum_product(arr1, size);
int[] arr2 = {
6 , 3 , -4 , -7 , 2 , 4
};
//Get the size of array
size = arr2.Length;
obj.maximum_product(arr2, size);
int[] arr3 = {
1 , 8 , 2 , -4 , -3
};
//Get the size of array
size = arr3.Length;
obj.maximum_product(arr3, size);
}
}``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16``````
``````<?php
/*
Php Program
Find a pair with maximum product in array
*/
class MyArray
{
//Function which is display array elements
public	function display( \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ". \$arr[\$i];
}
echo "\n";
}
//Find maximum product of given unsorted array
public	function maximum_product( \$arr, \$size)
{
if (\$size < 2)
{
return;
}
echo "\n Array :";
\$this->display(\$arr, \$size);
// Variable which is used to store two smallest element
\$small_a = PHP_INT_MAX;
\$small_b = PHP_INT_MAX;
// Variable which is used to store two largest element
\$big_a = -PHP_INT_MAX;
\$big_b = -PHP_INT_MAX;
for (\$i = 0; \$i < \$size; \$i++)
{
//Find two large numbers
if (\$big_a < \$arr[\$i])
{
if (\$big_b < \$big_a)
{
\$big_b = \$big_a;
}
\$big_a = \$arr[\$i];
}
else if (\$big_b < \$arr[\$i])
{
\$big_b = \$arr[\$i];
}
//Find two small numbers
//This calculations are used when number is negative
if (\$small_a > \$arr[\$i])
{
if (\$small_b > \$small_a)
{
\$small_b = \$small_a;
}
\$small_a = \$arr[\$i];
}
else if (\$small_b > \$arr[\$i])
{
\$small_b = \$arr[\$i];
}
}
if (\$small_a * \$small_b > \$big_a * \$big_b)
{
echo " Maximum product [(". \$small_a .")*(". \$small_b .")] : ". \$small_a * \$small_b ."\n";
}
else
{
echo " Maximum product [(". \$big_a .")*(". \$big_b .")] : ". (\$big_a * \$big_b) ."\n";
}
}
}

function main()
{
\$obj = new MyArray();
//Define array of integer elements
\$arr1 = array(1, 4, 8, 3, 5, 2);
//Get the size of array
\$size = count(\$arr1);
\$obj->maximum_product(\$arr1, \$size);
\$arr2 = array(6, 3, -4, -7, 2, 4);
//Get the size of array
\$size = count(\$arr2);
\$obj->maximum_product(\$arr2, \$size);
\$arr3 = array(1, 8, 2, -4, -3);
//Get the size of array
\$size = count(\$arr3);
\$obj->maximum_product(\$arr3, \$size);
}
main();``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16``````
``````/*
Node Js Program
Find a pair with maximum product in array
*/
class MyArray
{
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
//Find maximum product of given unsorted array
maximum_product(arr, size)
{
if (size < 2)
{
return;
}
process.stdout.write("\n Array :");
this.display(arr, size);
// Variable which is used to store two smallest element
var small_a = Number.MAX_VALUE;
var small_b = Number.MAX_VALUE;
// Variable which is used to store two largest element
var big_a = -Number.MAX_VALUE;
var big_b = -Number.MAX_VALUE;
for (var i = 0; i < size; i++)
{
//Find two large numbers
if (big_a < arr[i])
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr[i];
}
else if (big_b < arr[i])
{
big_b = arr[i];
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr[i])
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr[i];
}
else if (small_b > arr[i])
{
small_b = arr[i];
}
}
if (small_a * small_b > big_a * big_b)
{
process.stdout.write(" Maximum product [(" + small_a + ")*(" + small_b + ")] : " + small_a * small_b + "\n");
}
else
{
process.stdout.write(" Maximum product [(" + big_a + ")*(" + big_b + ")] : " + (big_a * big_b) + "\n");
}
}
}

function main()
{
var obj = new MyArray();
//Define array of integer elements
var arr1 = [1, 4, 8, 3, 5, 2];
//Get the size of array
var size = arr1.length;
obj.maximum_product(arr1, size);
var arr2 = [6, 3, -4, -7, 2, 4];
//Get the size of array
size = arr2.length;
obj.maximum_product(arr2, size);
var arr3 = [1, 8, 2, -4, -3];
//Get the size of array
size = arr3.length;
obj.maximum_product(arr3, size);
}
main();``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16``````
``````import sys

#   Python 3 Program
#   Find a pair with maximum product in array

class MyArray :
# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1

print("\n", end = "")

# Find maximum product of given unsorted array
def maximum_product(self, arr, size) :
if (size < 2) :
return

print("\n Array :", end = "")
self.display(arr, size)
#  Variable which is used to store two smallest element
small_a = sys.maxsize
small_b = sys.maxsize
#  Variable which is used to store two largest element
big_a = -sys.maxsize
big_b = -sys.maxsize
i = 0
while (i < size) :
# Find two large numbers
if (big_a < arr[i]) :
if (big_b < big_a) :
big_b = big_a

big_a = arr[i]

elif(big_b < arr[i]) :
big_b = arr[i]

# Find two small numbers
# This calculations are used when number is negative
if (small_a > arr[i]) :
if (small_b > small_a) :
small_b = small_a

small_a = arr[i]

elif(small_b > arr[i]) :
small_b = arr[i]

i += 1

if (small_a * small_b > big_a * big_b) :
print(" Maximum product [(", small_a ,")*(", small_b ,")] : ", small_a * small_b ,"\n", end = "")
else :
print(" Maximum product [(", big_a ,")*(", big_b ,")] : ", (big_a * big_b) ,"\n", end = "")

def main() :
obj = MyArray()
# Define array of integer elements
arr1 = [1, 4, 8, 3, 5, 2]
# Get the size of array
size = len(arr1)
obj.maximum_product(arr1, size)
arr2 = [6, 3, -4, -7, 2, 4]
# Get the size of array
size = len(arr2)
obj.maximum_product(arr2, size)
arr3 = [1, 8, 2, -4, -3]
# Get the size of array
size = len(arr3)
obj.maximum_product(arr3, size)

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

#### Output

`````` Array :  1  4  8  3  5  2
Maximum product [( 8 )*( 5 )] :  40

Array :  6  3  -4  -7  2  4
Maximum product [( -7 )*( -4 )] :  28

Array :  1  8  2  -4  -3
Maximum product [( 8 )*( 2 )] :  16``````
``````#   Ruby Program
#   Find a pair with maximum product in array

class MyArray

# Function which is display array elements
def display(arr, size)

i = 0
while (i < size)

print(" ", arr[i])
i += 1
end
print("\n")
end
# Find maximum product of given unsorted array
def maximum_product(arr, size)

if (size < 2)

return
end
print("\n Array :")
self.display(arr, size)
#  Variable which is used to store two smallest element
small_a = (2 ** (0. size * 8 - 2))
small_b = (2 ** (0. size * 8 - 2))
#  Variable which is used to store two largest element
big_a = -(2 ** (0. size * 8 - 2))
big_b = -(2 ** (0. size * 8 - 2))
i = 0
while (i < size)

# Find two large numbers
if (big_a < arr[i])

if (big_b < big_a)

big_b = big_a
end
big_a = arr[i]
elsif(big_b < arr[i])

big_b = arr[i]
end
# Find two small numbers
# This calculations are used when number is negative
if (small_a > arr[i])

if (small_b > small_a)

small_b = small_a
end
small_a = arr[i]
elsif(small_b > arr[i])

small_b = arr[i]
end
i += 1
end
if (small_a * small_b > big_a * big_b)

print(" Maximum product [(", small_a ,")*(", small_b ,")] : ", small_a * small_b ,"\n")
else

print(" Maximum product [(", big_a ,")*(", big_b ,")] : ", (big_a * big_b) ,"\n")
end
end
end
def main()

obj = MyArray.new()
# Define array of integer elements
arr1 = [1, 4, 8, 3, 5, 2]
# Get the size of array
size = arr1.length
obj.maximum_product(arr1, size)
arr2 = [6, 3, -4, -7, 2, 4]
# Get the size of array
size = arr2.length
obj.maximum_product(arr2, size)
arr3 = [1, 8, 2, -4, -3]
# Get the size of array
size = arr3.length
obj.maximum_product(arr3, size)
end
main()``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16
``````
``````/*
Scala Program
Find a pair with maximum product in array
*/
class MyArray
{
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
//Find maximum product of given unsorted array
def maximum_product(arr: Array[Int], size: Int): Unit = {
if (size < 2)
{
return;
}
print("\n Array :");
display(arr, size);
// Variable which is used to store two smallest element
var small_a: Int = Int.MaxValue;
var small_b: Int = Int.MaxValue;
// Variable which is used to store two largest element
var big_a: Int = Int.MinValue;
var big_b: Int = Int.MinValue;
var i: Int = 0;
while (i < size)
{
//Find two large numbers
if (big_a < arr(i))
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr(i);
}
else if (big_b < arr(i))
{
big_b = arr(i);
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr(i))
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr(i);
}
else if (small_b > arr(i))
{
small_b = arr(i);
}
i += 1;
}
if (small_a * small_b > big_a * big_b)
{
print(" Maximum product [(" + small_a + ")*(" + small_b + ")] : " + small_a * small_b + "\n");
}
else
{
print(" Maximum product [(" + big_a + ")*(" + big_b + ")] : " + (big_a * big_b) + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
//Define array of integer elements
var arr1: Array[Int] = Array(1, 4, 8, 3, 5, 2);
//Get the size of array
var size: Int = arr1.length;
obj.maximum_product(arr1, size);
var arr2: Array[Int] = Array(6, 3, -4, -7, 2, 4);
//Get the size of array
size = arr2.length;
obj.maximum_product(arr2, size);
var arr3: Array[Int] = Array(1, 8, 2, -4, -3);
//Get the size of array
size = arr3.length;
obj.maximum_product(arr3, size);
}
}``````

#### Output

`````` Array : 1 4 8 3 5 2
Maximum product [(8)*(5)] : 40

Array : 6 3 -4 -7 2 4
Maximum product [(-7)*(-4)] : 28

Array : 1 8 2 -4 -3
Maximum product [(8)*(2)] : 16``````
``````/*
Swift Program
Find a pair with maximum product in array
*/
class MyArray
{
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//Find maximum product of given unsorted array
func maximum_product(_ arr: [Int], _ size: Int)
{
if (size < 2)
{
return;
}
print("\n Array :", terminator: "");
self.display(arr, size);
// Variable which is used to store two smallest element
var small_a: Int = Int.max;
var small_b: Int = Int.max;
// Variable which is used to store two largest element
var big_a: Int = Int.min;
var big_b: Int = Int.min;
var i: Int = 0;
while (i < size)
{
//Find two large numbers
if (big_a < arr[i])
{
if (big_b < big_a)
{
big_b = big_a;
}
big_a = arr[i];
}
else if (big_b < arr[i])
{
big_b = arr[i];
}
//Find two small numbers
//This calculations are used when number is negative
if (small_a > arr[i])
{
if (small_b > small_a)
{
small_b = small_a;
}
small_a = arr[i];
}
else if (small_b > arr[i])
{
small_b = arr[i];
}
i += 1;
}
if (small_a * small_b > big_a * big_b)
{
print(" Maximum product [(", small_a ,")*(", small_b ,")] : ", small_a * small_b ,"\n", terminator: "");
}
else
{
print(" Maximum product [(", big_a ,")*(", big_b ,")] : ", (big_a * big_b) ,"\n", terminator: "");
}
}
}
func main()
{
let obj: MyArray = MyArray();
//Define array of integer elements
let arr1: [Int] = [1, 4, 8, 3, 5, 2];
//Get the size of array
var size: Int = arr1.count;
obj.maximum_product(arr1, size);
let arr2: [Int] = [6, 3, -4, -7, 2, 4];
//Get the size of array
size = arr2.count;
obj.maximum_product(arr2, size);
let arr3: [Int] = [1, 8, 2, -4, -3];
//Get the size of array
size = arr3.count;
obj.maximum_product(arr3, size);
}
main();``````

#### Output

`````` Array :  1  4  8  3  5  2
Maximum product [( 8 )*( 5 )] :  40

Array :  6  3  -4  -7  2  4
Maximum product [( -7 )*( -4 )] :  28

Array :  1  8  2  -4  -3
Maximum product [( 8 )*( 2 )] :  16``````

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 