Split the Array - Problem

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.

Input & Output

Example 1 — Basic Valid Split
$ Input: nums = [1,1,2,2]
Output: true
💡 Note: We can split into [1,2] and [1,2]. Both groups have distinct elements.
Example 2 — Impossible Split
$ Input: nums = [1,1,1,1]
Output: false
💡 Note: Element 1 appears 4 times. Cannot split into two distinct groups of size 2.
Example 3 — Mixed Frequencies
$ Input: nums = [1,1,2,3]
Output: true
💡 Note: Can split into [1,2] and [1,3]. Each group has distinct elements.

Constraints

  • 2 ≤ nums.length ≤ 100
  • nums.length is even
  • -100 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Split the Array Problem1122Input: [1,1,2,2]Group 1:[1, 2]Group 2:[1, 2]Both groups have distinct elementsOutput: true
Understanding the Visualization
1
Input
Array [1,1,2,2] of even length
2
Process
Check if we can split into two distinct groups
3
Output
Return true if splitting is possible
Key Takeaway
🎯 Key Insight: If any element appears more than twice, splitting into two distinct groups is impossible
Asked in
Amazon 15 Microsoft 12
23.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen