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
Code for Method of Handling Factorials of Any Size?
how to diplay a external image of output on winxp by using c & c++,
write a program using virtual function to find the transposing of a square matrix?
1+1/2!+1/3!+...+1/n!
Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?
can you please write a program for deadlock that can detect deadlock and to prevent deadlock.
How to Split Strings with Regex in Managed C++ Applications?
output for printf("printf");
Performance Algorithm A performs 10n2 basic operations and algorithm B performs 300 lg n basic operations. For what value of n does algorithm B start to show its better performance?
How to swap two ASCII numbers?
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?
Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37
how to take time as input in the format (12:02:13) from user so that controls remains between these columns?
Write a simple encryption program using string function which apply the substitution method.
Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.