[LeetCode] Split the Array

3046. Split the Array

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**.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool isPossibleToSplit(vector<int>& nums) {
unordered_map<int, int> freq;
for(auto x : nums) {
if(++freq[x] > 2) return false;
}
return true;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/25/PS/LeetCode/split-the-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.