
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find maximum difference of adjacent values after deleting k numbers in python
Suppose we have a list of numbers called nums and they are sorted in ascending order, we have to delete k values from the list such that the maximum difference between any two adjacent values is as minimum as possible, and finally find the difference.
So, if the input is like nums = [15, 20, 30, 400, 1500] k = 2, then the output will be 10, as if we remove [400, 1500] to get the difference of 20 and 30.
To solve this, we will follow these steps −
- abs_diff := a list of differences of every consecutive elements in nums
- Define a function dp() . This will take i, j, cnt
- if cnt is same as 0, then
- m := 0
- for k in range i to j, do
- m := maximum of m and abs_diff[k]
- return m
- return minimum of dp(i + 1, j, cnt - 1) and dp(i, j - 1, cnt - 1)
- From the main method do the following:
- return dp(0, size of abs_diff - 1, k)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums, k): abs_diff = [nums[i] - nums[i - 1] for i in range(1, len(nums))] def dp(i, j, cnt): if cnt == 0: m = 0 for k in range(i, j + 1): m = max(m, abs_diff[k]) return m return min(dp(i + 1, j, cnt - 1), dp(i, j - 1, cnt - 1)) return dp(0, len(abs_diff) - 1, k) ob = Solution() nums = [15, 20, 30, 400, 1500] k = 2 print(ob.solve(nums, k))
Input
[15, 20, 30, 400, 1500], 2
Output
10
- Related Articles
- Program to find minimum amplitude after deleting K elements in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find maximum additive score by deleting numbers in Python
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Find the k smallest numbers after deleting given elements in C++
- Find all possible substrings after deleting k characters in Python
- Find the k largest numbers after deleting the given elements in C++
- Program to find minimum possible maximum value after k operations in python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Program to find minimum amplitude after deleting KLength sublist in Python
- Program to find maximum adjacent absolute value sum after single reversal in C++
- Program to find minimum length of string after deleting similar ends in Python
- Program to find maximum sum by removing K numbers from ends in python
- Maximum possible middle element of the array after deleting exactly k elements in C++

Advertisements