Posted on by Kalkicode
Code Array

Find a pair with maximum product in array

The problem of finding a pair with the maximum product in an array involves identifying two elements whose product results in the highest possible value. This problem has practical applications in various domains, including optimization, financial modeling, and data analysis. In this article, we will explore the steps to solve this problem, mentioned a clear approach, explain the algorithm, analyze its time complexity, and present concise code examples.

Problem Statement

Given an array of integers, the task is to find and display a pair of elements that, when multiplied, produce the maximum product.

Example

Consider the array: Array: [1, 4, 8, 3, 5, 2]

The pair with the maximum product is (8, 5), as 8 * 5 equals 40.

Idea to Solve the Problem

To solve this problem, we need to identify the two largest positive integers and the two smallest negative integers (if present) in the array. The maximum product can be obtained either by multiplying the two largest positive integers or by multiplying the two smallest negative integers with the largest positive integer.

Pseudocode

maximum_product(arr, size):
    if size < 2:
        return
    
    small_a = INT_MAX
    small_b = INT_MAX
    big_a = INT_MIN
    big_b = INT_MIN
    
    for i from 0 to size:
        if arr[i] > big_a:
            if big_b < big_a:
                big_b = big_a
            big_a = arr[i]
        else if arr[i] > big_b:
            big_b = arr[i]
        
        if arr[i] < small_a:
            if small_b > small_a:
                small_b = small_a
            small_a = arr[i]
        else if arr[i] < small_b:
            small_b = arr[i]
    
    if small_a * small_b > big_a * big_b:
        print("Maximum product:", small_a, "*", small_b, "=", small_a * small_b)
    else:
        print("Maximum product:", big_a, "*", big_b, "=", big_a * big_b)

Algorithm Explanation

  1. Initialize variables to track the two smallest integers (small_a, small_b) and the two largest integers (big_a, big_b).
  2. Iterate through the array:
    • Update the largest integers if the current element is greater than big_a. If it's greater than big_b but smaller than big_a, update big_b.
    • Update the smallest integers if the current element is smaller than small_a. If it's smaller than small_b but larger than small_a, update small_b.
  3. Compare the products of the two largest positive integers and the products of the two smallest negative integers with the largest positive integer. Print the maximum product.

Code Solution

// 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[0]);
	maximum_product(arr1, size);
	int arr2[] = {
		6 , 3 , -4 , -7 , 2 , 4
	};
	//Get the size of array
	size = sizeof(arr2) / sizeof(arr2[0]);
	maximum_product(arr2, size);
	int arr3[] = {
		1 , 8 , 2 , -4 , -3
	};
	//Get the size of array
	size = sizeof(arr3) / sizeof(arr3[0]);
	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[0]);
	obj.maximum_product(arr1, size);
	int arr2[] = {
		6 , 3 , -4 , -7 , 2 , 4
	};
	//Get the size of array
	size = sizeof(arr2) / sizeof(arr2[0]);
	obj.maximum_product(arr2, size);
	int arr3[] = {
		1 , 8 , 2 , -4 , -3
	};
	//Get the size of array
	size = sizeof(arr3) / sizeof(arr3[0]);
	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

Time Complexity Analysis

The algorithm iterates through the array once, performing constant-time operations at each step. Therefore, the time complexity is O(n), where n is the size of the array. The algorithm provides efficient performance for arrays of moderate size.

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.

New Comment