Given an array of positive integers of size greater than 2. The task is to find the minimum value of sum of consecutive difference modulus of array i.e. the value of |A1-A0|+|A2-A1|+|A3-A2|+……+|An-1-An-2|+|An-An-1| after removal of one element from array, where An represent the nth index of array element value.
Examples:
Input: arr[] = [1, 5, 3, 2, 10]
Output: 7
On removing 10, we get B = {1, 5, 3, 2} i.e. |1-5|+|5-3|+|3-2| = 4+2+1 = 7Input: arr[] = [6, 12, 7, 8, 10, 15]
Output: 9
On removing 12, we get B = {6, 12, 7, 8, 10, 15} i.e. |6-7|+|7-8|+|8-10|+|10-15| = 1+1+2+5 = 9
The idea is to traverse the array from start to end, find the element in the array at which we get a maximum difference of consecutive modulus after its removal. Subtract the maximum value obtained from the total value calculated.
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find the element int findMinRemoval(int arr[], int n) { // Value variable for storing the total value int temp, value = 0; // Declaring maximum value as zero int maximum = 0; // If array contains on element if (n == 1) return 0; for (int i = 0; i < n; i++) { // Storing the maximum value in temp variable if (i != 0 && i != n - 1) { value = value + abs(arr[i] - arr[i + 1]); // Adding the adjacent difference modulus // values of removed element. Removing adjacent // difference modulus value after removing element temp = abs(arr[i] - arr[i + 1]) + abs(arr[i] - arr[i - 1]) - abs(arr[i - 1] - arr[i + 1]); } else if (i == 0) { value = value + abs(arr[i] - arr[i + 1]); temp = abs(arr[i] - arr[i + 1]); } else temp = abs(arr[i] - arr[i - 1]); maximum = max(maximum, temp); } // Returning total value-maximum value return (value - maximum); } // Drivers code int main() { int arr[] = { 1, 5, 3, 2, 10 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findMinRemoval(arr, n) << "\n"; return 0; } |
Java
// Java implementation of the approach class GFG { // Function to find the element static int findMinRemoval(int arr[], int n) { // Value variable for storing the total value int temp, value = 0; // Declaring maximum value as zero int maximum = 0; // If array contains on element if (n == 1) return 0; for (int i = 0; i < n; i++) { // Storing the maximum value in temp variable if (i != 0 && i != n - 1) { value = value + Math.abs(arr[i] - arr[i + 1]); // Adding the adjacent difference modulus // values of removed element. Removing adjacent // difference modulus value after removing element temp = Math.abs(arr[i] - arr[i + 1]) + Math.abs(arr[i] - arr[i - 1]) - Math.abs(arr[i - 1] - arr[i + 1]); } else if (i == 0) { value = value + Math.abs(arr[i] - arr[i + 1]); temp = Math.abs(arr[i] - arr[i + 1]); } else temp = Math.abs(arr[i] - arr[i - 1]); maximum = Math.max(maximum, temp); } // Returning total value-maximum value return (value - maximum); } // Drivers code public static void main(String[] args) { int arr[] = { 1, 5, 3, 2, 10 }; int n = arr.length; System.out.print(findMinRemoval(arr, n) + "\n"); } } // This code contributed by Rajput-Ji |
Python 3
# Python 3 implementation of above approach # Function to find the element def findMinRemoval(arr, n): # Value variable for storing the # total value value = 0 # Declaring maximum value as zero maximum = 0 # If array contains on element if (n == 1): return 0 for i in range( n): # Storing the maximum value in # temp variable if (i != 0 and i != n - 1): value = value + abs(arr[i] - arr[i + 1]) # Adding the adjacent difference modulus # values of removed element. Removing # adjacent difference modulus value after # removing element temp = (abs(arr[i] - arr[i + 1]) + abs(arr[i] - arr[i - 1]) - abs(arr[i - 1] - arr[i + 1])) elif (i == 0): value = value + abs(arr[i] - arr[i + 1]) temp = abs(arr[i] - arr[i + 1]) else: temp = abs(arr[i] - arr[i - 1]) maximum = max(maximum, temp) # Returning total value-maximum value return (value - maximum) # Drivers code if __name__ == "__main__": arr = [ 1, 5, 3, 2, 10 ] n = len(arr) print(findMinRemoval(arr, n)) # This code is contributed by ita_c |
C#
// C# implementation of the approach using System; class GFG { // Function to find the element static int findMinRemoval(int []arr, int n) { // Value variable for storing the total value int temp, value = 0; // Declaring maximum value as zero int maximum = 0; // If array contains on element if (n == 1) return 0; for (int i = 0; i < n; i++) { // Storing the maximum value in temp variable if (i != 0 && i != n - 1) { value = value + Math.Abs(arr[i] - arr[i + 1]); // Adding the adjacent difference modulus // values of removed element. Removing adjacent // difference modulus value after removing element temp = Math.Abs(arr[i] - arr[i + 1]) + Math.Abs(arr[i] - arr[i - 1]) - Math.Abs(arr[i - 1] - arr[i + 1]); } else if (i == 0) { value = value + Math.Abs(arr[i] - arr[i + 1]); temp = Math.Abs(arr[i] - arr[i + 1]); } else temp = Math.Abs(arr[i] - arr[i - 1]); maximum = Math.Max(maximum, temp); } // Returning total value-maximum value return (value - maximum); } // Driver code public static void Main() { int []arr = { 1, 5, 3, 2, 10 }; int n = arr.Length; Console.WriteLine(findMinRemoval(arr, n)); } } // This code contributed by Ryuga |
PHP
<?php // PHP implementation of above approach // Function to find the element function findMinRemoval($arr, $n) { // Value variable for storing the total value $value = 0; // Declaring maximum value as zero $maximum = 0; // If array contains on element if ($n == 1) return 0; $temp=0; for ($i = 0; $i < $n; $i++) { // Storing the maximum value in temp variable if ($i != 0 && $i != $n - 1) { $value = $value + abs($arr[$i] - $arr[$i + 1]); // Adding the adjacent difference modulus // values of removed element. Removing adjacent // difference modulus value after removing element $temp = abs($arr[$i] - $arr[$i + 1]) + abs($arr[$i] - $arr[$i - 1]) - abs($arr[$i - 1] - $arr[$i + 1]); } else if ($i == 0) { $value = $value + abs($arr[$i] - $arr[$i + 1]); $temp = abs($arr[$i] - $arr[$i + 1]); } else $temp = abs($arr[$i] - $arr[$i - 1]); $maximum = max($maximum, $temp); } // Returning total value-maximum value return ($value - $maximum); } // Drivers code $arr = array( 1, 5, 3, 2, 10 ); $n = count($arr); echo findMinRemoval($arr, $n); // This code is contributed by chandan_jnu ?> |
7
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.
Recommended Posts:
- Minimize the maximum minimum difference after one removal from array
- Maximum removal from array when removal time >= waiting time
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximize removal of adjacent array elements based on their absolute value
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Minimize the maximum difference between adjacent elements in an array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Reduce a given Binary Array to a single element by removal of Triplets
- Minimize the maximum difference of adjacent elements after at most K insertions
- Minimize the cost to make all the adjacent elements distinct in an Array
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Minimum peak elements from an array by their repeated removal at every iteration of the array
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Minimize the count of adjacent pairs with different parity
- Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Duplicates Removal in Array using BST
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

