Problem statement:- Program to Find a pair with maximum product in an array.
Example:-
Input : Given
size=5
arr[]=[9,1,8,11,13]
Output: Pair of Maximum Product=(11, 13)
Maximum Product of 2 numbers= 143
Data requirement:-
Input Data:- size, arr
Output Data:-First_element,Second_element, Max_Product
Additional Data:- i,j,product
Program in C
Here is the source code of the C Program to Find a pair with maximum product in an array.
Code:
#include<stdio.h>
#include<math.h>
main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size];
int i,j;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
int First_element=arr[0], Second_element=arr[1],product,Max_Product=First_element*Second_element;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
product=arr[i]*arr[j];
if(abs(product)>abs(Max_Product))
{
Max_Product=product;
First_element=arr[i];
Second_element=arr[j];
}
}
}
printf("Pair of Maximum Product is (%d, %d)\n", First_element,Second_element);
printf("Maximum Product of 2 numbers is %d", Max_Product);
}
Enter the size of the array:5
Enter the Element of the array:
9 1 8 11 13
Pair of Maximum Product is (11, 13)
Maximum Product of 2 numbers is 143
Program in C++
Here is the source code of the C++ Program to Find a pair with maximum product in an array.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size];
int i,temp=0,j,p;
cout<<"Enter the Element of the array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
int First_element=arr[0], Second_element=arr[1],product,Max_Product=First_element*Second_element;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
product=arr[i]*arr[j];
if(abs(product)>abs(Max_Product))
{
Max_Product=product;
First_element=arr[i];
Second_element=arr[j];
}
}
}
cout<<"Pair of Maximum Product is ("<<First_element<<","<<Second_element<<")";
cout<<"\nMaximum Product of 2 numbers is "<<Max_Product;
}
Enter the size of the array:6
Enter the Element of the array:
7 4 5 3 8 2
Pair of Maximum Product is (7,8)
Maximum Product of 2 numbers is 56
Program in Java
Here is the source code of the Java Program to Find a pair with maximum product in an array.
Code:
import java.util.Scanner;
public class MaximumProduct {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,i,j;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
int First_element=arr[0], Second_element=arr[1],product,Max_Product=First_element*Second_element;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
product=arr[i]*arr[j];
if(Math.abs(product)>Math.abs(Max_Product))
{
Max_Product=product;
First_element=arr[i];
Second_element=arr[j];
}
}
}
System.out.println("Pair of Maximum Product is ("+First_element+","+Second_element+")");
System.out.println("Maximum Product of 2 numbers is "+Max_Product);
sc.close();
}
}
Enter the size of the array:
8
Enter the Element of the array:
10 5 20 13 14 6 2 1
Pair of Maximum Product is (20,14)
Maximum Product of 2 numbers is 280
Program in Python
Here is the source code of the Python Program to Find a pair with maximum product in an array.
Code:
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
First_element=arr[0]
Second_element=arr[1]
Max_Product=First_element*Second_element
for i in range(0,size-1):
for j in range(i+1, size):
product = arr[i] * arr[j]
if abs(product) >= abs(Max_Product):
Max_Product =product
First_element = arr[i]
Second_element = arr[j]
print("Pair of Maximum Product is (",First_element,",",Second_element,")")
print("\nMaximum Product of 2 numbers is ",Max_Product)
Enter the size of the array: 4
Enter the Element of the array:
33
3
34
4
Pair of Maximum Product is ( 33 , 34 )
Maximum Product of 2 numbers is 1122
0 Comments
Please do not Enter any spam link in the comment box