✅☑[C++/C/Java/Python/JavaScript] || Easy Approach || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n1. Findme Function:\n\n    - This function calculates the sum of difference
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n*(Also explained in the code)*\n\n1. **Findme Function:**\n\n    - This function calculates the sum of differences between elements within a given range [l, r] in the sorted \'nums\' array.\n    - It computes two parts:\n        - \'first\' holds the sum of elements up to the middle element minus the sum till the left boundary.\n        - \'second\' holds the sum of elements from the middle to the end minus the middle element\'s value times the remaining count.\n1. **maxFrequencyScore Function:**\n\n    - Sorts the input array \'nums\' in non-decreasing order.\n    - Computes the prefix sum of the sorted \'nums\' array and stores it in the \'prefix\' vector.\n    - Iterates through each element of \'nums\' to find the maximum length of subarrays.\n    - For each \'i\', it performs a binary search to find the right boundary \'ans\' for the subarray such that the sum of differences doesn\'t exceed \'k\'.\n    - Updates \'maxi\' with the maximum subarray length found so far.\n\n\n\n# Complexity\n- Time complexity:\n   $$O(nlogn)$$\n    \n\n- Space complexity:\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Function to calculate the sum of differences in a range [l, r] in the sorted \'nums\' array\n    long long findme(int l, int r, vector<int>& nums, vector<long long>& prefix) {\n        int mid = (r + l) / 2;\n        long long first = prefix[mid];\n\n        // Adjusting the \'first\' by subtracting the prefix sum till the left boundary \'l\'\n        if (l > 0) {\n            first -= prefix[l - 1];\n        }\n\n        long long ans = (1LL * nums[mid] * (mid - l + 1)) - first;\n\n        // Calculating the second part of the range (right side)\n        long long second = prefix[r] - prefix[mid];\n        ans += second - (1LL * nums[mid] * (r - mid));\n\n        return ans;\n    }\n    \n    // Function to find the maximum length of subarray satisfying the conditions\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        vector<long long> prefix(nums.size());\n\n        // Calculating the prefix sum of the sorted \'nums\' array\n        for (int i = 0; i < n; ++i) {\n            if (i == 0) {\n                prefix[i] = nums[i];\n            } else {\n                prefix[i] = nums[i] + prefix[i - 1];\n            }\n        }\n\n        int maxi = 0;\n\n        // Iterating through each element to find the maximum length of subarray\n        for (int i = 0; i < n; ++i) {\n            int low = i;\n            int high = n - 1;\n            int ans = -1;\n\n            // Binary search to find the right boundary of the subarray\n            while (low <= high) {\n                int mid = low + (high - low) / 2;\n                if (findme(i, mid, nums, prefix) <= k) {\n                    ans = mid;\n                    low = mid + 1;\n                } else {\n                    high = mid - 1;\n                }\n            }\n\n            // Update the maximum length found so far\n            if (ans != -1) {\n                maxi = max(maxi, ans - i + 1);\n            }\n        }\n        return maxi;\n    }\n};\n\n\n\n```\n```C []\n#include <stdio.h>\n#include <stdlib.h>\n\nlong long findme(int l, int r, int *nums, long long *prefix, int size) {\n    int mid = (r + l) / 2;\n    long long first = prefix[mid];\n\n    if (l > 0) {\n        first -= prefix[l - 1];\n    }\n\n    long long ans = (1LL * nums[mid] * (mid - l + 1)) - first;\n\n    long long second = prefix[r] - prefix[mid];\n    ans += second - (1LL * nums[mid] * (r - mid));\n\n    return ans;\n}\n\nint maxFrequencyScore(int *nums, int size, long long k) {\n    qsort(nums, size, sizeof(int), compare);\n    long long *prefix = (long long *)malloc(size * sizeof(long long));\n\n    for (int i = 0; i < size; ++i) {\n        if (i == 0) {\n            prefix[i] = nums[i];\n        } else {\n            prefix[i] = nums[i] + prefix[i - 1];\n        }\n    }\n\n    int maxi = 0;\n\n    for (int i = 0; i < size; ++i) {\n        int low = i;\n        int high = size - 1;\n        int ans = -1;\n\n        while (low <= high) {\n            int mid = low + (high - low) / 2;\n            if (findme(i, mid, nums, prefix, size) <= k) {\n                ans = mid;\n                low = mid + 1;\n            } else {\n                high = mid - 1;\n            }\n        }\n\n        if (ans != -1) {\n            maxi = maxi > (ans - i + 1) ? maxi : (ans - i + 1);\n        }\n    }\n    free(prefix);\n    return maxi;\n}\n\n\n\n\n```\n```Java []\nimport java.util.Arrays;\n\nclass Solution {\n    public long findme(int l, int r, int[] nums, long[] prefix) {\n        int mid = (r + l) / 2;\n        long first = prefix[mid];\n\n        if (l > 0) {\n            first -= prefix[l - 1];\n        }\n\n        long ans = (1L * nums[mid] * (mid - l + 1)) - first;\n\n        long second = prefix[r] - prefix[mid];\n        ans += second - (1L * nums[mid] * (r - mid));\n\n        return ans;\n    }\n\n    public int maxFrequencyScore(int[] nums, long k) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        long[] prefix = new long[n];\n\n        for (int i = 0; i < n; ++i) {\n            prefix[i] = (i == 0) ? nums[i] : nums[i] + prefix[i - 1];\n        }\n\n        int maxi = 0;\n\n        for (int i = 0; i < n; ++i) {\n            int low = i;\n            int high = n - 1;\n            int ans = -1;\n\n            while (low <= high) {\n                int mid = low + (high - low) / 2;\n                if (findme(i, mid, nums, prefix) <= k) {\n                    ans = mid;\n                    low = mid + 1;\n                } else {\n                    high = mid - 1;\n                }\n            }\n\n            if (ans != -1) {\n                maxi = Math.max(maxi, ans - i + 1);\n            }\n        }\n        return maxi;\n    }\n}\n\n\n\n```\n```python3 []\nclass Solution:\n    def findme(self, l, r, nums, prefix):\n        mid = (r + l) // 2\n        first = prefix[mid]\n\n        if l > 0:\n            first -= prefix[l - 1]\n\n        ans = (nums[mid] * (mid - l + 1)) - first\n        second = prefix[r] - prefix[mid]\n        ans += second - (nums[mid] * (r - mid))\n        return ans\n\n    def maxFrequencyScore(self, nums, k):\n        nums.sort()\n        n = len(nums)\n        prefix = [0] * n\n\n        for i in range(n):\n            prefix[i] = nums[i] if i == 0 else nums[i] + prefix[i - 1]\n\n        maxi = 0\n        for i in range(n):\n            low = i\n            high = n - 1\n            ans = -1\n\n            while low <= high:\n                mid = low + (high - low) // 2\n                if self.findme(i, mid, nums, prefix) <= k:\n                    ans = mid\n                    low = mid + 1\n                else:\n                    high = mid - 1\n\n            if ans != -1:\n                maxi = max(maxi, ans - i + 1)\n        return maxi\n\n\n\n```\n```javascript []\nfunction findme(l, r, nums, prefix) {\n    let mid = Math.floor((r + l) / 2);\n    let first = prefix[mid];\n\n    if (l > 0) {\n        first -= prefix[l - 1];\n    }\n\n    let ans = nums[mid] * (mid - l + 1) - first;\n    let second = prefix[r] - prefix[mid];\n    ans += second - nums[mid] * (r - mid);\n    return ans;\n}\n\nfunction maxFrequencyScore(nums, k) {\n    nums.sort((a, b) => a - b);\n    let n = nums.length;\n    let prefix = new Array(n).fill(0);\n\n    for (let i = 0; i < n; ++i) {\n        prefix[i] = (i === 0) ? nums[i] : nums[i] + prefix[i - 1];\n    }\n\n    let maxi = 0;\n    for (let i = 0; i < n; ++i) {\n        let low = i;\n        let high = n - 1;\n        let ans = -1;\n\n        while (low <= high) {\n            let mid = Math.floor(low + (high - low) / 2);\n            if (findme(i, mid, nums, prefix) <= k) {\n                ans = mid;\n                low = mid + 1;\n            } else {\n                high = mid - 1;\n            }\n        }\n\n        if (ans !== -1) {\n            maxi = Math.max(maxi, ans - i + 1);\n        }\n    }\n    return maxi;\n}\n\n\n\n```\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Binary Search', 'C', 'Sorting', 'Prefix Sum', 'C++', 'Java', 'Python3', 'JavaScript']