/** * Author : Mr_Sohel * Task : * Algo : Multiset + Sliding Window **/ #include #define endl '\n' #define sqr(x) (x) * (x) #define gcd(x,y) __gcd(x,y) #define lcm(x,y) ((x/gcd(x,y)) * y) #define pb push_back #define pf push_front #define mk make_pair #define fi first #define se second #define sz(x) (int)x.size() #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define prec(x) fixed<; using pll = pair; const ld PI = acos((ld) - 1); const int MOD = 1e9 + 7; const ll INF = 2e18 + 1; const ld EPS = 1e-9; const int MX = 1e5 + 2; #ifdef LOCAL #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) template < typename Arg1 > void __f(const char* name, Arg1&& arg1) { cerr << name << " = " << arg1 << endl; } template < typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...); } #else #define debug(...) #endif int a[MX]; int main() { #ifdef LOCAL clock_t tStart = clock(); freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif unsyncIO; int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int i = 0, j = k + 1, ans = 0; multiset mp; // i wil try with multimap later for (i = 0; i <= j and i < n; i++) { mp.insert(a[i]); } i = 0; auto f = mp.begin(), l = --mp.end(); // l--; ans = max(ans, *l - *f); while (j < n) { f = mp.begin(), l = --mp.end(); ans = max(ans, (*l - *f)); /*for (auto in : mp) { cout << in << " "; } cout << "ans = " << ans << endl;*/ auto x = mp.find(a[i]); mp.erase(x); j++; i++; mp.insert(a[j]); } cout << ans << endl; } #ifdef LOCAL cerr << "\nRuntime: " << (ld) (clock() - tStart) / CLOCKS_PER_SEC << " Seconds" << endl; #endif return 0; }