Swift | O(n log n) solution
Swift version of Editorial 6 approach\n\n# Complexity\n- Time complexity: $O(n \log{n})$\n- Space complexity: $O(n)$\n\n# Code\n\nclass Solution {\n    func bea
# Swift version of Editorial 6 approach\n\n# Complexity\n- Time complexity: $O(n \\log{n})$\n- Space complexity: $O(n)$\n\n# Code\n```\nclass Solution {\n    func beautifulSubsets(_ nums: [Int], _ k: Int) -> Int\n    {\n        var totalCount = 1\n        var freqMap: [Int: [Int:Int]] = [:]\n\n        // Calculate frequencies based on remainder\n        for num in nums {\n            freqMap[num % k, default: [:]][num, default: 0] += 1\n        }\n\n        // Iterate through each remainder group\n        for group in freqMap.values\n        {\n            var prevNum = -k\n            var curr = 0, prev1 = 1, prev2 = 0\n            // Iterate through each number in the current remainder group\n            for (num, freq) in group.sorted { $0.key < $1.key }\n            {\n                // Count of subsets skipping the current number\n                var skip = prev1\n\n                // Count of subsets including the current number\n                var take: Int\n                // Check if the current number and the previous number form a beautiful pair\n                if num - prevNum == k {\n                    take = ((1 << freq) - 1) * prev2\n                }\n                else {\n                    take = ((1 << freq) - 1) * prev1\n                }\n\n                // Store the total count for the current number\n                curr = skip + take\n                (prev2, prev1) = (prev1, curr)\n                prevNum = num\n            }\n            totalCount *= curr\n        }\n        return totalCount - 1\n    }\n}\n```
['Array', 'Dynamic Programming', 'Swift', 'Sorting']