Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find most occurring number after k increments in python
Suppose we have a list of numbers called nums and another value k. We can perform an operation where we increase some element by one, at most k times. We have to find the value of the most frequently occurring number we can obtain after these increments. If there are multiple solutions, we select the smallest possible number.
So, if the input is like nums = [1, 0, 0, 0, 8, 8, 8, 8] and k = 8, then the output will be 8. We can increase 1 seven times to get 8, and increase any 0 to 1, so we get [8, 1, 0, 0, 8, 8, 8, 8]. The result is 8 since it appears most frequently.
Algorithm Approach
To solve this problem, we use a sliding window technique with the following steps ?
- Sort the list nums to group similar values together
- Use two pointers (low and high) to maintain a sliding window
- Calculate the cost to make all elements in the window equal to the highest element
- Expand the window when possible, shrink when the cost exceeds k
- Track the window with maximum size and return its target value
Example Implementation
class Solution:
def solve(self, nums, k):
nums.sort()
low, high = 0, 0
dist = 0
best = 0
ret = -1
while high < len(nums):
if high > 0 and nums[high] != nums[high - 1]:
dist += (high - low) * (nums[high] - nums[high - 1])
high += 1
while dist > k:
dist -= nums[high - 1] - nums[low]
low += 1
if high - low > best:
best = high - low
ret = nums[high - 1]
return ret
# Test the solution
ob = Solution()
nums = [1, 0, 0, 0, 8, 8, 8, 8]
k = 8
result = ob.solve(nums, k)
print(f"Most frequent number after {k} increments: {result}")
Most frequent number after 8 increments: 8
How It Works
The algorithm maintains a sliding window of elements that can be made equal within k increments ?
- dist tracks the total cost to make all elements in the current window equal to the highest element
- When dist > k, we shrink the window from the left by incrementing low
- We expand the window by incrementing high and update the best result when we find a larger valid window
- The target value is always the rightmost element in the current window after sorting
Step-by-Step Example
def solve_with_steps(nums, k):
print(f"Original array: {nums}")
nums.sort()
print(f"Sorted array: {nums}")
low, high = 0, 0
dist = 0
best = 0
ret = -1
while high < len(nums):
if high > 0 and nums[high] != nums[high - 1]:
cost = (high - low) * (nums[high] - nums[high - 1])
dist += cost
print(f"Window [{low}:{high}], cost to make equal to {nums[high]}: {cost}, total: {dist}")
high += 1
while dist > k:
reduction = nums[high - 1] - nums[low]
dist -= reduction
low += 1
print(f"Reducing window, new range: [{low}:{high}], dist: {dist}")
if high - low > best:
best = high - low
ret = nums[high - 1]
print(f"New best: window size {best}, target value {ret}")
return ret
# Test with example
nums = [1, 0, 0, 0, 8, 8, 8, 8]
k = 8
result = solve_with_steps(nums, k)
print(f"\nFinal answer: {result}")
Original array: [1, 0, 0, 0, 8, 8, 8, 8] Sorted array: [0, 0, 0, 1, 8, 8, 8, 8] Window [0:4], cost to make equal to 1: 4, total: 4 New best: window size 4, target value 1 Window [0:5], cost to make equal to 8: 28, total: 32 Reducing window, new range: [1:5], dist: 24 Reducing window, new range: [2:5], dist: 16 Reducing window, new range: [3:5], dist: 9 Reducing window, new range: [4:5], dist: 1 New best: window size 5, target value 8 Final answer: 8
Conclusion
This sliding window approach efficiently finds the most frequent number achievable after k increments. The algorithm sorts the array first, then uses two pointers to maintain a valid window where all elements can be made equal within the given increment budget.
