TechoSagar: Maximum product of two numbers

Search

Google Alert - jobs

Maximum product of two numbers

Given an array with all elements greater than or equal to zero.Return the maximum product of two numbers possible.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N, N is size of array.
The second line of each test case contains N input A[i].

Output:
Print the maximum product of two numbers possible.

Constraints:
1 ≤ T ≤ 20
2 ≤ N ≤ 50
0 ≤ A[i] ≤ 1000

Example:
Input
1
5
1 100 42 4 23
Output
4200
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int i, a[1000];
int n;
cin>>n;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int k, product, x;
       for(i=0; i<n-1; i++)
       {
        for(k=i+1; k<n; k++)
        {  
           if(i==0 && k==i+1)
           x=a[i]*a[k];
           else
           {
        product=a[i]*a[k];
       
        if(x<product)
        {
        x=product;
  }
           }
  }
  }
  cout<<x<<endl;
}
return 0;
}

Follow us

Follow Bijendra Kumar on Facebook