#3046
Split the Array
EasyApproaches
💡
Intuition
Time UnknownSpace Unknown
We can try all possible ways to split the array into two halves and check if both halves contain distinct elements. This is straightforward but inefficient.
⚙️
Algorithm
3 steps- 1Step 1: Generate all possible combinations of splitting the array into two equal parts.
- 2Step 2: For each combination, check if both parts contain distinct elements.
- 3Step 3: If any valid combination is found, return true; otherwise, return false.
solution.py15 lines
1# Full working Python code
2from itertools import combinations
3
4def can_split(nums):
5 n = len(nums)
6 half = n // 2
7 for comb in combinations(nums, half):
8 nums1 = list(comb)
9 nums2 = [x for x in nums if x not in nums1]
10 if len(set(nums1)) == half and len(set(nums2)) == half:
11 return True
12 return False
13
14# Example usage
15print(can_split([1, 1, 2, 2, 3, 4])) # Output: TrueSolutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.