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?
find level of following tree (state, parent) " J,D I,D H,C E,B F,B G,C B,A D,A C,A A,& K,E L,E L,F M,F N,G O,H P,I P,H Q,I R,J S,K U,P T,L
How to swap two ASCII numbers?
i don't know about working of nested for loop can any one help me
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?
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
Code for Easily Using Hash Table?
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
write a program that reads a series of strings and prints only those strings begging with letter "b"
Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?
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+……………...
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?
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
write a program that can LOCATE and INSERT elements in array using c++ programming languages.
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