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
how to write a program that opens a file and display in reverse order?
output for printf("printf");
Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)
how to diplay a external image of output on winxp by using c & c++,
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.
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.
i don't know about working of nested for loop can any one help me
write a program to convert temperature from fa height into celcius and vise versa,use modular programming
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
A suduco given & u hv 2 check if it is incomplete(blanks left),or correct or incorrect
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+……………...
develop a program to calculate and print body mass index for 200 employees
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.
write a program that can LOCATE and INSERT elements in array using c++ programming languages.
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