Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
An element x of an integer array arr of length m is dominant if more than half the elements of arr have a value of x.
You are given a 0-indexed integer array nums of length n with one dominant element.
You can split nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1], but the split is only valid if:
0 <= i < n - 1nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element.Here, nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j, both ends being inclusive. Particularly, if j < i then nums[i, ..., j] denotes an empty subarray.
Return the minimum index of a valid split. If no valid split exists, return -1.
Example 1:
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.
Example 2:
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.
Example 3:
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 <= 1051 <= nums[i] <= 109nums has exactly one dominant element.Problem summary: An element x of an integer array arr of length m is dominant if more than half the elements of arr have a value of x. You are given a 0-indexed integer array nums of length n with one dominant element. You can split nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1], but the split is only valid if: 0 <= i < n - 1 nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element. Here, nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j, both ends being inclusive. Particularly, if j < i then nums[i, ..., j] denotes an empty subarray. Return the minimum index of a valid split. If no valid split exists, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,2,2]
[2,1,3,1,1,1,7,1,2,1]
[3,3,3,3,7,2,2]
majority-element)partition-array-into-disjoint-intervals)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2780: Minimum Index of a Valid Split
class Solution {
public int minimumIndex(List<Integer> nums) {
int x = 0, cnt = 0;
Map<Integer, Integer> freq = new HashMap<>();
for (int v : nums) {
int t = freq.merge(v, 1, Integer::sum);
if (cnt < t) {
cnt = t;
x = v;
}
}
int cur = 0;
for (int i = 1; i <= nums.size(); ++i) {
if (nums.get(i - 1) == x) {
++cur;
if (cur * 2 > i && (cnt - cur) * 2 > nums.size() - i) {
return i - 1;
}
}
}
return -1;
}
}
// Accepted solution for LeetCode #2780: Minimum Index of a Valid Split
func minimumIndex(nums []int) int {
x, cnt := 0, 0
freq := map[int]int{}
for _, v := range nums {
freq[v]++
if freq[v] > cnt {
x, cnt = v, freq[v]
}
}
cur := 0
for i, v := range nums {
i++
if v == x {
cur++
if cur*2 > i && (cnt-cur)*2 > len(nums)-i {
return i - 1
}
}
}
return -1
}
# Accepted solution for LeetCode #2780: Minimum Index of a Valid Split
class Solution:
def minimumIndex(self, nums: List[int]) -> int:
x, cnt = Counter(nums).most_common(1)[0]
cur = 0
for i, v in enumerate(nums, 1):
if v == x:
cur += 1
if cur * 2 > i and (cnt - cur) * 2 > len(nums) - i:
return i - 1
return -1
// Accepted solution for LeetCode #2780: Minimum Index of a Valid Split
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2780: Minimum Index of a Valid Split
// class Solution {
// public int minimumIndex(List<Integer> nums) {
// int x = 0, cnt = 0;
// Map<Integer, Integer> freq = new HashMap<>();
// for (int v : nums) {
// int t = freq.merge(v, 1, Integer::sum);
// if (cnt < t) {
// cnt = t;
// x = v;
// }
// }
// int cur = 0;
// for (int i = 1; i <= nums.size(); ++i) {
// if (nums.get(i - 1) == x) {
// ++cur;
// if (cur * 2 > i && (cnt - cur) * 2 > nums.size() - i) {
// return i - 1;
// }
// }
// }
// return -1;
// }
// }
// Accepted solution for LeetCode #2780: Minimum Index of a Valid Split
function minimumIndex(nums: number[]): number {
let [x, cnt] = [0, 0];
const freq: Map<number, number> = new Map();
for (const v of nums) {
freq.set(v, (freq.get(v) ?? 0) + 1);
if (freq.get(v)! > cnt) {
[x, cnt] = [v, freq.get(v)!];
}
}
let cur = 0;
for (let i = 1; i <= nums.length; ++i) {
if (nums[i - 1] === x) {
++cur;
if (cur * 2 > i && (cnt - cur) * 2 > nums.length - i) {
return i - 1;
}
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.