Advertisement
LEGEND2004

Practice #2 I

Jun 22nd, 2023
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int N = 105;
  6. int a[N];
  7.  
  8. signed main()
  9. {
  10.     int t;
  11.     cin >> t; //test cases
  12.     while(t--){
  13.         int n;
  14.         cin >> n; // array size
  15.         for(int i = 0; i < n; i++){
  16.             cin >> a[i]; //array elements
  17.         }
  18.         sort(a , a + n);
  19.         int k = n / 2 , ans = 0;
  20.         for(int i = 0; i < k; i++){
  21.             ans += (a[n - i - 1] - a[i]);
  22.         }
  23.         cout << ans << endl;
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement