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 / jack
#include"stdafx.h"
#include<iostream>
using namespace std;
void sort(int[],int );
int maxproduct(int ar[],int n);
int main()
{
int arr[]={2,7,9,5,3,6,-12};
cout<<maxproduct(arr,7);
system("calc");
system("pause");
return 0;
}
void sort(int array1[],int n)
{
for(int i=1;i<n;i++)
for(int r=0;r<n-1;r++)
if(array1[r]>array1[r+1])
{
int temp=array1[r];
array1[r]=array1[r+1];
array1[r+1]=temp;
}
}
int maxproduct(int ar[],int n)
{
sort(ar,n);
int max=1;
for(int i=n-1;i>=n-3;i--)
max*=ar[i];
return max;
}
Is This Answer Correct ? | 1 Yes | 1 No |
Post New Answer View All Answers
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.
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
A suduco given & u hv 2 check if it is incomplete(blanks left),or correct or incorrect
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; }
how to diplay a external image of output on winxp by using c & c++,
create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file
Ask the user to input three positive integers M, N and q. Make the 2 dimensional array of integers with size MxN, where all the elements of I (I = 1,…,M) line will be members of geometrical progression with first element equal to the number of line (I) and denominator q.
how to write a program that opens a file and display in reverse order?
How to Split Strings with Regex in Managed C++ Applications?
output for printf("printf");
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.
3. Program to find the Sum of give series. a. (1)+(1+2)+(1+2+3)+(1+2+3+4)+……………………………….. b. 1/1+1/9+1/25+1/49+……………...
can you please write a program for deadlock that can detect deadlock and to prevent deadlock.
write a program to convert temperature from fa height into celcius and vise versa,use modular programming
write a program that can LOCATE and INSERT elements in array using c++ programming languages.