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 minimum difference of max and mins after updating elements at most three times in Python
Suppose we have a list of numbers called nums, and we can perform an operation to update any element to any value. We can perform at most 3 such operations, and we need to find the resulting minimum difference between the max and min values in nums.
So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 2. We can change the list to [4, 3, 4, 5, 4, 4] and then 5 - 3 = 2.
Algorithm
To solve this problem, we follow these steps ?
- If size of
nums? 4, then return 0 (we can make all elements equal) - Sort the list
nums - Try all possible combinations of removing up to 3 elements from either end
- Return the minimum difference between max and min for the remaining elements
Example
Let's implement the solution step by step ?
class Solution:
def solve(self, nums):
if len(nums) <= 4:
return 0
nums.sort()
n = len(nums)
# Try all combinations of removing at most 3 elements from either end
min_diff = float('inf')
for i in range(4):
# Remove i elements from left, (3-i) elements from right
left = i
right = n - 1 - (3 - i)
min_diff = min(min_diff, nums[right] - nums[left])
return min_diff
# Test the solution
ob = Solution()
nums = [2, 3, 4, 5, 6, 7]
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"Output: {result}")
Input: [2, 3, 4, 5, 6, 7] Output: 2
How It Works
The key insight is that with 3 operations, we can effectively remove up to 3 elements from consideration. After sorting the array, we try all possible ways to remove elements:
- Remove 0 from left, 3 from right: Keep elements from index 0 to n-4
- Remove 1 from left, 2 from right: Keep elements from index 1 to n-3
- Remove 2 from left, 1 from right: Keep elements from index 2 to n-2
- Remove 3 from left, 0 from right: Keep elements from index 3 to n-1
Another Example
Let's test with a different array to see how the algorithm works ?
nums = [1, 5, 0, 10, 14]
ob = Solution()
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"After sorting: {sorted(nums)}")
print(f"Minimum difference: {result}")
Input: [1, 5, 0, 10, 14] After sorting: [0, 1, 5, 10, 14] Minimum difference: 1
Comparison of Approaches
| Strategy | Elements Removed | Remaining Range | Difference |
|---|---|---|---|
| Remove 3 largest | From right end | nums[0] to nums[n-4] | nums[n-4] - nums[0] |
| Remove 3 smallest | From left end | nums[3] to nums[n-1] | nums[n-1] - nums[3] |
| Mixed approach | From both ends | Variable range | Minimum of all combinations |
Conclusion
The solution sorts the array and tries all possible ways to remove up to 3 elements from either end. This ensures we find the minimum possible difference between max and min values after at most 3 operations.
