> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Index of a Valid Split Python Solution

> Tested Python solution for LeetCode 2780 with 27 pytest cases. Generate a practice environment with lcpy.

LeetCode 2780, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/minimum-index-of-a-valid-split/description/).

Generate this problem as a practice environment: tested reference solution, 27 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2780   # by problem number
lcpy gen -s minimum_index_of_a_valid_split   # by problem name
```

## 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

```
Input: nums = [1,2,2,2]
Output: 2
Explanation: We can split the array at index 2 to obtain arrays [1,2,2] and [2].
In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3.
In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1.
Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split.
It can be shown that index 2 is the minimum index of a valid split.
```

```
Input: nums = [2,1,3,1,1,1,7,1,2,1]
Output: 4
Explanation: We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1].
In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split.
It can be shown that index 4 is the minimum index of a valid split.
```

```
Input: nums = [3,3,3,3,7,2,2]
Output: -1
Explanation: It can be shown that there is no valid split.
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_index_of_a_valid_split/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_index_of_a_valid_split/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def minimum_index(self, nums: list[int]) -> int:
        dominant = 0
        count = 0
        for num in nums:
            if count == 0:
                dominant = num
                count = 1
            elif num == dominant:
                count += 1
            else:
                count -= 1

        total = 0
        for num in nums:
            if num == dominant:
                total += 1

        left = 0
        for i, num in enumerate(nums):
            if num == dominant:
                left += 1
            right = total - left
            if left * 2 > i + 1 and right * 2 > len(nums) - i - 1:
                return i
        return -1
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.