G1. Big Wins! (easy version)

G1. Big Wins! (easy version)

Problem Statement

This is the easy version of the problem. The difference between the versions is that in this version aimin(n,100)a_i \leq \min(n, 100).

You are given an array of nn integers a1,a2,,ana_1, a_2, \ldots, a_n.

Your task is to find a subarray a[l,r]a[l,r] (a continuous sequence of elements al,al+1,,ara_l, a_{l+1}, \ldots, a_r) for which the value of the expression med(a[l,r])min(a[l,r])\text{med}(a[l,r]) - \min(a[l,r]) is maximized.

Here:

  • med\text{med} — the median of the subarray, which is the element at position k+12\lceil \frac{k+1}{2} \rceil after sorting the subarray, where kk is its length;
  • min\min — the minimum element of this subarray.

Example

For example, consider the array a=[1,4,1,5,3,3]a = [1, 4, 1, 5, 3, 3] and choose the subarray a[2,5]=[4,1,5,3]a[2,5] = [4, 1, 5, 3]. In sorted form, it looks like [1,3,4,5][1, 3, 4, 5].

  • med(a[2,5])=4\text{med}(a[2,5]) = 4, since 4+12=3\lceil \frac{4+1}{2} \rceil = 3, so the third element in the sorted subarray is 4;
  • min(a[2,5])=1\min(a[2,5]) = 1, since the minimum element is 1.

In this example, the value medmin=41=3\text{med} - \min = 4 - 1 = 3.

Input

The first line contains an integer tt (1t104)(1 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains one integer nn (1n2105)(1 \leq n \leq 2 \cdot 10^5) — the length of the array.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1aimin(n,100))(1 \leq a_i \leq \min(n, 100)) — the elements of the array.

It is guaranteed that the sum of nn across all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output a single integer — the maximum possible value of medmin\text{med} - \min among all subarrays of the array.

Solution Overview

The problem asks us to find a subarray a[l,r]a[l, r] such that med(a[l,r])min(a[l,r])med(a[l, r]) - min(a[l, r]) is maximized. The constraint aimin(n,100)a_i \le \min(n, 100) is crucial here, as it limits the possible values in the array to a small range (1 to 100).

The provided solution leverages this small range of values by iterating through all possible median values.

Here’s a breakdown of the solution:


Understanding the Strategy: Iterating on the Median (mm)

The core idea is to fix a potential value for the median, let’s call it mm. Since array values are between 1 and 100, mm can also only be between 1 and 100. We will try each of these 100 possible values for mm.

For a fixed mm, our goal is to find a subarray a[l,r]a[l, r] such that:

  1. med(a[l,r])mmed(a[l, r]) \ge m (the median is at least mm)
  2. We want to maximize mmin(a[l,r])m - min(a[l, r]). This means we want to find a subarray where the median is at least mm, and its minimum element is as small as possible.

Step-by-Step Explanation for a Fixed Median mm:

  1. Transforming the Array into bb: For a fixed mm, we create a new array bb of the same size as aa. Each element bjb_j is determined as follows:

    • bj=+1b_j = +1 if ajma_j \ge m
    • bj=1b_j = -1 if aj<ma_j < m

    Why this transformation? The definition of the median relies on the majority of elements. A subarray a[l,r]a[l, r] will have its median at least mm if and only if more than half of its elements are m\ge m. If we sum the bjb_j values for a subarray a[l,r]a[l, r], this sum (let’s call it SS) will tell us about the count of elements greater than or equal to mm versus those less than mm.

    • Each ajma_j \ge m contributes +1+1 to the sum.
    • Each aj<ma_j < m contributes 1-1 to the sum. If the sum S>0S > 0, it means there are more elements m\ge m than elements <m< m in the subarray. This ensures that the median of the subarray is at least mm. (Specifically, for a subarray of length kk, if the number of elements m\ge m is CgeC_{ge} and elements <m< m is CltC_{lt}, then S=CgeCltS = C_{ge} - C_{lt}. If S>0S > 0, then Cge>CltC_{ge} > C_{lt}. Since Cge+Clt=kC_{ge} + C_{lt} = k, it implies Cge>k/2C_{ge} > k/2. This is exactly the condition for the median to be m\ge m.)
  2. Using Prefix Sums for Subarray Sums: We calculate the prefix sums of the bb array: prefk=i=1kbipref_k = \sum_{i=1}^k b_i. We define pref0=0pref_0 = 0. The sum of bb values for a subarray b[l,r]b[l, r] is prefrprefl1pref_r - pref_{l-1}. So, the condition “med(a[l,r])mmed(a[l, r]) \ge m” is equivalent to “prefrprefl1>0pref_r - pref_{l-1} > 0”, or prefr>prefl1pref_r > pref_{l-1}.

  3. Finding “Valid” Indices for Potential Medians: For a fixed mm, we want to know for each position jj if it can be part of some subarray whose median is at least mm. This is true if there exists a subarray a[l,r]a[l, r] containing jj such that med(a[l,r])mmed(a[l, r]) \ge m. This means we need to check if there’s an ljrl \le j \le r such that prefr>prefl1pref_r > pref_{l-1}.

    The solution states two conditions for this:

    • min0x<jprefx<prefjmin_{0 \le x < j} pref_x < pref_j: This means there’s a starting point l1=xl-1 = x before jj such that prefjprefx>0pref_j - pref_x > 0. In other words, there’s a subarray a[x+1,j]a[x+1, j] ending at jj whose median is m\ge m.
    • prefj1<maxjxnprefxpref_{j-1} < max_{j \le x \le n} pref_x: This means there’s an ending point r=xr = x at or after jj such that prefxprefj1>0pref_x - pref_{j-1} > 0. In other words, there’s a subarray a[j,x]a[j, x] starting at jj whose median is m\ge m.

    To efficiently check these conditions for every jj:

    • Prefix Minima (prefmnjprefmn_j): Compute prefmnj=min0xjprefxprefmn_j = \min_{0 \le x \le j} pref_x. This allows us to quickly check min0x<jprefx<prefjmin_{0 \le x < j} pref_x < pref_j.
    • Suffix Maxima (suffmxjsuffmx_j): Compute suffmxj=maxjxnprefxsuffmx_j = \max_{j \le x \le n} pref_x. This allows us to quickly check prefj1<maxjxnprefxpref_{j-1} < max_{j \le x \le n} pref_x.

    If either prefmnj1<prefjprefmn_{j-1} < pref_j OR prefj1<suffmxjpref_{j-1} < suffmx_j (note: the indices might need careful adjustment based on 0-indexing vs 1-indexing, but the concept holds), then position jj can be part of a subarray with median m\ge m.

  4. Maximizing majm - a_j: For each position jj that satisfies either of the conditions from step 3 (meaning aja_j can be part of a subarray whose median is at least mm), we consider aja_j as a potential minimum element of that subarray. If we pick aja_j as the minimum element, and we know there exists a subarray containing jj whose median is at least mm, then we can achieve a value of majm - a_j. We want to maximize medminmed - min. Since we fixed medmmed \ge m, to maximize mminm - min, we should try to minimize minmin. The smallest possible minmin for a subarray that contains jj and has its median m\ge m is aja_j itself, if we choose a subarray a[l,r]a[l, r] where aja_j is indeed the minimum.

    The solution says: “If either inequality holds, index jj can lie in some subarray whose median is m\ge m. We are free to take that same jj as the minimum of the chosen subarray, giving value aja_j. Hence, for that jj we can realise the gap majm-a_j. Among all indices that satisfy the test keep the largest difference.” This is a key simplification. It means we don’t need to explicitly find the optimal subarray for each jj. If jj can be part of any valid median m\ge m subarray, we assume we can form a subarray where aja_j is the minimum, and its median is m\ge m. This is a greedy approach.

    So, for a fixed mm, we iterate through all jj from 11 to nn. If aja_j is “valid” (can be part of a subarray with median m\ge m), we update our maximum difference found so far with majm - a_j.


Overall Algorithm:

  1. Initialize max_difference = -infinity (or a very small number).
  2. Iterate m from 1 to 100: a. Create array b where b[j] = +1 if a[j] >= m, else b[j] = -1. b. Compute prefix sums pref for b. (pref[0] = 0, pref[k] = pref[k-1] + b[k]) c. Compute prefix minima prefmn for pref. (prefmn[j] = min(prefmn[j-1], pref[j])) d. Compute suffix maxima suffmx for pref. (suffmx[j] = max(suffmx[j+1], pref[j])) e. Iterate j from 1 to n: i. Check if prefmn[j-1] < pref[j] (subarray ending at jj with median m\ge m). ii. Check if pref[j-1] < suffmx[j] (subarray starting at jj with median m\ge m). iii. If either condition holds, update max_difference = max(max_difference, m - a[j]).
  3. Output max_difference.

Complexity:

  • Time Complexity:

    • The outer loop runs 100 times (for m=1m=1 to 100100).
    • Inside the loop:
      • Building b: O(n)O(n)
      • Computing pref: O(n)O(n)
      • Computing prefmn: O(n)O(n)
      • Computing suffmx: O(n)O(n)
      • Scanning j and updating max_difference: O(n)O(n)
    • Total time complexity: 100×O(n)=O(100n)100 \times O(n) = O(100n).
    • Given n2105n \le 2 \cdot 10^5, 100×2105=2107100 \times 2 \cdot 10^5 = 2 \cdot 10^7, which is well within the 4-second time limit.
  • Space Complexity:

    • We need arrays for a, b, pref, prefmn, suffmx, all of size O(n)O(n).
    • Total space complexity: O(n)O(n).
    • Given n2105n \le 2 \cdot 10^5, this fits within 256 megabytes.

