Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Maximize sum of absolute difference between adjacent elements in Array with sum K
Next article icon

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

Last Updated : 18 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
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: 7 
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: 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>  

Output: 
7

 

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


Next Article
Maximize sum of absolute difference between adjacent elements in Array with sum K

S

Sairahul Jella
Improve
Article Tags :
  • Mathematical
  • DSA
  • Arrays
  • array-traversal-question
Practice Tags :
  • Arrays
  • Mathematical

Similar Reads

  • Find minimum difference with adjacent elements in Array
    Given an array A[] of N integers, the task is to find min(A[0], A[1], ..., A[i-1]) - min(A[i+1], A[i+2], ..., A[n-1]) for each i (1 ≤ i ≤ N). Note: If there are no elements at the left or right of i then consider the minimum element towards that part zero. Examples: Input: N = 4, A = {8, 4, 2, 6}Out
    12 min read
  • Minimize sum of squares of adjacent elements difference
    Given an Array arr[] or N elements, the task is to minimize the sum of squares of difference of adjacent elements by adding one element at any position of the array. Examples: Input: N = 4, arr = [4, 7, 1, 4]Output: 36Explanation: The sum of squares of difference of adjacent element before inserting
    7 min read
  • Maximize sum of absolute difference between adjacent elements in Array with sum K
    Given two integers N and K, the task is to maximize the sum of absolute differences between adjacent elements of an array of length N and sum K. Examples: Input: N = 5, K = 10 Output: 20 Explanation: The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adj
    4 min read
  • Array element with minimum sum of absolute differences | Set 2
    Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4
    7 min read
  • Modify array to maximize sum of adjacent differences
    Given an array, we need to modify the values of this array in such a way that the 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. Examples : Input : arr[] = [3, 2, 1, 4, 5] Output : 8 We can mod
    9 min read
  • Minimize sum of Array formed using given relation between adjacent elements
    Given a binary string S of length N, consisting of 0's and 1's, the task is to find the minimum sum of the array of non-negative integers of length N+1 created by following the below conditions: If the ith number in the given binary string is 0, then the (i + 1)th number in the array must be less th
    11 min read
  • Minimize the sum of MEX by removing all elements of array
    Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
    7 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
    11 min read
  • Rearrange array such that difference of adjacent elements is in descending order
    Given an array a[] with n integers the task is to rearrange the elements of the array in such a way that the differences of the adjacent elements are in descending order.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output : 6 1 5 2 4 3 Explanation: For first two elements the difference is abs(6-1)=5
    6 min read
  • Maximize difference between the Sum of the two halves of the Array after removal of N elements
    Given an integer N and array arr[] consisting of 3 * N integers, the task is to find the maximum difference between first half and second half of the array after the removal of exactly N elements from the array. Examples: Input: N = 2, arr[] = {3, 1, 4, 1, 5, 9}Output: 1Explanation:Removal of arr[1]
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences