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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that:
nums1.length == nums2.length == nums.length / 2.nums1 should contain distinct elements.nums2 should also contain distinct elements.Return true if it is possible to split the array, and false otherwise.
Example 1:
Input: nums = [1,1,2,2,3,4] Output: true Explanation: One of the possible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4].
Example 2:
Input: nums = [1,1,1,1] Output: false Explanation: The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false.
Constraints:
1 <= nums.length <= 100nums.length % 2 == 0 1 <= nums[i] <= 100Problem summary: You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that: nums1.length == nums2.length == nums.length / 2. nums1 should contain distinct elements. nums2 should also contain distinct elements. Return true if it is possible to split the array, and false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,1,2,2,3,4]
[1,1,1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3046: Split the Array
class Solution {
public boolean isPossibleToSplit(int[] nums) {
int[] cnt = new int[101];
for (int x : nums) {
if (++cnt[x] >= 3) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #3046: Split the Array
func isPossibleToSplit(nums []int) bool {
cnt := [101]int{}
for _, x := range nums {
cnt[x]++
if cnt[x] >= 3 {
return false
}
}
return true
}
# Accepted solution for LeetCode #3046: Split the Array
class Solution:
def isPossibleToSplit(self, nums: List[int]) -> bool:
return max(Counter(nums).values()) < 3
// Accepted solution for LeetCode #3046: Split the Array
use std::collections::HashMap;
impl Solution {
pub fn is_possible_to_split(nums: Vec<i32>) -> bool {
let mut cnt = HashMap::new();
for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
*cnt.values().max().unwrap_or(&0) < 3
}
}
// Accepted solution for LeetCode #3046: Split the Array
function isPossibleToSplit(nums: number[]): boolean {
const cnt: number[] = Array(101).fill(0);
for (const x of nums) {
if (++cnt[x] >= 3) {
return false;
}
}
return true;
}
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.