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

2364


output for printf("printf");

1977


Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?

2555


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?

2905


what mean void creat_object?in public class in this code class A{ public: int x; A(){ cout << endl<< "Constructor A";} ~A(){ cout << endl<< "Destructor A, x is\t"<< x;} }; void create_object(); void main() { A a; a.x=10; { A c; c.x=20; } create_object(); } void create_object() { A b; b.x=30; }

2061






1+1/2!+1/3!+...+1/n!

1939


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

3112


Definition of priority queue was given. We have to implement the priority queue using array of pointers with the priorities given in the range 1..n. The array could be accessed using the variable top. The list corresponding to the array elements contains the items having the priority as the array index. Adding an item would require changing the value of top if it has higher priority than top. Extracting an item would require deleting the first element from the corresponding queue. The following class was given: class PriorityQueue { int *Data[100]; int top; public: void put(int item, int priority); // inserts the item with the given priority. int get(int priority); // extract the element with the given priority. int count(); // returns the total elements in the priority queue. int isEmpty(); // check whether the priority queue is empty or not. }; We had to implement all these class functions.

4389


What output does this program generate as shown? Why? class A { A() { cout << "A::A()" << endl; } ~A() { cout << "A::~A()" << endl; throw "A::exception"; } }; class B { B() { cout << "B::B()" << endl; throw "B::exception"; } ~B() { cout << "B::~B()"; } }; int main(int, char**) { try { cout << "Entering try...catch block" << endl; A objectA; B objectB; cout << "Exiting try...catch block" << endl; } catch (char* ex) { cout << ex << endl; } return 0; }

575


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+……………...

4345


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

2434


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

3949


write a program that can LOCATE and INSERT elements in array using c++ programming languages.

3426


Write a simple encryption program using string function which apply the substitution method.

5529


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.

2061