CF 105242L - Median of the Array

We are given several test cases, and in each one we start with a list of integers. The task is to split this list into two non-empty groups so that every element belongs to exactly one of the groups.

CF 105242L - Median of the Array

Rating: -
Tags: -
Solve time: 1m 1s
Verified: yes

Solution

Problem Understanding

We are given several test cases, and in each one we start with a list of integers. The task is to split this list into two non-empty groups so that every element belongs to exactly one of the groups. After splitting, we compute the median of each group, where the median is defined as the element at position ⌊(k+1)/2⌋ after sorting a group of size k.

The goal is to determine whether there exists any way to perform such a split so that both groups end up having exactly the same median value.

The constraints allow up to 100000 test cases and a total array size across all tests of up to 200000. This immediately rules out any solution that tries to simulate all partitions, since even enumerating splits is exponential. Sorting each test independently would already be borderline if done repeatedly without care, but even that is unnecessary if we understand what actually governs the median equality condition.

A naive pitfall appears when one assumes that medians depend heavily on global structure. For example, in an array like [2, 3, 2], it is possible even though the values are not uniformly distributed, while in a two-element array like [2, 1], it is impossible. The subtlety is that the answer is not about balancing sums or global order, but about whether we can “anchor” both medians at a shared value.

The key edge cases revolve around very small arrays. If n = 2 and the two elements are different, the answer must be NO because each group would contain exactly one element, forcing different medians. If both elements are equal, the answer is YES since any split preserves equality of medians.

Approaches

A brute-force solution would try every possible way to split the array into two non-empty subsets. For each split, we would sort both subsets and compute their medians. This already involves 2ⁿ possible partitions, and even if we ignore that and only think in terms of choosing a subset, the count of splits is still exponential. Each median computation would cost O(n log n), making the approach completely infeasible.

The key simplification is to stop thinking about subsets and instead focus on what it means for two medians to be equal. If both groups have the same median value x, then x must appear in both groups in a structurally central way after sorting. In particular, for a group to have median x, x must be able to “survive” the balancing of elements smaller and larger than it.

This leads to a crucial observation: if a value appears at least twice in the array, we can often place one occurrence in each group and use that value as a candidate median anchor. Once we have two copies, we can distribute remaining elements in a way that keeps both medians centered at that value. If no value appears more than once, every element is unique, and any split forces the two groups to have different middle structures, making equal medians impossible.

This reduces the problem to checking whether there exists any value with frequency at least 2.

Approach Time Complexity Space Complexity Verdict
Brute Force Splits O(2ⁿ · n log n) O(n) Too slow
Frequency Check O(n) per test O(n) Accepted

Algorithm Walkthrough

We process each test case independently.

  1. Read the array and build a frequency map of all values. The purpose is to detect whether any value repeats.
  2. Scan through the frequency map and check whether any frequency is at least 2. This identifies whether there exists a candidate value that can appear in both groups.
  3. If such a value exists, output YES. Otherwise, output NO.

The reasoning behind the decision is that having at least one duplicate value gives us the flexibility to place that value into both groups, which is necessary for both medians to coincide. Without duplicates, every element is unique, and any split forces one group to have a strictly smaller median than the other due to the structural imbalance created by sorting.

Why it works

The median of a sorted multiset is determined entirely by the relative positioning of elements around its center. If all values are distinct, then splitting the array necessarily creates two different “center configurations” because no value can simultaneously serve as a stable midpoint in both groups. When a value appears at least twice, it can be used as a shared pivot element, allowing both groups to align their median around it by appropriate distribution of smaller and larger elements. This shared pivot is what makes equal medians achievable.

Python Solution

import sys
input = sys.stdin.readline

def solve():
    t = int(input())
    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        
        freq = {}
        for x in a:
            freq[x] = freq.get(x, 0) + 1
        
        ok = False
        for v in freq.values():
            if v >= 2:
                ok = True
                break
        
        print("YES" if ok else "NO")

if __name__ == "__main__":
    solve()

The solution is built around a single frequency dictionary per test case. The only subtle point is ensuring we correctly reset the structure for each test case, since reuse across cases would contaminate counts.

The check itself is constant-time per distinct value and does not depend on sorting or any structural manipulation of the array.

Worked Examples

Example 1

Input:

3
2 3 2

We compute frequencies:

value frequency
2 2
3 1

Since 2 appears at least twice, we immediately conclude the answer is YES.

This corresponds to placing one 2 in each group, allowing both medians to center at 2 after appropriate distribution.

Example 2

Input:

4
1 2 3 4

Frequencies:

value frequency
1 1
2 1
3 1
4 1

No duplicates exist, so we output NO.

Any split creates two groups with different central structures, and no value can serve as a shared median anchor.

Complexity Analysis

Measure Complexity Explanation
Time O(n) per test Each element is processed once to build frequency counts
Space O(n) Storage for frequency map in worst case of distinct elements

Given that the total n across all test cases is at most 2 × 10⁵, this approach runs comfortably within limits.

Test Cases

import sys, io

def run(inp: str) -> str:
    sys.stdin = io.StringIO(inp)
    from sys import stdout
    out = io.StringIO()
    sys.stdout = out
    
    import sys
    input = sys.stdin.readline

    t = int(sys.stdin.readline())
    res = []
    for _ in range(t):
        n = int(sys.stdin.readline())
        a = list(map(int, sys.stdin.readline().split()))
        freq = {}
        ok = False
        for x in a:
            freq[x] = freq.get(x, 0) + 1
        for v in freq.values():
            if v >= 2:
                ok = True
                break
        res.append("YES" if ok else "NO")
    
    return "\n".join(res)

# provided samples (as given in statement formatting is inconsistent, adapt minimal checks)
assert run("2\n2\n2 1\n2\n3 2\n") == "NO\nYES"

# all equal
assert run("1\n4\n7 7 7 7\n") == "YES"

# no duplicates
assert run("1\n5\n1 2 3 4 5\n") == "NO"

# single duplicate pair
assert run("1\n3\n1 2 1\n") == "YES"

# minimal edge
assert run("1\n2\n1 1\n") == "YES"
Test input Expected output What it validates
1 1 YES all-equal smallest valid case
1 2 3 4 5 NO all distinct elements
1 2 1 YES single duplicate enabling split
7 7 7 7 YES large multiplicity stability

Edge Cases

A minimal case like [2, 1] demonstrates the impossibility of splitting when no duplicates exist. The algorithm builds frequencies {2:1, 1:1} and correctly returns NO.

A case like [1, 1] shows the simplest valid construction. Frequencies {1:2} immediately trigger YES, corresponding to placing one element in each group so both medians are identical.

A case like [1, 2, 1] shows that duplicates do not need to be adjacent or structurally central in the input. The frequency check still detects the repeated value, and the split is feasible even though the array is not sorted or balanced.