Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
 Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE

 Categories  >>  Code Snippets  >>  Programming Code  >>  C++ Code

 C Code Interview Questions C++ Code Interview Questions VC++ Code Interview Questions Java Code Interview Questions Dot Net Code Interview Questions Visual Basic Code Interview Questions Programming Code AllOther Interview Questions
Question
```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..```
Question Submitted By :: Bharghavi90
I also faced this Question!!     Rank Answer Posted By

Re: 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..
# 1
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 ? 9 Yes 13 No
Sathish

Re: 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..
# 2
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 ? 3 Yes 8 No
Bharghavi

Re: 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..
# 3
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 ? 8 Yes 7 No
Sathish

Re: 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..
# 4
/*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 ? 3 Yes 0 No
Utkarsh

Re: 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..
# 5
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
Utkarsh

Re: 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..
# 6
#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
Jack

  Question Asked @ Answers Other C++ Code Interview Questions How reader and writer problem was implemented and come up with effective solution for reader and writer problem in case we have n readers and 1 writer. NetApp 5 Where now stands that small knot of villages known as the Endians, a mighty forest once stood. Indeed, legand has it that you could have stoodon the edge of the wood and seen it stretch out for miles, were it not for the trees getting in the way. In one section of the forest, the trees stood in a row and were of hight from 1 to n, each hight occurring once and once only. A tree was only visible if there were no higher trees before it in the row. For example, if the heights were 324165, the only visible trees would have been those of height 3,4 & 6. Write a Program that takes an array of integers representing the heights of the trees in the row as input and prints the list of the visible trees. Nagarro 2 what is virtual constroctor ? give an exam for it?-(parimal dhimmar) 2 what is the best algorithm to sort out unique words from a list of more than 10 million words(1 crore+)? we need the best technique in the terms of execution time. TCS 9 . Write a program using two-dimensional arrays that computes the sum of data in tows and the sum of data in columns of the 3x3 (three by three) array variable n[3][3]. IBM 2 write a program that can locate elements in array. 1 what is the difference between int &r and int& r 3 Display Pattern: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 … 2 A Binary no. is given, we hav to find it's decimal equivalent. Microsoft 2 void main() { int i,j=2; for(i=0;i<3;i++) if(j=i) cout<<"Lotus "; else cout<<"Rose "; } Its result is Rose Lotus Lotus.. How? Explain it? 2 Write a program using two-dimensional arrays that determines the highest and lowest of the 12 input values. Example: Enter 12 numbers: 13 15 20 13 35 40 16 18 20 18 20 14 highest: 40 lowest: 13 1 Write a function- oriented to convert the input dollar(s) into its equivalent peso. Assume that one dollar is equivalent to 51.60 1 For more C++ Code Interview Questions Click Here