Find the maximum product of three numbers in an array?
Eg. 9,5,1,2,3
Max product= 9*5*3= 135
The array can hav negative numbers also..
Answer Posted / srikanthgowda
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int i,j,a[100];
cout<<"Enter the size of array"<<endl;
int n;
cin>>n;
if(n==0)
{
cout<<"invaild!!"<<endl;
exit(1);
}
cout<<"Enter the element for array"<<endl;
for ( i=0;i<n;i++)
cin>>a[i];
int lr=0;
if(n==1)
{
cout<<"only one array is present!!"<<a[0]<<endl;
exit(0);
}
else if(n==2)
{
if(a[0]<a[1])
{
cout<<a[1]<<"largest number";
exit(0);
}
else
{
cout<<a[0]<<"largestn number";
exit(0);
}
}
else if(n==3)
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]*a[j]>lr)
{
lr=a[i]*a[j];
}
}
}
cout<<"max product="<<lr<<endl;
}
else
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(a[i]*a[j]*a[k]>lr)
{
lr=a[i]*a[j]*a[k];
}
}
}
}
cout<<"max product="<<lr<<endl;
}
return 0 ;
}
/* Ouput
1.
Enter the size of array
5
Enter the element for array
9
5
1
2
3
max product=135
2.
srikanth@srikanth-System-Product-Name:~/c++$ ./a.out
Enter the size of array
5
Enter the element for array
-5
3
1
2
4
max product=24
srikanth@srikanth-System-Product-Name:~/c++$
*/
Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)
1+1/2!+1/3!+...+1/n!
write a program to convert temperature from fa height into celcius and vise versa,use modular programming
output for printf("printf");
Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?
How can I Draw an ellipse in 3d space and color it by using graph3d?
write a program using virtual function to find the transposing of a square matrix?
write a program to perform generic sort in arrays?
How to swap two ASCII numbers?
write a program that creates a sequenced array of numbers starting with 1 and alternately add 1 and then 2 to create the text number in the series , as shown below. 1,33,4,6,7,9,............147,148,150 Then , using a binary search , searches the array 100 times using randomly generated targets in the range of 1 to 150
Create a program to read two random data set in two files named data1.txt and data2.txt manifold contains integer numbers, whereas data2.txt file contains the float type numbers. Simpanlahmasing each into 2 pieces of data that is an array of type integer array and an array of type float, then calculate the average numbers in the second array.
create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file
write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used
write a program that reads a series of strings and prints only those strings begging with letter "b"
write a function that allocates memory for a single data type passed as a parameter.the function uses the new operator and return a pointer to the allocated memory.the function must catch and handle any exception during allocation