Kotlin. Beats 100% (214 ms). DFS + static IntArray to exclude elements.
\n\n# Code\n\nclass Solution {\n    fun beautifulSubsets(nums: IntArray, k: Int, index: Int = 0, size: Int = 0): Int {\n        var result = 0\n        when {\n
![image.png](https://assets.leetcode.com/users/images/4affec41-6a1b-40a8-a30c-215fadc25439_1716461102.2506785.png)\n\n# Code\n```\nclass Solution {\n    fun beautifulSubsets(nums: IntArray, k: Int, index: Int = 0, size: Int = 0): Int {\n        var result = 0\n        when {\n            index < nums.size -> {\n                val offset = nums[index] + 1000\n                if (marked[offset] == 0) {\n                    marked[offset + k]++\n                    marked[offset - k]++\n                    result += beautifulSubsets(nums, k, index + 1, size + 1)\n                    marked[offset - k]--\n                    marked[offset + k]--\n                }\n                result += beautifulSubsets(nums, k, index + 1, size)\n            }\n            size > 0 -> result++\n        }\n        return result\n    }\n\n    companion object {\n        val marked = IntArray(3003)\n    }\n}\n```\n\nOne more attempt:\n\n![image.png](https://assets.leetcode.com/users/images/92dcfc81-7e3f-474f-8d10-0a3dcb82321e_1716471006.397202.png)\n\n```\nclass Solution {\n\n    var result = 0\n\n    fun beautifulSubsets(nums: IntArray, k: Int, index: Int = 0): Int {\n        if (index < nums.size) {\n            val offset = nums[index] + 1000\n            if (marked[offset] == 0) {\n                marked[offset - k]++\n                marked[offset + k]++\n                beautifulSubsets(nums, k, index + 1)\n                marked[offset + k]--\n                marked[offset - k]--\n                result++\n            }\n            beautifulSubsets(nums, k, index + 1)\n        }\n        return result\n    }\n\n    companion object {\n        val marked = IntArray(3003)\n    }\n}\n```
['Kotlin']