Split the Array - Problem

You're given an integer array nums of even length, and your task is to determine if you can split it into two equal-sized subarrays where each subarray contains only distinct elements.

More specifically, you need to split the array into:

  • nums1 and nums2 where both have length nums.length / 2
  • Each element in nums1 must be unique (no duplicates)
  • Each element in nums2 must be unique (no duplicates)

Return true if such a split is possible, false otherwise.

Example: If nums = [1,1,2,2], you can split it as nums1 = [1,2] and nums2 = [1,2]. Both subarrays have distinct elements, so return true.

Input & Output

example_1.py โ€” Basic Valid Split
$ Input: nums = [1,1,2,2]
โ€บ Output: true
๐Ÿ’ก Note: We can split into nums1 = [1,2] and nums2 = [1,2]. Both arrays have length 2 and contain only distinct elements.
example_2.py โ€” Impossible Split
$ Input: nums = [1,1,1,1]
โ€บ Output: false
๐Ÿ’ก Note: No matter how we split, each array would need 2 elements, but we only have element '1' appearing 4 times. Each sub-array would have duplicates.
example_3.py โ€” Mixed Elements
$ Input: nums = [1,1,2,3]
โ€บ Output: true
๐Ÿ’ก Note: We can split into nums1 = [1,2] and nums2 = [1,3]. Both arrays have distinct elements and equal length.

Constraints

  • 2 โ‰ค nums.length โ‰ค 100
  • nums.length is even
  • 1 โ‰ค nums[i] โ‰ค 100

Visualization

Tap to expand
Team Jersey Distribution ProblemAvailable Jerseys: [1, 1, 2, 2]1122Team A12Team B12โœ“ Successful Distribution!Each team gets unique jersey numbers
Understanding the Visualization
1
Count Jersey Numbers
Count how many of each jersey number we have
2
Check Distribution
Verify if we can give each team unique jersey numbers
Key Takeaway
๐ŸŽฏ Key Insight: Mathematical validation is much faster than trying all combinations - just count frequencies and check if distribution is possible!
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
24.5K Views
Medium Frequency
~15 min Avg. Time
847 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