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 .
You are given an array of integers .
Your task is to find a subarray (a continuous sequence of elements ) for which the value of the expression is maximized.
Here:
- — the median of the subarray, which is the element at position after sorting the subarray, where is its length;
- — the minimum element of this subarray.
Example
For example, consider the array and choose the subarray . In sorted form, it looks like .
- , since , so the third element in the sorted subarray is 4;
- , since the minimum element is 1.
In this example, the value .
Input
The first line contains an integer — the number of test cases.
The first line of each test case contains one integer — the length of the array.
The second line of each test case contains integers — the elements of the array.
It is guaranteed that the sum of across all test cases does not exceed .
Output
For each test case, output a single integer — the maximum possible value of among all subarrays of the array.
Solution Overview
The problem asks us to find a subarray such that is maximized. The constraint 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 ()
The core idea is to fix a potential value for the median, let’s call it . Since array values are between 1 and 100, can also only be between 1 and 100. We will try each of these 100 possible values for .
For a fixed , our goal is to find a subarray such that:
- (the median is at least )
- We want to maximize . This means we want to find a subarray where the median is at least , and its minimum element is as small as possible.
Step-by-Step Explanation for a Fixed Median :
Transforming the Array into : For a fixed , we create a new array of the same size as . Each element is determined as follows:
- if
- if
Why this transformation? The definition of the median relies on the majority of elements. A subarray will have its median at least if and only if more than half of its elements are . If we sum the values for a subarray , this sum (let’s call it ) will tell us about the count of elements greater than or equal to versus those less than .
- Each contributes to the sum.
- Each contributes to the sum. If the sum , it means there are more elements than elements in the subarray. This ensures that the median of the subarray is at least . (Specifically, for a subarray of length , if the number of elements is and elements is , then . If , then . Since , it implies . This is exactly the condition for the median to be .)
Using Prefix Sums for Subarray Sums: We calculate the prefix sums of the array: . We define . The sum of values for a subarray is . So, the condition “” is equivalent to “”, or .
Finding “Valid” Indices for Potential Medians: For a fixed , we want to know for each position if it can be part of some subarray whose median is at least . This is true if there exists a subarray containing such that . This means we need to check if there’s an such that .
The solution states two conditions for this:
- : This means there’s a starting point before such that . In other words, there’s a subarray ending at whose median is .
- : This means there’s an ending point at or after such that . In other words, there’s a subarray starting at whose median is .
To efficiently check these conditions for every :
- Prefix Minima (): Compute . This allows us to quickly check .
- Suffix Maxima (): Compute . This allows us to quickly check .
If either OR (note: the indices might need careful adjustment based on 0-indexing vs 1-indexing, but the concept holds), then position can be part of a subarray with median .
Maximizing : For each position that satisfies either of the conditions from step 3 (meaning can be part of a subarray whose median is at least ), we consider as a potential minimum element of that subarray. If we pick as the minimum element, and we know there exists a subarray containing whose median is at least , then we can achieve a value of . We want to maximize . Since we fixed , to maximize , we should try to minimize . The smallest possible for a subarray that contains and has its median is itself, if we choose a subarray where is indeed the minimum.
The solution says: “If either inequality holds, index can lie in some subarray whose median is . We are free to take that same as the minimum of the chosen subarray, giving value . Hence, for that we can realise the gap . 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 . If can be part of any valid median subarray, we assume we can form a subarray where is the minimum, and its median is . This is a greedy approach.
So, for a fixed , we iterate through all from to . If is “valid” (can be part of a subarray with median ), we update our maximum difference found so far with .
Overall Algorithm:
- Initialize
max_difference = -infinity(or a very small number). - Iterate
mfrom 1 to 100: a. Create arraybwhereb[j] = +1ifa[j] >= m, elseb[j] = -1. b. Compute prefix sumsprefforb. (pref[0] = 0,pref[k] = pref[k-1] + b[k]) c. Compute prefix minimaprefmnforpref. (prefmn[j] = min(prefmn[j-1], pref[j])) d. Compute suffix maximasuffmxforpref. (suffmx[j] = max(suffmx[j+1], pref[j])) e. Iteratejfrom 1 ton: i. Check ifprefmn[j-1] < pref[j](subarray ending at with median ). ii. Check ifpref[j-1] < suffmx[j](subarray starting at with median ). iii. If either condition holds, updatemax_difference = max(max_difference, m - a[j]). - Output
max_difference.
Complexity:
Time Complexity:
- The outer loop runs 100 times (for to ).
- Inside the loop:
- Building
b: - Computing
pref: - Computing
prefmn: - Computing
suffmx: - Scanning
jand updatingmax_difference:
- Building
- Total time complexity: .
- Given , , which is well within the 4-second time limit.
Space Complexity:
- We need arrays for
a,b,pref,prefmn,suffmx, all of size . - Total space complexity: .
- Given , this fits within 256 megabytes.
- We need arrays for
Example Walkthrough (Simplified):
Let
Let’s pick .
Build for :
Prefix sums : () (0-indexed to )
Prefix minima :
Suffix maxima : (assuming is ) (from right to left: , , , etc.)
Iterate and check validity:
- For : . is FALSE. . is FALSE. is not valid for .
- For : . is TRUE. is valid. .
- For : . is FALSE. . is FALSE. is not valid.
- For : . is TRUE. is valid. .
- For : . is FALSE. . is FALSE. is not valid.
- For : . is FALSE. . is FALSE. is not valid.
For , the maximum difference found is .
If we try :
Let’s check : . is FALSE. . is TRUE. is valid. .
The solution’s logic is sound and exploits the small range of values in the array to efficiently check all possible median values.