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 the sum of consecutive difference modulus of an array, i.e. the value of |A1-A0|+|A2-A1|+|A3-A2|+……+|An-1-An-2|+|An-A(n-1)| after removal of one element from the array, where An represents the nth index of an 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 on 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 an 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 ?> |
Javascript
<script> // Javascript implementation of above approach // Function to find the element function findMinRemoval(arr, n) { // Value variable for storing the total value var temp, value = 0; // Declaring maximum value as zero var maximum = 0; // If array contains on element if (n == 1) return 0; for ( var 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 var arr = [1, 5, 3, 2, 10]; var n = arr.length; document.write( findMinRemoval(arr, n) + "<br>" ); </script> |
7
Time Complexity: O(n)
Auxiliary Space: O(1)