C++ CODE || EASY TO UNDERSTAND || MIX CONCEPT PROBLEMS || (B.S+P.S+S.W)||
\n# Complexity\n- Time complexity:\nO(N*log(n))\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution {\npublic:\n    bool isPossible(int len,long long K,vec
\n# Complexity\n- Time complexity:\n**O(N*log(n))**\n\n- Space complexity:\n**O(n)**\n\n# Code\n```\nclass Solution {\npublic:\n    bool isPossible(int len,long long K,vector<int>& nums){\n        //slidingwindow+prefixsum\n        vector<long long>prefix(nums.size(),0);\n        prefix[0]=nums[0];\n        int i=0,j=len-1;\n       \n        for(int l=1; l<nums.size(); l++){\n        prefix[l]=prefix[l-1]+nums[l];\n        }\n\n       while(j<nums.size()){\n            long long target_idx=(i+j)/2;\n            long long target=nums[target_idx];\n              long long operations_left  = 0;\n            long long 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            \n            operations_right    = abs(((j - target_idx) * target) - (prefix[j] - prefix[target_idx]));\n          \n            if(operations_right + operations_left <= K) {\n                return true;\n            }\n            \n            i++;\n            j++;\n            \n        }\n        \n        return false;\n       }\n\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        //binarysearch\n        sort(nums.begin(),nums.end());\n        int n=nums.size();\n        long long minfreq=1,maxfreq=n,ans=1;\n\n        while(minfreq<=maxfreq){\n            long long mid=(minfreq+maxfreq)/2;\n\n            if(isPossible(mid,k,nums)){\n                 ans=mid;\n                 minfreq=mid+1;\n            }else{\n                maxfreq=mid-1;\n            }\n        }\n        return ans;\n    }\n};\n\n```\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n![c83c425b-74b7-4651-b014-2310dac32c10_1702522958.7782986.gif](https://assets.leetcode.com/users/images/3d4d52a3-d4f8-44a5-8944-863be620f3a3_1703148792.342967.gif)\n\n\n\n\n\n\n\n\n
['Binary Search', 'Sliding Window', 'Prefix Sum', 'C++']