Program to find maximum difference of adjacent values after deleting k numbers in python

Given a sorted list of numbers, we need to delete k values to minimize the maximum difference between adjacent elements. This problem uses dynamic programming to find the optimal solution.

Problem Understanding

If we have nums = [15, 20, 30, 400, 1500] and k = 2, removing [400, 1500] leaves [15, 20, 30] with maximum adjacent difference of 10 (between 20 and 30).

Algorithm Steps

The approach follows these steps ?

  • Calculate differences between consecutive elements
  • Use dynamic programming to try removing elements from both ends
  • Find the minimum possible maximum difference

Example

class Solution:
    def solve(self, nums, k):
        # Calculate differences between consecutive elements
        abs_diff = [nums[i] - nums[i - 1] for i in range(1, len(nums))]
        
        def dp(i, j, cnt):
            # Base case: no more deletions needed
            if cnt == 0:
                max_diff = 0
                for idx in range(i, j + 1):
                    max_diff = max(max_diff, abs_diff[idx])
                return max_diff
            
            # Try removing from left or right
            return min(dp(i + 1, j, cnt - 1), dp(i, j - 1, cnt - 1))
        
        return dp(0, len(abs_diff) - 1, k)

# Test the solution
ob = Solution()
nums = [15, 20, 30, 400, 1500]
k = 2
result = ob.solve(nums, k)
print(f"Maximum difference after deleting {k} numbers: {result}")
Maximum difference after deleting 2 numbers: 10

How It Works

The algorithm works by ?

  • Difference Array: Creates array of consecutive differences [5, 10, 370, 1100]
  • Dynamic Programming: Recursively tries removing elements from both ends
  • Base Case: When no more deletions needed, finds maximum difference in remaining range

Step-by-Step Execution

nums = [15, 20, 30, 400, 1500]
k = 2

# Step 1: Calculate differences
differences = []
for i in range(1, len(nums)):
    diff = nums[i] - nums[i-1]
    differences.append(diff)
    print(f"nums[{i}] - nums[{i-1}] = {nums[i]} - {nums[i-1]} = {diff}")

print(f"Differences array: {differences}")
print(f"Need to delete {k} numbers to minimize max difference")
nums[1] - nums[0] = 20 - 15 = 5
nums[2] - nums[1] = 30 - 20 = 10
nums[3] - nums[2] = 400 - 30 = 370
nums[4] - nums[3] = 1500 - 400 = 1100
Differences array: [5, 10, 370, 1100]
Need to delete 2 numbers to minimize max difference

Alternative Approach

def find_min_max_difference(nums, k):
    n = len(nums)
    if k >= n - 1:
        return 0
    
    # Try all possible ways to keep (n-k) consecutive elements
    min_max_diff = float('inf')
    
    for i in range(k + 1):
        # Keep elements from index i to i + (n-k-1)
        start = i
        end = i + (n - k - 1)
        
        if end < n:
            max_diff = 0
            for j in range(start + 1, end + 1):
                max_diff = max(max_diff, nums[j] - nums[j-1])
            min_max_diff = min(min_max_diff, max_diff)
    
    return min_max_diff

# Test with example
nums = [15, 20, 30, 400, 1500]
k = 2
result = find_min_max_difference(nums, k)
print(f"Result: {result}")
Result: 10

Conclusion

This problem demonstrates dynamic programming where we recursively try different deletion strategies. The key insight is that we only need to consider removing elements from the ends to minimize the maximum adjacent difference.

Updated on: 2026-03-25T13:03:29+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements