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

Answers were Sorted based on User's Feedback



Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

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

Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

Answer / sathish

Just count the number of -ve symbols in the array before sorting.

CODE:
void main()
{
clrscr();
int i,j,count=0,a[5]={10,2,-7,-5,3};
for(i=0;i<5;i++)
if(a[i]<0)
count=count+1;
if((count%2)==0)
{
for(i=0;i<5;i++)
{

if(a[i]<0)
a[i]=-a[i];
}
}
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int mul=1;
for(i=0;i<3;i++)
{
mul=mul*a[i];
}
cout<<mul;
getch();
}

Is This Answer Correct ?    9 Yes 8 No

Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

Answer / srikanthgowda

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
int i,j,a[100];
cout<<"Enter the size of array"<<endl;
int n;
cin>>n;


if(n==0)
{
cout<<"invaild!!"<<endl;
exit(1);
}

cout<<"Enter the element for array"<<endl;
for ( i=0;i<n;i++)
cin>>a[i];


int lr=0;
if(n==1)
{
cout<<"only one array is present!!"<<a[0]<<endl;
exit(0);
}
else if(n==2)
{
if(a[0]<a[1])
{
cout<<a[1]<<"largest number";
exit(0);
}

else
{
cout<<a[0]<<"largestn number";
exit(0);
}
}
else if(n==3)
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]*a[j]>lr)
{
lr=a[i]*a[j];

}
}
}

cout<<"max product="<<lr<<endl;
}



else
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(a[i]*a[j]*a[k]>lr)
{
lr=a[i]*a[j]*a[k];

}
}
}
}

cout<<"max product="<<lr<<endl;
}
return 0 ;
}


/* Ouput

1.

Enter the size of array
5
Enter the element for array
9
5
1
2
3
max product=135

2.

srikanth@srikanth-System-Product-Name:~/c++$ ./a.out
Enter the size of array
5
Enter the element for array
-5
3
1
2
4
max product=24
srikanth@srikanth-System-Product-Name:~/c++$

*/

Is This Answer Correct ?    1 Yes 0 No

Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

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

Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

Answer / jack

#include"stdafx.h"
#include<iostream>


using namespace std;
void sort(int[],int );
int maxproduct(int ar[],int n);
int main()
{
int arr[]={2,7,9,5,3,6,-12};

cout<<maxproduct(arr,7);
system("calc");


system("pause");
return 0;
}
void sort(int array1[],int n)
{
for(int i=1;i<n;i++)
for(int r=0;r<n-1;r++)

if(array1[r]>array1[r+1])
{
int temp=array1[r];
array1[r]=array1[r+1];
array1[r+1]=temp;
}

}
int maxproduct(int ar[],int n)
{
sort(ar,n);
int max=1;
for(int i=n-1;i>=n-3;i--)
max*=ar[i];
return max;
}

Is This Answer Correct ?    1 Yes 1 No

Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

Answer / bharghavi

consider the array: 10,2,-7,-5,3
Descending order: 10,3,2,-5,-7
Then ur result will give: 10*3*2=60
But the max. product can be: 10*(-5)*(-7) =350

Is This Answer Correct ?    4 Yes 8 No

Find the maximum product of three numbers in an array? Eg. 9,5,1,2,3 Max product= 9*5*3= 135 The..

Answer / sathish

1)Sort the array in descending order.
2)Multiply for required number of indices.

CODE:

int mul=1;
for(i=0;i<3;i++)
{
mul=mul*a[i];
}
cout<<mul;

Is This Answer Correct ?    11 Yes 16 No

Post New Answer

More C++ Code Interview Questions

using friend function find the maximum number from given two numbers from two different classes.write all necessary functions and constructor for the classes.

1 Answers   TCS,


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?

0 Answers   ASD Lab, Qatar University, UNV,


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

0 Answers  


An array of size 5X5 is given to us. The elements from 1 to 25 are to be inserted in the array, such that starting from a particular position for an element i, the next element i+1can be inserted only at the mentioned positions (u,v), and if these all positions are occupied then it returns giving a count of how many positions have been occupied in the array: (u,v) = (x+/-3 , y) (u,v) = (x , y+/-3) (u,v) = (x+/-2 , y+/-2). Example: if the starting element is 1 with the given positions (1,2), then next element 2 can be placed at any one of the positions marked with *. _ _ _ _ _ 1 _ _ _ * _ _ _ _ _ _ _ * _ _ * _ _ _ _

0 Answers   Nagarro,


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?

0 Answers  






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.

0 Answers  


Faster Computers Suppose you have a computer that requires 1 minute to solve problem instances of size 1000. What instance sizes can be run in 1 minute if you buy a new computer that runs 1000 times faster than the old one, assuming the following time complexities T(n) for our algorithm? (a) T(n) = O(n). (b) T(n) = O(n3). (c) T(n) = O(10n).

1 Answers   Qatar University,


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

0 Answers  


Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, or if it is not prime find out its factors.

5 Answers   ADP, Amazon, HCL, IBM, Infosys, Satyam, TCS, Vimukti Technologies,


what is the diffrence between ++x , x++ pleaaaaase ???

7 Answers  


write a function – oriented program that calculates the sum of the squares from 1 to n. thus, if the input is 3, the output is 14

3 Answers  


how to write a program that opens a file and display in reverse order?

0 Answers   SMC,


Categories