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
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
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
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?
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.
How to Split Strings with Regex in Managed C++ Applications?
write a program using virtual function to find the transposing of a square matrix?
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
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; }
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
write a function that reverse the elements of an array in place.The function must accept only one pointer value and return void.
1+1/2!+1/3!+...+1/n!
write a program that reads a series of strings and prints only those strings begging with letter "b"
develop a program to calculate and print body mass index for 200 employees
A suduco given & u hv 2 check if it is incomplete(blanks left),or correct or incorrect
how to take time as input in the format (12:02:13) from user so that controls remains between these columns?