Maximize Element Frequency
Updated
Problem statement
Given an array of positive integers nums and a positive integer k, you may perform at most k operations.
In one operation, choose one index and increase nums[i] by 1.
Return the maximum possible frequency of any value in the array after performing the operations. The frequency of a value is the number of times it appears.
Function
maxFrequency(nums: int[], k: int) → intExamples
Example 1
nums = [1,3,5,7,8,9,10,15]k = 6return = 4Increase 7 three times, 8 twice, and 9 once. The array then contains four occurrences of 10, using exactly 6 operations.
Example 2
nums = [1,2,4]k = 5return = 3Increase 1 to 4 using 3 operations and increase 2 to 4 using 2 operations. All three elements become 4.
Example 3
nums = [1,4,8,13]k = 5return = 2Two elements can be made equal, for example by increasing 8 to 13. Making any three elements equal requires more than 5 operations.
Example 4
nums = [3,9,6]k = 2return = 1No value can reach another array value with only 2 increments, so the maximum frequency remains 1.
Constraints
1 <= nums.length <= 10^51 <= nums[i] <= 10^51 <= k <= 10^5
More Goldman Sachs Onsite Interview questions
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Dungeon HealthONSITE INTERVIEW · Seen Sep 2026
- Insert, Delete, And Get Random In Constant TimeONSITE INTERVIEW · Seen Sep 2026
- Least Recently Used CacheONSITE INTERVIEW · Seen Sep 2026
- Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEW
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEW
- Count Zero Request ServersSalesforce · ONSITE INTERVIEW
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEW
- Unit-Weight Knapsack with Limited QuantitiesGoogle · ONSITE INTERVIEW
- Reduce Array Size to At Most HalfMicrosoft · ONSITE INTERVIEW
- Select K Paired Indices for Maximum ScoreAmazon · ONSITE INTERVIEW
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Removals for Non-Overlapping IntervalsByteDance · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Workers with Job AssignmentsLyft · ONSITE INTERVIEW