Java | Cakewalk Solution 🍰 | 100% Faster
IntuitionWe need to split the given array nums into two equal halves, nums1 and nums2, while ensuring that both contain distinct elements.To do this, no element
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to split the given array nums into two equal halves, nums1 and nums2, while ensuring that both contain distinct elements.

To do this, no element in nums should appear more than twice. If any number appears more than twice, we cannot form two sets where all elements are distinct.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Using a Frequency Array:

    - Since the given numbers are constrained between [1, 100], we can use a frequency array arr[101] instead of a HashMap to avoid O(n) space complexity.

    - Iterate through nums and update the frequency count in arr[num].

    - If any number appears more than twice (arr[num] > 2), return false immediately.

    - If all numbers appear at most twice, return true.

# Complexity
- Time complexity: O(n) for single pass.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1) for creating arr[101].
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public boolean isPossibleToSplit(int[] nums) {
        // HashMap<Integer,Integer> map = new HashMap<>();
        // for(int num : nums){
        //     map.put(num,map.getOrDefault(num,0)+1);
        // }
        // for(int k : map.keySet()){
        //     if(map.get(k)>2){
        //         return false;
        //     }
        // }
        // return true;

        // O(n),O(1)
        // Store count of digits [1-100] according to constraints
        // as index 0 and missing elements in array
        // will be by default 0
        int[] arr = new int[101]; 
        for(int num : nums){
            arr[num]++;
            // Condition: if count>2 which means splitted array
            // won't have distinct elements
            if(arr[num]>2){
                return false;
            }
        }
        return true;
    }
}
```
['Array', 'Math', 'Java']