diff --git a/problems/split-array-into-consecutive-subsequences.kt b/problems/split-array-into-consecutive-subsequences.kt new file mode 100644 index 0000000..8ebabd7 --- /dev/null +++ b/problems/split-array-into-consecutive-subsequences.kt @@ -0,0 +1,37 @@ +class Solution { + private fun getFrequencies(nums: IntArray): MutableMap { + val freqs = mutableMapOf() + + nums.forEach { + freqs[it] = 1 + freqs.getOrDefault(it, 0) + } + + return freqs + } + + fun isPossible(nums: IntArray): Boolean { + val frequencies = getFrequencies(nums) + val sortedNumbers = frequencies.keys.toList().sorted() + + sortedNumbers.forEach { + while (frequencies[it]!! > 0) { + var last = 0 + var j = it + var k = 0 + + while (frequencies.getOrDefault(j, 0) >= last) { + last = frequencies[j]!! + frequencies[j] = frequencies[j]!! - 1 + + j++ + k++ + } + if (k < 3) { + return false + } + } + } + + return true + } +}