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

/*The maximum value of the product fr a set of 3 numbers positiv or negative can be
->either the product of 2 negative numbers( the ones with highest absolut values) and 1(biggest) positive number
->or product of three biggest positive numbers
->zero if all other numbers (except that 0) ar negativ
-> product of smallest 3 negativ number if all numbers are negative
>>>
<you can try it for ANY POSSIBLE VALUES..in th array and see..>
<<<<
*/

procedure
I)
so first 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 set a boolean value of a variabl ISZERO as TRUE

II) IF (neg[] has zero or only 1 element) THEN

/* product of the three highest values in pos[] (ie the first three values) is the answer*/

RETURN (pos[0]*pos[1]*pos[2]);

III) ELSE IF pos[] is empty
/*(meaning there are only negative values..multiply the smallest three negative numbers (ie say -1,-2,-3,-4,-5 means multiply -1,-2,-3 you get -6)..that is the answer */


if pos[] is NULL AND if there is a zero then
RETURN ZERO;
else //if pos[] is null and no element is zero then
RETURN neg[neg.length-1]*neg[neg.length-2]*neg[neg.length-3]

/*
//NOTE:

there are only negative values and zeros
highest value is 0
check that too from ISZERO variabl..
*/

IV)ONLY if neg[] array has more than 1 value THEN
multiply th biggest 2 negativ absolute numbers


/*
//(eg {-9,-7,-6,-5 } means...tak -9 and -7..so product is 63)

V) multiply th above result with th highest postiv value from positive array...(ie pos[])

VI) compare this value got in STEP FIVE with product of the three highest values in pos[](ie product of first three values) which ever is greate is the answer
ie
*/

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])


IT IS VERY EASY TO CODE THIS BECAUSE THE STEPS ARE ALMOST IN PSUEDOCODE....hope it helps...

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

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

4310


how to diplay a external image of output on winxp by using c & c++,

2938


We need to write the function to check the password entered is correct or not based on the following conditions.. a) It must have atleast one lower case character and one digit. b)It must not have any Upper case characters and any special characters c) length should be b/w 5-12. d) It should not have any same immediate patterns like abcanan1 : not acceptable coz of an an pattern abc11se: not acceptable, coz of pattern 11 123sd123 : acceptable, as not immediate pattern adfasdsdf : not acceptable, as no digits Aasdfasd12: not acceptable, as have uppercase character

3957


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

4579


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






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


i don't know about working of nested for loop can any one help me

1794


How can I Draw an ellipse in 3d space and color it by using graph3d?

2138


Code for Method of Handling Factorials of Any Size?

2008


write a program that creates a sequenced array of numbers starting with 1 and alternately add 1 and then 2 to create the text number in the series , as shown below. 1,33,4,6,7,9,............147,148,150 Then , using a binary search , searches the array 100 times using randomly generated targets in the range of 1 to 150

3270


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

7043


Write a (n) algorithm that sorts n distinct integers, ranging in size between 1 and kn inclusive, where k is a constant positive integer. (Hint: Use a kn-element array.)

4453


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 program using virtual function to find the transposing of a square matrix?

2845


write a program to perform generic sort in arrays?

2631