Observations || Diff Array + Prefix Sum || Easy to Understand
Observations :\n1. The maximum frequency that can be possible is the maxFreq that is present in the initial array + numOperations .\n\n2. The maximum element to
# Observations :\n1. The **maximum frequency** that can be possible is the **maxFreq that is present in the initial array** + **numOperations** .\n\n2. The maximum element to which nums[i] can extend is 1e5 + 1e5 as per the constraints .\n    \n3. We dont need to check for negative elements as all nums[i] are greater than 0 , so if any -ve no.(possible only by nums[i]-k) is having max freq after operations than freq of 0 will be our answer .\n\n# Approach\nAfter these operations , we can clearly code the solution , the another prerequisite was the concept of **difference array** (mostly used when operating in ranges ) .\nThe approach was **to use the difference array and the prefix sum technique (Line Sweep Algo) **to check frequenies of all the elements that can be possible by doing operations. We increase the frequency at arr[i] - k and decrease it at arr[i]+k+1, then apply the prefix sum to find the cumulative frequencies. Finally, we take the minimum of each possible frequency and the allowed frequency increase (freq[i] + numOperations) to calculate the maximum achievable frequency within the operation limit.\n\n# Note\nThe important thing here as compared to previous variant was the **memory limit constraint** , so for that iterate or check only those elements which can be possible instead of iterating whole diff array did previously.\n\nCheck here variant 1 : https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-i/solutions/6027201/observations-diff-array-easy/\n\n# Code\n```cpp []\n#define ll long long \nclass Solution {\npublic:\n    int maxFrequency(vector<int>& nums, int k, int numOperations) {\n        map<ll, int> diff;\n        map<ll, int> freq;\n        int maxFreq = 0;\n        \n        for(int ele : nums) {\n            ll mini = max(0LL, (ll)ele - k);\n            ll maxi = (ll)ele + k;\n            \n            diff[mini]++;\n            diff[maxi + 1]--;\n            freq[ele]++;\n            maxFreq = max(maxFreq, freq[ele]);\n        }\n        \n        int maxiAns = 0;\n        int curr_sum = 0;\n        \n        set<ll> p;\n        for(auto &[ele, f] : diff) p.insert(ele);\n        for(auto &[ele, f] : freq) p.insert(ele);\n        \n        for(ll ele : p) {\n            \n            curr_sum += diff[ele];\n            if(curr_sum > 0) {\n                maxiAns = max(maxiAns, min(curr_sum, freq[ele] + numOperations));\n            }\n        }\n        \n        return min(maxiAns, maxFreq + numOperations);\n    }\n};\n\n```
['Line Sweep', 'Prefix Sum', 'C++']