The sliding window technique is used to find the longest subarray where elements can be incremented to make them equal using at most k
increments. We sort the array first to ensure it's easier to increment elements to become equal. We then use two pointers: one for the current end of the subarray and the other to represent the start. By checking the cost to transform the current subarray into an equal value using the difference between values and leveraging the sorted property, we can determine the maximum frequency we can achieve.
Time Complexity: O(n log n), due to sorting the array.
Space Complexity: O(1), as no extra space is used apart from variable allocations.
1#include <vector>
2#include <algorithm>
3#include <numeric>
4
5class Solution {
6public:
7 int maxFrequency(std::vector<int>& nums, int k) {
8 std::sort(nums.begin(), nums.end());
9 long long sum = 0;
10 int left = 0, res = 0;
11 for (int right = 0; right < nums.size(); ++right) {
12 sum += nums[right];
13 while (nums[right] * (right - left + 1LL) > sum + k) {
14 sum -= nums[left++];
15 }
16 res = std::max(res, right - left + 1);
17 }
18 return res;
19 }
20};
21
22// Sample Usage
23#include <iostream>
24
25int main() {
26 Solution sol;
27 std::vector<int> nums = {1, 2, 4};
28 int k = 5;
29 std::cout << sol.maxFrequency(nums, k) << std::endl;
30 return 0;
31}
The C++ solution mirrors the logic of the C solution but utilizes C++'s std::vector
for arrays and std::sort
for sorting. It uses a for loop to manage the right pointer and a while loop to shrink the left pointer's position if the condition is violated, updating the maximum feasible subarray size.
This approach makes use of binary search in combination with prefix sums to efficiently find the maximum frequency possible by transforming elements. We first sort the array. For a given target frequency, we check through calculating the required increment operations using a prefix sum array and binary searching over possible solutions. This allows us to leverage efficient range queries and reduce time complexity.
Time Complexity: O(n log n), because of sorting and binary search iterations.
Space Complexity: O(n), due to additional space for prefix sums.
1class Solution:
2 def maxFrequency(self, nums, k):
3 nums.sort()
4 prefix_sum = [0] * len(nums)
5 for i in range(len(nums)):
6 prefix_sum[i] = prefix_sum[i - 1] + nums[i] if i > 0 else nums[i]
7 def can_achieve(freq):
8 for end in range(freq - 1, len(nums)):
9 required = nums[end] * freq
10 if required - (prefix_sum[end] - (prefix_sum[end - freq] if end >= freq else 0)) <= k:
11 return True
12 return False
13 left, right, res = 1, len(nums), 1
14 while left <= right:
15 mid = (left + right) // 2
16 if can_achieve(mid):
17 res = mid
18 left = mid + 1
19 else:
20 right = mid - 1
21 return res
22
23# Sample Usage
24sol = Solution()
25print(sol.maxFrequency([1, 2, 4], 5))
The Python code efficiently utilizes sorting and binary search to scan through possible frequencies. By organizing prefix sums, it ensures quick calculation of how many increments are necessary to achieve uniform elements over a subarray, respecting the operations limit.