constructive algorithms greedy sortings

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
    n = int(input())
    l = list(map(int, input().split()))
    l = sorted(l)
    s = 0
    for i in range(1,n):
        s += l[i] - l[i-1]
    print(s)

C++ Code:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define pb push_back
#define pp pop_back
const int mod = 1e9 + 7;
const double e = 1e-6;

int main(){
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n);
    cout<<a[n-1]-a[0]<<endl;

}
return 0;
}


Comments

Submit
0 Comments
More Questions

1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers