LeetCode 2780, Medium. Topics: Array, Hash Table, Sorting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 27 parametrized pytest cases, and a playground notebook:
Problem
An element <code>x</code> of an integer array <code>arr</code> of length <code>m</code> is <strong>dominant</strong> if <strong>more than half</strong> the elements of <code>arr</code> have a value of <code>x</code>.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> with one <strong>dominant</strong> element.</p>
<p>You can split <code>nums</code> at an index <code>i</code> into two arrays <code>nums[0, …, i]</code> and <code>nums[i + 1, …, n - 1]</code>, but the split is only <strong>valid</strong> if:</p>
<ul>
<li><code>0 <= i < n - 1</code></li>
<li><code>nums[0, …, i]</code>, and <code>nums[i + 1, …, n - 1]</code> have the same dominant element.</li>
</ul>
<p>Here, <code>nums[i, …, j]</code> denotes the subarray of <code>nums</code> starting at index <code>i</code> and ending at index <code>j</code>, both ends being inclusive. Particularly, if <code>j < i</code> then <code>nums[i, …, j]</code> denotes an empty subarray.</p>
<p>Return <em>the <strong>minimum</strong> index of a <strong>valid split</strong></em>. If no valid split exists, return <code>-1</code>.</p>
Examples
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9
- nums has exactly one dominant element.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026