Sorting | Prefix Sum | Window Sliding | Binary Search
\n\nclass Solution {\npublic:\n    int maxFrequencyScore(vector<int>& a, long long k) \n    {\n        sort(a.begin(), a.end());\n        int l = 0, r = a.size(
\n```\nclass Solution {\npublic:\n    int maxFrequencyScore(vector<int>& a, long long k) \n    {\n        sort(a.begin(), a.end());\n        int l = 0, r = a.size(), ans = 1;\n        vector <long long> pre(a.size());\n        pre[0] = a[0];\n        for (int i = 1; i < a.size(); i++) pre[i] = pre[i-1] + a[i];\n        \n        while(l <= r)\n        {\n            int mid = (l+r)/2; \n            long long sm = 0;\n            bool ok = false;\n            for (int i = 0, j = 0; i < a.size(); i++)\n            {\n                sm += a[i];\n                if (i-j+1 == mid)\n                {\n                    int m = (i+j)/2;\n                    \n                    long long x = pre[m]-pre[j]+a[j], xl = (m-j)+1;\n                    long long y = pre[i]-pre[m]+a[m], yl = (i-m)+1;\n                    \n                    long long p = abs(x-a[m]*xl) + abs(y-a[m]*yl);\n                    if (p <= k) {\n                        ok = true; break;\n                    }\n                    j++;\n                }\n            }\n            if (ok) {\n                ans = mid;\n                l = mid+1;\n            }\n            else r = mid-1;\n        }\n        return ans;\n    }\n};\n\n```
['Two Pointers', 'Binary Search', 'Sliding Window', 'Python', 'C++']