🔥C++ || 🤗 Clean Implementation || Binary Search
Approach\nUsing Binary Search on the Answer from 1 to n\n\n\n# Complexity\n- Time complexity: O(n*log(n))\n\n\n- Space complexity: O(n)\n\n\n# Code\n\n/*\n\uD83
# Approach\nUsing `Binary Search` on the Answer from 1 to n\n![image.png](https://assets.leetcode.com/users/images/9feb77a1-d732-4765-8c85-aee1f92dc4a2_1710182294.844547.png)\n\n# Complexity\n- Time complexity: `O(n*log(n))`\n\n\n- Space complexity: `O(n)`\n\n\n# Code\n```\n/*\n\uD83D\uDD0EReference: codestorywithMik\n\nNote:\nBrute Force: for every element try + calculate operation required: TC->O(n2)\n\n\u26A1Binary search on the answer: as we can\'t able to optimized input\n\nminscore = 1 & maxscore = n\n\n*/\n\nclass Solution {\npublic:\n    typedef long long ll;\n    bool isPossible(ll len, vector<int>& nums, vector<ll>& prefix, ll k, int n){\n       \n        int i = 0;\n        int j = len-1;\n\n        while(j < n){\n            ll target_idx = (i + j)/2;\n            ll target = nums[target_idx];\n\n            ll operations_left = 0;\n            ll operations_right = 0;\n\n            if(target_idx == 0)\n                operations_left = 0;\n            else\n                operations_left = abs(((target_idx - i)*target) - (prefix[target_idx-1] - (i>0 ? prefix[i-1] : 0)));\n            \n            operations_right = abs(((j-target_idx)*target) - (prefix[j] - prefix[target_idx]));\n\n            if(operations_left + operations_right <= k)\n                return true;\n\n            //slide window\n            i++;\n            j++;\n        }\n        return false;\n    }\n\n\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        int n = nums.size();\n\n        sort(begin(nums), end(nums));\n\n        vector<ll> prefix(n, 0);\n        prefix[0] = nums[0];\n\n        for(int i=1; i<n; i++){\n            prefix[i] = prefix[i-1] + nums[i];\n        }\n\n        ll min_score = 1;\n        ll max_score = n; \n        ll result_score = 1;  //atleast single element is equal to itself\n\n        while(min_score <= max_score){\n            ll mid_score = min_score + (max_score - min_score)/2;\n\n            if(isPossible(mid_score, nums, prefix, k, n)){\n                result_score = mid_score;\n                min_score = mid_score + 1;\n            }\n            else\n                max_score = mid_score - 1;\n        }\n        return result_score;\n    }\n};\n\n\n```
['C++']