Given an array, we need to modify values of this array in such a way that sum of absolute differences between two consecutive elements is maximized. If the value of an array element is X, then we can change it to either 1 or X.
Input : arr = [3, 2, 1, 4, 5] Output : 8 We can modify above array as, Modified arr = [3, 1, 1, 4, 1] Sum of differences = |1-3| + |1-1| + |4-1| + |1-4| = 8 Which is the maximum obtainable value among all choices of modification. Input : arr = [1, 8, 9] Output : 14
This problem is a variation of Assembly Line Scheduling and can be solved using dynamic programming. We need to maximize sum of differences each value X should be changed to either 1 or X. To achieve above stated condition we take a dp array of array length size with 2 columns, where dp[i] stores the maximum value of sum using first i elements only if ith array value is modified to 1 and dp[i] stores the maximum value of sum using first i elements if ith array value is kept as a[i] itself.Main thing to observe is,
Time Complexity : O(N)
Auxiliary Space : O(N)
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Modify array by merging elements with addition such that it consists of only Primes.
- Clustering/Partitioning an array such that sum of square differences is minimum
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Remove array end element to maximize the sum of product
- Maximize the sum of selected numbers from an array to make it empty
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Maximize the numbers of splits in an Array having sum divisible by 3
- Reverse a subarray to maximize sum of even-indexed elements of given array
- Maximize the Sum of a Subsequence from an Array based on given conditions
- Maximize Sum possible from an Array by the given moves
- Maximize sum of an Array by flipping sign of all elements of a single subarray
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximize sum by choosing elements from different section of a matrix
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Maximize sum of topmost elements of S stacks by popping at most N elements
- Maximize sum of all elements which are not a part of the Longest Increasing Subsequence