An integer v
is a median of an array b
of length m
if and only if:
v
is greater than or equal to at least ⌈m2⌉
elements of the array, and
v
is less than or equal to at least ⌈m2⌉
elements of the array.
For instance:
the only median of [9,3,7]
is 7
,
the medians of [5,3,7,9]
are 5
, 6
, and 7
, and
the only median of [2,2,2]
is 2
.
You're given an integer k
and an array a1,…,an
of integers between 1
and n
.
An integer v
from 1
to n
is said to be a submedian if there exists at least one pair of indices (l,r)
such that
1≤l≤r≤n
,
r−l+1≥k
,
v
is a median of the subarray [al,…,ar]
.
It can be proven that there always exists at least one submedian. Find the maximum submedian vmax
and any corresponding pair of indices (l,r)
.
Input
Each test contains multiple test cases. The first line contains the number of test cases t
(1≤t≤50000
). The description of the test cases follows.
The first line of each test case contains two integers n
and k
(1≤k≤n≤300000
).
The second line of each test case contains n
integers a1,a2,…,an
(1≤ai≤n
).
It is guaranteed that the sum of n
over all test cases doesn't exceed 300000
.
Output
For each test case, output three integers vmax
, l
, and r
— the maximum submedian vmax
and the bounds of a subarray of length at least k
(r−l+1≥k
) such that vmax
is one of its medians.
If there are many solutions, you can print any of them.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
So for a fixed v we only have to find any subarray of length ≥ k
satisfying the two inequalities above.
2. Checking a fixed v – two pointers
Both inequalities are monotone in the right end of the subarray:
when we extend the subarray to the right, cnt_le, cnt_ge and need
only increase.
Therefore a classic two‑pointer (sliding window) scan works.
code
l = 1 , r = 0
while l ≤ n
if r < l r = l
while r ≤ n
len = r-l+1
if len < k r++ , continue
cnt_le = pref_le[r] - pref_le[l-1]
cnt_ge = pref_ge[r] - pref_ge[l-1]
need = (len+1)/2
if cnt_le ≥ need and cnt_ge ≥ need
success – we found a subarray
else r++
l++
r only moves to the right, never back, therefore the whole scan is
O(n) for the current v.
If a subarray is found we also remember its l and r.
3. Binary search on v
v is an integer in [1 , n].
If a value v is a submedian, every larger value is not guaranteed
to be a submedian, but the set of submedians is monotone in the sense
that if v is a submedian, all values ≤ v are also submedians.
Hence we can binary search the maximum v.
code
low = 1 , high = n
while low < high
mid = (low + high + 1) / 2
if check(mid) low = mid
else high = mid-1
After the loop low is the largest submedian.
We run check(low) once more to obtain the required indices l , r.
4. Correctness Proof
We prove that the algorithm outputs the required vmax, l, r.
Lemma 1
For a fixed v the procedure check(v) returns true
iff there exists a subarray of length ≥ k whose median is v.
Proof.
If part –
During the scan every pair (l,r) with r ≥ l is examined exactly once.
For each examined pair the algorithm computes
code
cnt_le = number of elements ≤ v in a[l … r]
cnt_ge = number of elements ≥ v in a[l … r]
need = ceil((r-l+1)/2)
If cnt_le ≥ need and cnt_ge ≥ need the subarray satisfies the
definition of a median v.
Thus the algorithm returns true only when such a subarray exists.
Only if part –
Assume a subarray a[l … r] of length ≥ k has median v.
While scanning, the pointer l will eventually equal this l.
Because r only moves to the right, the inner loop will reach this
r.
At that moment the algorithm checks the same inequalities and
recognises the subarray, therefore it returns true. ∎
Lemma 2
Let S(v) be the set of submedians for value v.
If v1 < v2 and v2 ∈ S(v2) then v1 ∈ S(v1).
Proof.
Take any subarray a[l … r] of length m whose median is v2.
For this subarray
code
cnt_le(v2) ≥ ceil(m/2) and cnt_ge(v2) ≥ ceil(m/2)
For any v1 < v2
code
cnt_le(v1) = number of elements ≤ v1
≥ number of elements ≤ v2 (because v1 < v2)
= cnt_le(v2)
cnt_ge(v1) = number of elements ≥ v1
≥ number of elements ≥ v2 (because v1 < v2)
= cnt_ge(v2)
Thus the same subarray satisfies the inequalities for v1 as well,
hence v1 is also a submedian. ∎
Lemma 3
During binary search the invariant low is a submedian and high is not larger than the maximum submedian
holds.
Proof.
Initially low = 1 – obviously a submedian (any subarray of length k
has median at least 1). high = n – may or may not be a submedian.
At each step let mid = (low+high+1)/2.
If check(mid) is true, by Lemma 1 mid is a submedian,
therefore the maximum submedian is at least mid.
We set low = mid, keeping the invariant.
If check(mid) is false, mid is not a submedian.
By Lemma 2 every value larger than mid cannot be a submedian
either, so the maximum submedian is < mid.
We set high = mid-1, again preserving the invariant.
Thus the invariant holds after every iteration. ∎
Lemma 4
After binary search terminates, low equals the maximum submedian
vmax.
Proof.
The loop stops when low == high.
By Lemma 3, low is a submedian and no larger value is a
submedian.
Hence low is exactly the maximum submedian. ∎
Lemma 5
The final call check(low) returns a pair (l,r) such that
a[l … r] has median low and length ≥ k.
Proof.
By Lemma 1, check(low) returns true because low is a
submedian.
The procedure stores the first pair (l,r) that satisfies the median
conditions, therefore the returned indices fulfil the required
properties. ∎
Theorem
For every test case the algorithm outputs
vmax – the maximum submedian,
l , r – indices of a subarray of length at least k whose median
is vmax.
Proof.
By Lemma 4 the binary search yields vmax.
By Lemma 5 the subsequent call to check(vmax) supplies indices
l , r of a valid subarray.
Thus the output satisfies the specification. ∎
5. Complexity Analysis
For one test case of length n
Binary search: O(log n) iterations.
Each check runs in O(n) time and O(n) memory for the two
prefix arrays.
code
Time : O(n log n)
Memory : O(n)
The sum of n over all test cases is at most 3·10^5, therefore the
total running time is well below one second.
6. Reference Implementation (GNU‑C++17)
cpp
#include <bits/stdc++.h>
using namespace std;
struct Result {
bool ok;
int l, r;
};
Result check(const vector<int>& a, int n, int k, int v) {
vector<int> pref_le(n + 1, 0), pref_ge(n + 1, 0);
for (int i = 1; i <= n; ++i) {
pref_le[i] = pref_le[i - 1] + (a[i] <= v);
pref_ge[i] = pref_ge[i - 1] + (a[i] >= v);
}
int l = 1, r = 0;
while (l <= n) {
if (r < l) r = l;
while (r <= n) {
int len = r - l + 1;
if (len < k) { ++r; continue; }
int cnt_le = pref_le[r] - pref_le[l - 1];
int cnt_ge = pref_ge[r] - pref_ge[l - 1];
int need = (len + 1) / 2;
if (cnt_le >= need && cnt_ge >= need) {
return {true, l, r};
}
++r;
}
++l;
}
return {false, -1, -1};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
if (!(cin >> T)) return 0;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
int low = 1, high = n;
while (low < high) {
int mid = (low + high + 1) >> 1;
if (check(a, n, k, mid).ok)
low = mid;
else
high = mid - 1;
}
Result res = check(a, n, k, low);
cout << low << ' ' << res.l << ' ' << res.r << '\n';
}
return 0;
}
The program follows exactly the algorithm proven correct above and
conforms to the GNU++17 compiler.