ANITGUY7 - Editorial

Problem: Contest Page | CodeChef

DIFFICULTY:

EASY.

PROBLEM:

Chef has an array of N natural numbers. Cheffina challenges the chef to choose the two numbers from the array and following the condition as the area of the rectangle formed from the two numbers is maximum. Cheffina also asks the chef to choose two numbers different from the previous two to form the rectangle with a minimum area.

Program:

#include<bits/stdc++.h> 
using namespace std; 
#define ll long long
 
int main() 
{ 
	ios_base::sync_with_stdio(false);
   	cin.tie(NULL);
   	cout.tie(NULL);
   	int t;
   	cin>>t;
   	while(t--){
   		int n;
   		cin>>n;
   		long long arr[n];
   		for(int i=0;i<n;i++)cin>>arr[i];
   		sort(arr,arr+n);
   		long long mx=arr[n-1]*arr[n-2];
   		long long mn=arr[0]*arr[1];
   		cout<<mx<<" "<<mn<<endl;
   	}
} 

Hey there,

This was my solution, pardon the unused ints there but I was hoping anyone could tell how my submission was wrong here.
https://www.codechef.com/viewsolution/40984330
Thanks

1 Like

cout<<(arr[arr.size()-1]*arr[arr.size()-2])<<" "<<(arr[0]*arr[1]);
this line has error.
because multiplication may go beyond the int limit.
Make data type of array as long long.

Thanks and Regards

1 Like

I see, Thanks

1 Like

try with long long because the if u multiply two number(may be large) it may overflow

1 Like