Find maximum product of 3 numbers in an array

Problem statement:- Program to Find the maximum product of 3 numbers in an array.

Example:-

              Input : Given

                                 size=5

                                 arr[]=[9,1,8,11,13] 

             Output:  Pair of Maximum Product=(9, 11, 13)

                           Maximum Product of 3 numbers= 1287                

Data requirement:-


   Input Data:- size, arr

  Output Data:-First_element,Second_elementthird_element, Max_Product

  Additional Data:- i,j,k,product

Program in C

Here is the source code of the C Program to Find the maximum product of 3 numbers in an array.

Code:

#include<stdio.h>
#include<math.h>
#include<limits.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,j,k;
    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],third_element=arr[2],product,Max_Product=INT_MIN;

   for(i=0;i<size-2;i++)
   {

       for(j=i+1;j<size-1;j++)
       {
           for(k=j+1;k<size;k++)
         {
            product=arr[i]*arr[j]*arr[k];
            if(abs(product)>=abs(Max_Product))
            {
                Max_Product=product;
                First_element=arr[i];
                Second_element=arr[j];
                third_element=arr[k];
            }
       }
   }}
   printf("Maximum Product of 3 numbers pair is (%d, %d, %d)\n", First_element,Second_element,third_element);
   printf("Maximum Product of 3 numbers is %d", Max_Product);
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
9 1 8 11 13
Maximum Product of 3 numbers pair is (9, 11, 13)
Maximum Product of 3 numbers is 1287

Program in C++

Here is the source code of the C++ Program to Find the maximum product of 3 numbers 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,k;
    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],third_element=arr[2],product,Max_Product=First_element*Second_element*third_element;
   for(i=0;i<size-2;i++)
   {

       for(j=i+1;j<size-1;j++)
       {
           for(k=j+1;k<size;k++)
         {
            product=arr[i]*arr[j]*arr[k];
            if(abs(product)>=abs(Max_Product))
            {
                Max_Product=product;
                First_element=arr[i];
                Second_element=arr[j];
                third_element=arr[k];
            }
       }
   }}
   cout<<"Maximum Product of 3 numbers pair is ("<<First_element<<","<<Second_element<<","<<third_element<<")";
   cout<<"\nMaximum Product of 3 numbers is "<<Max_Product;
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
10 5 3 4
Maximum Product of 3 numbers pair is (10,5,4)
Maximum Product of 3 numbers is 200

Program in Java

Here is the source code of the Java Program to Find the maximum product of 3 numbers in an array.

Code:

import java.util.Scanner;
public class MaximumProductOfThreeNumber {

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,k;
     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],third_element=arr[2],product,Max_Product=First_element*Second_element*third_element;

      for(i=0;i<size-2;i++)
      {

          for(j=i+1;j<size-1;j++)
          {
              for(k=j+1;k<size;k++)
            {
               product=arr[i]*arr[j]*arr[k];
               if(Math.abs(product)>=Math.abs(Max_Product))
               {
                   Max_Product=product;
                   First_element=arr[i];
                   Second_element=arr[j];
                   third_element=arr[k];
               }
          }
      }}
      System.out.println("Maximum Product of 3 numbers pair is ("+First_element+","+Second_element+","+third_element+")");
      System.out.println("Maximum Product of 3 numbers is "+Max_Product);
      sc.close();
}}

Input/Output:
Enter the size of the array:
8
Enter the Element of the array:
10 5 20 13 14 6 2 1
Maximum Product of 3 numbers pair is (20,13,14)
Maximum Product of 3 numbers is 3640

Program in Python

Here is the source code of the Python Program to Find the maximum product of 3 numbers 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]
third_element=arr[2]
Max_Product=First_element*Second_element*third_element

for i in range(0,size-2):
    for j in range(i+1, size-1):
        for k in range(j + 1, size):
            product=arr[i]*arr[j]*arr[k]
            if abs(product) >= abs(Max_Product):
                Max_Product =product
                First_element = arr[i]
                Second_element = arr[j]
                third_element=arr[k]

print("Maximum Product of 3 numbers pair is (",First_element,",",Second_element,",",third_element,")")
print("Maximum Product of 3 numbers is ",Max_Product)

Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
33
3
34
4
Maximum Product of 3 numbers pair is ( 33 , 34 , 4 )
Maximum Product of 3 numbers is  4488


Post a Comment

0 Comments