My issue
My logic : Sort the array and just output diff of kth element and first element.
Can’t figure out why this doesn’t work
My code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define nl "\n"
#define maxe(a) (*max_element(a.begin(), a.end()))
#define mine(a) (*min_element(a.begin(), a.end()))
#define all(a) a.begin(), a.end()
#define nsb(n) (__builtin_popcountll(n)) //no of set bits.
#define len(n) (to_string(n).size())
#define is2pow(n) (n && (!(n&(n-1))))
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
// code here
int t;
cin>>t;
while(t--){
ll n,k;
cin>>n>>k;
vector<ll> v(n);
for(int i=0 ; i<n ; i++){
cin>>v[i];
}
sort(all(v));
cout<<v[k-1] - v[0]<<nl;
}
return 0;
}
Learning course: Jump from 2* to 3*
Problem Link: CodeChef: Practical coding for everyone