Maximum Frequency After Subarray Operation
Real-world version: Most Dials Reading k After One NudgeThe statement here is our own: the same inputs, outputs and technique.
Dial readings are given as nums.
Choose exactly one contiguous stretch of the readings and add the same whole number to every reading in it. That number may be negative, and it may be zero.
Return the greatest number of readings that can end up equal to k.
Examples
Example 1
- Input
- nums = [3, 7, 3, 9, 7, 3, 7], k = 7
- Output
- 4
Three readings already show 7. Adding four to the stretch covering positions 0 to 2 turns two threes into sevens while pushing the single 7 inside it off, for a net gain of one, giving four in all.
Example 2
- Input
- nums = [1, 1, 1, 1, 1], k = 2
- Output
- 5
Adding one to the whole list turns every reading into 2.
Example 3
- Input
- nums = [7, 7, 7, 7], k = 3
- Output
- 4
Every reading is the same, so one shift moves as many of them onto 3 as the stretch covers, and covering all four is best.
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 50
- 1 <= k <= 50
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_frequency(nums: list[int], k: int) -> int:public int maxFrequency(int[] nums, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Removing Minimum Number of Magic BeansReal-world version: Levelling the Seed SacksMedium
- Split Array Largest SumReal-world version: Balanced TruckloadsHard
- Check if All the Integers in a Range Are CoveredReal-world version: Patrol Coverage AuditEasy
- Longest Subsequence With Limited SumReal-world version: Parcels Within Each AllowanceEasy