GSJANB - Editorial

PROBLEM LINK: Contest

DIFFICULTY: Simple

PREREQUISITES: Adhoc, Math, Sorting, Basic I/O

PROBLEM:

The aim is to find maximum sum by getting maximum difference of any two elements of each array.

SETTER’S SOLUTION: Solution Link

APPROACH:

Initialize sum S=0.

Take every array as input and sort it.

Take absolute sum of largest and smallest of array which is first and last element of the array.
Add it to the sum S.

Implementation:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
long long int n;
cin>>n;
long long int sum=0;
for(int i=0;i<n;i++)
{
long long int a[n];
for(long long int j=0;j<n;j++)
{
cin>>a[j];
}
sort(a,a+n);
sum+=a[n-1]-a[0];
}
cout<<sum<<endl;
}
}