Three Great Candidates

PROBLEM :
The hiring team of Google aims to find 3 candidates who are great collectively. Each candidate has his or her ability expressed as an integer. 3 candidate are great collectively if product of their abilities is maximum. Find the maximum collective ability from the given pool of candidates.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. First line of each test case conatins an interger N  denoting the number of candidates.
The second line of each test case contains N space separated elements denoting the ablities of candidates.

Output:
Corresponding to each test case, print the desired output(maximum collective ability of three candidates) in a newline.

Constraints:
1 = T = 100
3 = N = 1000
-1000 = ability = 1000

Example:
Input
1
6
0 -1 3 100 70 50

Output:
350000

Explanation
70*50*100 = 350000 which is the maximum possible.

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<algorithm>
long long three_greatest(int [],int ) ;
int main()
 {
int t,i,no ;
int arr[1000] ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   cout<<three_greatest(arr,no)<<endl  ;
}
return 0;
}

long long three_greatest(int arr[],int no)
{
    int i,temp[5] ;
    long long p1,p2 ;
    p1=1,p2=1 ;
   
    sort(arr,arr+no) ;
   
    temp[0]=arr[0] ;
    temp[1]=arr[1] ;
    temp[2]=arr[no-3] ;
    temp[3]=arr[no-2] ;
    temp[4]=arr[no-1] ;
   
    p1=(temp[0]*temp[1])*temp[4] ;
    p2=(temp[2]*temp[3])*temp[4] ;
   
    return p1>p2?p1:p2 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )