C++ transform_reduce
Calculate frequency, sort then apply transform/reduce\n\n# Code\ncpp []\nclass Solution {\n\npublic:\n    vector<int> findXSum(const vector<int>& nums, const in
Calculate frequency, sort then apply transform/reduce\n\n# Code\n```cpp []\nclass Solution {\n\npublic:\n    vector<int> findXSum(const vector<int>& nums, const int k, const int x) {\n        std::vector<int> res;\n        for (int i = 0; i < nums.size() - k + 1; ++i) {\n\n            unordered_map<int, int> freq;\n            for (int j = i; j != i + k && j < nums.size(); ++j) {\n                freq[nums[j]]++;\n            }\n\n            std::vector<std::pair<int, int>> pick;\n            for (const auto [k, v] : freq) {\n                pick.push_back({v, k});\n            }\n            std::sort(rbegin(pick), rend(pick));\n            const int size = std::min(x, (int)pick.size());\n            const int sum = std::transform_reduce(\n                begin(pick), begin(pick) + size, 0, std::plus<>(),\n                [](const auto item) { return item.first * item.second; });\n\n            res.push_back(sum);\n        }\n\n        return res;\n    }\n};\n```
['C++']