1720B - Interesting Sum - CodeForces Solution


data structures greedy math sortings

Please click on ads to support us..

Python Code:

t=int(input())
while t>0:
    n=int(input())
    a=[]
    a=list(map(int,input().split()))
    a.sort()
    print(a[n-1]+a[n-2]-a[0]-a[1])
    t=t-1
            

C++ Code:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <queue>
#include <numeric>
#include <cassert>
#include <bitset>
#include <cstdio>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <map>
#include <unordered_map>
#include <set>
#include <deque>

using namespace std;

#define all(a) a.begin(), a.end()
#define cnt0(x) __builtin_ctz(x)
#define endl '\n'
#define itn int
#define ll long long
#define ull unsigned long long
#define rep(i, a, b) for(int i = a;i <= b; i ++)
#define per(i, a, b) for(int i = a;i >= b; i --)
#define cntone(x) __builtin_popcount(x)
#define db double
#define x first
#define y second
#define AC main(void)
#define HYS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef pair<int, int > PII;
typedef pair<int, pair<int, int>> PIII;
typedef pair<ll, ll> Pll;
typedef pair<double, double> PDD;
using ld = double long;

const long double eps = 1e-9;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10, M = 4e5 + 10;
const int INF = 0x3f3f3f3f;

int d1[] = {0, 0, 1, -1};
int d2[] = {1, -1, 0, 0};

int n, m;
int _ = 1;
ll res;
int a[M];

int h[N], ne[M], e[M], idx;

void solve(){
	cin >> n;
	for(int i = 1; i <= n; i ++)	cin >> a[i];
	sort(a + 1, a + n + 1);
	cout << a[n] + a[n - 1] - a[1] - a[2] << endl;
}
int AC{
    HYS

	cin >> _;
	while(_ --)
        solve();

    return 0;
}


Comments

Submit
0 Comments
More Questions

1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading