From 1524366e77cbc3ade59fa0a9b56d3a275993e1f9 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 16 Nov 2024 21:20:02 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB3254.=20Find=20the=20Power=20?= =?UTF-8?q?of=20K-Size=20Subarrays=20I=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/ Signed-off-by: Matej Focko --- kt/find-the-power-of-k-size-subarrays-i.kt | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 kt/find-the-power-of-k-size-subarrays-i.kt diff --git a/kt/find-the-power-of-k-size-subarrays-i.kt b/kt/find-the-power-of-k-size-subarrays-i.kt new file mode 100644 index 0000000..9a3acb8 --- /dev/null +++ b/kt/find-the-power-of-k-size-subarrays-i.kt @@ -0,0 +1,29 @@ +class Solution { + fun resultsArray( + nums: IntArray, + k: Int, + ): IntArray { + if (k == 1) { + return nums + } + + val result = IntArray(nums.size - k + 1) { -1 } + (0.. + if (nums[i] == nums[i + 1] - 1) { + consecutive + 1 + } else { + 1 + } + } + .withIndex() + .drop(1) + .filter { + it.value >= k + } + .forEach { + result[it.index - k + 1] = nums[it.index] + } + return result + } +}