1092B - Teams Forming - CodeForces Solution


sortings *800

Please click on ads to support us..

Python Code:

n=int(input())
a=list(map(int,input().split()))
a.sort()
sum=0
for i in range(1,n,2):
    sum+=a[i]-a[i-1]
print(sum)

C++ Code:

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int> v;
    int n,x,c=0;
    cin>>n;
    while(n--){
        cin>>x;
        v.push_back(x);
        }
        sort(v.begin(),v.end());
    for(int i=0;i<v.size();i+=2){
        c+=v[i+1]-v[i];
    }
    cout <<c;
    return 0;
}


Comments

Submit
0 Comments
More Questions

Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum