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


Please Help Members By Posting Answers For Below Questions

write a program to perform generic sort in arrays?

2631


write a program using 2 D that searches a number and display the number of items 12 inputs values input 15,20, 13, 30, 38, 40,16, 18, 20 ,18 ,20 enter no. to search : 20

3368


Question 1: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date. *This Should Be Done IN C++

554


What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }

644


write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n

2370






Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

2067


Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?

2565


Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

4579


i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.

1972


develop a program to calculate and print body mass index for 200 employees

2216


Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?

2915


write a program that reads a series of strings and prints only those strings begging with letter "b"

2674


write a program to convert temperature from fa height into celcius and vise versa,use modular programming

2443


Teta-Omeg-Big-Oh Show that f(n) = n2 + 3n3 is ;(n3).

3195


write a program that reverses the input number of n.Formulate an equation to come up with the answer.

7043