Open In App

Minimize sum of adjacent difference with removal of one element from array

Last Updated : 18 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:
On removing 10, we get B = {1, 5, 3, 2} i.e. |1-5|+|5-3|+|3-2| = 4+2+1 = 7

Input: arr[] = [6, 12, 7, 8, 10, 15] 
Output:
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>


Output: 

7

 

 Time Complexity: O(n)
Auxiliary Space: O(1) 



Similar Reads

Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element
Given an array arr[] consisting of N integers, the task is to find the remaining array element after subtracting each element from its next adjacent element and removing the last array element repeatedly. Examples: Input: arr[] = {3, 4, 2, 1}Output: 4Explanation:Operation 1: The array arr[] modifies to {4 - 3, 2 - 4, 1 - 2} = {1, -2, -1}.Operation
8 min read
Minimize removal from Array so that in any pair one element is multiple of other
Given an array arr[] of size N, the task is to count the minimum number of elements required to be removed from the given array such that whenever any pair (arr[i], arr[j]) is picked, where i != j and 0 ≤ i &lt; j &lt; N, either arr[i] is multiple of arr[j] or vice versa. Examples: Input: N = 5, arr[] = {4, 3, 4, 5, 2}Output: 2Explanation: Currentl
9 min read
Minimize the maximum minimum difference after one removal from array
Given an array arr[] of size n ? 3, the task is to find the minimum possible difference between the maximum and the minimum element from the array after removing one element. Examples: Input: arr[] = {1, 2, 3} Output: 1 Removing 1 will give 3 - 2 = 1 Removing 2, 3 - 1 = 2 And removing 3 will result in 2 - 1 = 1 Input: arr[] = {1, 2, 4, 3, 4} Output
11 min read
Minimize maximum difference between any pair of array elements by exactly one removal
Given an array arr[] consisting of N integers(N &gt; 2), the task is to minimize the maximum difference between any pair of elements (arr[i], arr[j]) by removing exactly one element. Examples: Input: arr[] = {1, 3, 4, 7}Output: 3Explanation:Removing the element 7 from array, modifies the array to {1, 3, 4}.Here (4, 1) has difference = 4 – 1 = 3 whi
9 min read
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements
Given an array arr[] consisting of N integers, the task is to print the last remaining array element after repeatedly selecting pairs of increasing adjacent elements (arr[i], arr[i + 1]) and removing any of the elements in the pair. If it is not possible to reduce the array to a single element, then print "Not Possible". Examples: Input: arr[] = {3
4 min read
Minimize removal of non-equal adjacent characters required to make a given string empty
Given a string S consisting of '(' and ')', the task is to find the minimum count of bracket reversals required to make the string an empty string by repeatedly removing a pair of non-equal adjacent characters. If it is not possible to empty the string, then print -1. Examples: Input: S = ")))((("Output: 0Explanation: Removing (S[2], S[3]) from S m
7 min read
Maximum removal from array when removal time &gt;= waiting time
Given there are N elements in an array. The task is to remove elements from the array from left to right. However, some time is required to remove an element from the array(let us call it removal time). The time to remove an element is equal to the value of that element in seconds.An element can only be removed when the time required to remove it(r
6 min read
Maximum sum in an array such that every element has exactly one adjacent element to it
Given an array arr[] of N integers, you can select some indexes such that every selected index has exactly one other selected index adjacent to it and the sum of elements at the chosen indexes should be maximum. In other words, the task is to select elements from an array such that a single element alone is not selected and elements at three consec
9 min read
Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
Given an array arr[] of size N and an integer K, the task is to check if all array elements can be made equal by removing an array element and incrementing the value of all other array elements such that the total sum of the array elements remains the same. If found to be true, then print "YES". Otherwise, print "NO". Examples : Input: arr[] = { 2,
7 min read
Remaining array element after repeated removal of the smallest element from pairs with absolute difference of 2 or 0
Given an array arr[] consisting of N positive integers, the task is to check if it is possible to reduce the size of the array to 1 by repeatedly removing the smallest element from a pair having absolute difference of 2 or 0 between them. If it is not possible to reduce, then print "-1". Otherwise, print the last remaining element in the array. Exa
6 min read