Example Walkthrough (Simplified):

Let a=[1,4,1,5,3,3]a = [1, 4, 1, 5, 3, 3]

Let’s pick m=4m=4.

  1. Build bb for m=4m=4: aj4    bj=+1a_j \ge 4 \implies b_j = +1 aj<4    bj=1a_j < 4 \implies b_j = -1 a=[1,4,1,5,3,3]a = [1, 4, 1, 5, 3, 3] b=[1,+1,1,+1,1,1]b = [-1, +1, -1, +1, -1, -1]

  2. Prefix sums prefpref: (pref0=0pref_0=0) pref=[0,1,0,1,0,1,2]pref = [0, -1, 0, -1, 0, -1, -2] (0-indexed pref0pref_0 to pref6pref_6)

  3. Prefix minima prefmnprefmn: prefmn=[0,1,1,1,1,1,2]prefmn = [0, -1, -1, -1, -1, -1, -2]

  4. Suffix maxima suffmxsuffmx: (assuming suffmxn+1suffmx_{n+1} is -\infty) suffmx=[0,0,0,0,0,1,2]suffmx = [0, 0, 0, 0, 0, -1, -2] (from right to left: max(2)\max(-2), max(1,2)\max(-1, -2), max(0,1)\max(0, -1), etc.)

  5. Iterate jj and check validity:

    • For j=1(a1=1)j=1 (a_1=1): prefmn0=0,pref1=1prefmn_0=0, pref_1=-1. 0<10 < -1 is FALSE. pref0=0,suffmx1=0pref_0=0, suffmx_1=0. 0<00 < 0 is FALSE. a1a_1 is not valid for m=4m=4.
    • For j=2(a2=4)j=2 (a_2=4): prefmn1=1,pref2=0prefmn_1=-1, pref_2=0. 1<0-1 < 0 is TRUE. a2a_2 is valid. max_difference=max(,44)=0max\_difference = \max(-\infty, 4-4) = 0.
    • For j=3(a3=1)j=3 (a_3=1): prefmn2=1,pref3=1prefmn_2=-1, pref_3=-1. 1<1-1 < -1 is FALSE. pref2=0,suffmx3=0pref_2=0, suffmx_3=0. 0<00 < 0 is FALSE. a3a_3 is not valid.
    • For j=4(a4=5)j=4 (a_4=5): prefmn3=1,pref4=0prefmn_3=-1, pref_4=0. 1<0-1 < 0 is TRUE. a4a_4 is valid. max_difference=max(0,45)=0max\_difference = \max(0, 4-5) = 0.
    • For j=5(a5=3)j=5 (a_5=3): prefmn4=1,pref5=1prefmn_4=-1, pref_5=-1. 1<1-1 < -1 is FALSE. pref4=0,suffmx5=1pref_4=0, suffmx_5=-1. 0<10 < -1 is FALSE. a5a_5 is not valid.
    • For j=6(a6=3)j=6 (a_6=3): prefmn5=1,pref6=2prefmn_5=-1, pref_6=-2. 1<2-1 < -2 is FALSE. pref5=1,suffmx6=2pref_5=-1, suffmx_6=-2. 1<2-1 < -2 is FALSE. a6a_6 is not valid.

For m=4m=4, the maximum difference found is 00.

If we try m=3m=3: a=[1,4,1,5,3,3]a = [1, 4, 1, 5, 3, 3] b=[1,+1,1,+1,+1,+1]b = [-1, +1, -1, +1, +1, +1] pref=[0,1,0,1,0,1,2]pref = [0, -1, 0, -1, 0, 1, 2] prefmn=[0,1,1,1,1,1,1]prefmn = [0, -1, -1, -1, -1, -1, -1] suffmx=[2,2,2,2,2,2,2]suffmx = [2, 2, 2, 2, 2, 2, 2]

Let’s check j=3(a3=1)j=3 (a_3=1): prefmn2=1,pref3=1prefmn_2=-1, pref_3=-1. 1<1-1 < -1 is FALSE. pref2=0,suffmx3=2pref_2=0, suffmx_3=2. 0<20 < 2 is TRUE. a3a_3 is valid. max_difference=max(current_max,3a3)=max(current_max,31)=max(current_max,2)max\_difference = \max(\text{current\_max}, 3 - a_3) = \max(\text{current\_max}, 3 - 1) = \max(\text{current\_max}, 2).

The solution’s logic is sound and exploits the small range of values in the array to efficiently check all possible median values.

submission