Question { CTS, 22124 }
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
#include
#include
using namespace std;
int main()
{
int i,j,a[100];
cout<<"Enter the size of array"<
int n;
cin>>n;
if(n==0)
{
cout<<"invaild!!"<
exit(1);
}
cout<<"Enter the element for array"<
for ( i=0;i
cin>>a[i];
int lr=0;
if(n==1)
{
cout<<"only one array is present!!"<
exit(0);
}
else if(n==2)
{
if(a[0]
{
cout<
exit(0);
}
else
{
cout<
exit(0);
}
}
else if(n==3)
{
for(i=0;i
{
for(j=i+1;j
{
if(a[i]*a[j]>lr)
{
lr=a[i]*a[j];
}
}
}
cout<<"max product="<
}
else
{
for(i=0;i
{
for(j=i+1;j
{
for(int k=j+1;k
{
if(a[i]*a[j]*a[k]>lr)
{
lr=a[i]*a[j]*a[k];
}
}
}
}
cout<<"max product="<
}
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++$
*/