LeetCode in Kotlin

3046. Split the Array

Easy

You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that:

Return true if it is possible to split the array, and false otherwise__.

Example 1:

Input: nums = [1,1,2,2,3,4]

Output: true

Explanation: One of the possible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4].

Example 2:

Input: nums = [1,1,1,1]

Output: false

Explanation: The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false.

Constraints:

Solution

class Solution {
    fun isPossibleToSplit(nums: IntArray): Boolean {
        val a = IntArray(101)
        for (n in nums) {
            a[n]++
            if (a[n] > 2) {
                return false
            }
        }
        return true
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy