Program to find maximum adjacent absolute value sum after single reversal in C++


Suppose we have a list of numbers called nums and we can reverse any sublist in the list at most once. After performing this operation, we have to find the maximum possible value of

$\displaystyle\sum\limits_{i=0}^{n-2}| nums[i+1]-[nums[i]|$

So, if the input is like nums = [2, 4, 6], then the output will be 6, as when we reverse [4, 6] we will get the list as [2, 6, 4] and the value |2 − 6| + |6 − 4| = 6

To solve this, we will follow these steps −

  • if size of nums <= 1, then −

    • return 0

  • ans := 0

  • n := size of nums

  • for initialize i := 1, when i < n, update (increase i by 1), do −

    • ans := ans + |nums[i] − nums[i − 1]|

  • orig := ans

  • for initialize i := 1, when i < n − 1, update (increase i by 1), do −

    • ans := maximum of ans and orig − |(nums[i] − nums[i + 1]| + |nums[0] − nums[i + 1]|

    • ans := maximum of ans and orig − |(nums[i] − nums[i − 1]| + |nums[n − 1] − nums[i − 1]|

  • pp := −|nums[1] − nums[0]|

  • pm := −|nums[1] − nums[0]|

  • mp := −|nums[1] − nums[0]|

  • mm := −|nums[1] − nums[0]|

  • for initialize j := 2, when j < n − 1, update (increase j by 1), do −

    • jerror := |nums[j + 1] − nums[j]|

    • ans := maximum of ans and (orig + pp − jerror − nums[j] − nums[j + 1])

    • ans := maximum of ans and (orig + pm − jerror − nums[j] + nums[j + 1])

    • ans := maximum of ans and (orig + mp − jerror + nums[j] − nums[j + 1])

    • ans := maximum of ans and (orig + mm − jerror + nums[j] + nums[j + 1])

    • pp := maximum of pp and −|nums[j] − nums[j − 1]|

    • pm := maximum of pm and −|nums[j] − nums[j − 1]|

    • mp := maximum of mp and −|nums[j] − nums[j − 1]|

    • mm := maximum of mm and −|nums[j] − nums[j − 1]|

  • return ans

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int>& nums) {
   if (nums.size() <= 1)
   return 0;
   int ans = 0;
   int n = nums.size();
   for (int i = 1; i < n; i++) {
      ans += abs(nums[i] − nums[i − 1]);
   }
   int orig = ans;
   for (int i = 1; i < n − 1; i++) {
      ans = max(ans, orig − abs(nums[i] − nums[i + 1]) +
      abs(nums[0] − nums[i + 1]));
      ans = max(ans, orig − abs(nums[i] − nums[i − 1]) + abs(nums[n
   − 1] − nums[i − 1]));
   }
   int pp = −abs(nums[1] − nums[0]) + nums[0] + nums[1];
   int pm = −abs(nums[1] − nums[0]) + nums[0] − nums[1];
   int mp = −abs(nums[1] − nums[0]) − nums[0] + nums[1];
   int mm = −abs(nums[1] − nums[0]) − nums[0] − nums[1];
   for (int j = 2; j < n − 1; j++) {
      int jerror = abs(nums[j + 1] − nums[j]);
      ans = max(ans, orig + pp − jerror − nums[j] − nums[j + 1]);
      ans = max(ans, orig + pm − jerror − nums[j] + nums[j + 1]);
      ans = max(ans, orig + mp − jerror + nums[j] − nums[j + 1]);
      ans = max(ans, orig + mm − jerror + nums[j] + nums[j + 1]);
      pp = max(pp, −abs(nums[j] − nums[j − 1]) + nums[j − 1] +
      nums[j]);
      pm = max(pm, −abs(nums[j] − nums[j − 1]) + nums[j − 1] −
      nums[j]);
      mp = max(mp, −abs(nums[j] − nums[j − 1]) − nums[j − 1] +
      nums[j]);
      mm = max(mm, −abs(nums[j] − nums[j − 1]) − nums[j − 1] −
      nums[j]);
   }
   return ans;
}
int main(){
   vector<int> v = {2, 4, 6};
   cout << solve(v);
}

Input

{2, 4, 6}

Output

6

Updated on: 26-Dec-2020

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements