✅✅||Easy To Understand || C++ || Binary Search || Sliding Window || Prefix Sum ||
Intuition\n Describe your first thoughts on how to solve this problem. \nGiven a set of numbers (nums) and a limit of operations (k), the goal is to find the ma
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Given a set of numbers (nums) and a limit of operations (k), the goal is to find the maximum frequency score achievable within the constraint of k operations.\nFrequency Score: This score is obtained by making elements within a window of a certain length in the sorted array equal using a limited number of operations.**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1)possible function:**\n**Parameters:\nlen: Length being evaluated.\nnums: Input vector of integers.\nprefix: A vector storing cumulative sums of the sorted nums vector.\nk: A limit for the total operations allowed.\nn: Size of the input vector.\nReturns:\ntrue if it\'s possible to achieve the frequency score within the given limit k, false otherwise.\nFunctionality:\nIt iterates through the nums vector while maintaining a window of length len.\nFor each window, it calculates the operations needed to make all elements in the window equal.\nIf the total operations fall within the limit k, it returns true.\nUses prefix sums to calculate the operations efficiently.**\n\n**2)maxFrequencyScore function:\nParameters:\nnums: Input vector of integers.\nk: A limit for the total operations allowed.\nReturns:\nMaximum frequency score within the given limit k.\nFunctionality:\nSorts the nums vector.\nInitializes a prefix vector to store cumulative sums of the sorted nums.\nUses binary search to find the maximum possible length for which the condition in the possible function holds true.\nReturns the maximum length that satisfies the condition.**\n\n**3)Explanation:\nSorting: The input vector nums is sorted in non-decreasing order.\nPrefix Sum: prefix vector stores the cumulative sum of elements in nums.\nBinary Search: The maxFrequencyScore function performs a binary search to find the maximum possible length (mid) such that the possible function returns true.\npossible function: It calculates the operations needed to make elements within a window of length len equal and checks if these operations fall within the given limit k.\nReturn: The function returns the maximum length res found through the binary search.\nThe goal of this code seems to be finding the maximum frequency score achievable within the given limit of operations k, where a frequency score is determined by making elements within a window of a certain length in the input vector equal, using a limited number of operations.**\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(NlogN)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(N)$$\n# Code\n```\nclass Solution {\n    typedef long long int ll;\n    bool possible (int len,vector<int>& nums,vector<ll>& prefix,ll k, int n) {\n        if(len==0)\n            return false;\n        \n        int i=0;\n        int j=len-1;\n        \n        while(j<n) {\n            ll targetind=(i+j)/2;\n            ll target=nums[targetind];\n            \n            \n            ll operations_left=0;\n            ll operations_right=0;\n            \n            if(targetind==0){\n                operations_left=0;\n            } \n            else{\n                if(i>0){\n                    operations_left=abs(((targetind-i) * target)-(prefix[targetind-1]-prefix[i-1]));\n                }\n                else{\n                    operations_left=abs(((targetind-i) * target)-prefix[targetind-1]);\n                }\n            }\n            \n            operations_right=abs(((j-targetind)*target)-(prefix[j]-prefix[targetind]));\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    \npublic:\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        \n        vector<ll> prefix(n, 0);\n        prefix[0]=nums[0];\n        for (int i=1;i<n;i++) \n            prefix[i]=prefix[i-1]+nums[i];\n        \n        int left=1;\n        int right=n;\n        int res=1;\n        \n        while (left<=right) {\n            int mid=(left+right)/2;\n            \n            if (possible(mid,nums,prefix,k,n)) {\n                res=mid;\n                left=mid+1;\n            } else {\n                right=mid-1;\n            }\n        }\n        \n        return res;\n    }\n};\n\n```
['Array', 'Binary Search', 'Sliding Window', 'Sorting', 'Prefix Sum', 'C++']