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 / utkarsh

IF ARRAY HAS LESS THAN 3 ELEMENTS
{
THROW ERROR
}

ELSE
{
sort the numbers in 2 arrays..
neg[]-> with negativ values in descending order
pos[]-> with positive values in descending order
->if there is a zero THEN
set a boolean value of a variabl ISZERO as TRUE
}


IF pos[] is empty
{
IF ISZERO==true THEN
// if there are only negativ values and a zero
RETURN ZERO;

ELSE
// all elements are -ve and no element is zero then
RETURN (neg[neg.length-1]*neg[neg.length-2]*neg[neg.length-3]);

}

IF (neg[] has zero or only 1 element) THEN
{
RETURN (pos[0]*pos[1]*pos[2]);
}
ELSE IF neg[] array has more than 1 value THEN
{
IF (neg[0]*neg[1]*pos[0]) > (pos[0]*pos[1]*pos[2]) THEN
RETURN (neg[0]*neg[1]*pos[0]);
ELSE
RETURN (pos[0]*pos[1]*pos[2]);

}


FOR DETAILS ABT LOGIC READ ABOVE POST

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to Split Strings with Regex in Managed C++ Applications?

3316


Create a program to read two random data set in two files named data1.txt and data2.txt manifold contains integer numbers, whereas data2.txt file contains the float type numbers. Simpanlahmasing each into 2 pieces of data that is an array of type integer array and an array of type float, then calculate the average numbers in the second array.

2368


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

2292


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

2682


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

2592


write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used

4597


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

4770


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.

2296


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.

3624


How to swap two ASCII numbers?

2661


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

2220


output for printf("printf");

2208


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

2629


solve the problem in the programming language C++"if a five digit number is input through the keyboard.Write a program to calculate the sum of its digits(hint: use the modulus operator)

3156


create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file

2445