Minimize sum of adjacent difference with removal of one element from array
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
Recommended Posts:
- Minimize the maximum minimum difference after one removal from array
- Search an element in an array where difference between adjacent elements is 1
- Minimize the difference between the maximum and minimum values of the modified array
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum adjacent difference in an array in its sorted form
- Minimum absolute difference of adjacent elements in a circular array
- Replace each element by the difference of the total size of the array and frequency of that element
- Maximum removal from array when removal time >= waiting time
- Maximum difference between first and last indexes of an element in array
- Sum of minimum absolute difference of each array element
- Minimize the absolute difference of sum of two subsets
- Minimize the maximum difference between the heights
- Minimize the difference between minimum and maximum elements
- Python Set difference to find lost element from a duplicated array
- Maximum sum of difference of adjacent elements
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.



