Wednesday, August 26, 2020

Codeforces problem-b-Teams forming

 Link : http://codeforces.com/contest/1092/problem/B

Python code : 

n=int(input())
x=[int(x) for x in input().split()]
x.sort()
i=1
s=0
while(i<n):
s=s+x[i]-x[i-1]
i=i+2
print(s)


C ++ Code :


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,i,n;
cin >> n;
int x[n];
for(i=0;i<n;i++)
{
cin >> x[i];
}
sort(x,x+n);
int s=0;
for(i=1;i<n;i=i+2)
{
s=s+(x[i]-x[i-1]);
}
cout << s << endl;
}


No comments:

Post a Comment

UVa 10550- combination lock

 ``` while True: a , b , c , d = map ( int , input (). split ()) if a == b and b == c and c == d and c == 0 : break x1 =...