
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum value of |arr[0] β arr[1] - + |arr[1] β arr[2] - + β¦ +|arr[n β 2] β arr[n β 1] - when elements are from 1 to n in C++
In this problem, we are given an array of n integers of range [1,n]. Our task is to create a program that will find the maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1].
Let’s take an example to understand the problem,
Input − array= {1, 2, 3}
Output − 3
Explanation −
max sum is |1-3|+|2-1| = 3
To solve this problem, a simple approach is to create all permutations from the array. And find the maximum value of all the values from permutation. A more effective method is to generalize all the maximum values for each value of n and then create a general formula.
So,
Maximum sum for (n = 1) = 0 Maximum sum for (n = 2) = 1 Maximum sum for (n = 3) = 3 Maximum sum for (n = 4) = 7 Maximum sum for (n = 5) = 11 So, the maximum value is 0, 1, 3, 7, 11…
The general formula is, ((n*n/2)-1)
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int maxAbsVal(int n) { if (n == 1) return 0; return ((n*n/2) - 1); } int main() { int n = 4; cout<<"The maximum sum of absolute difference is "<<maxAbsVal(n); return 0; }
Output
The maximum sum of absolute difference is 7
- Related Articles
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Maximum value of |arr[i] β arr[j] - + |i β j| in C++
- Find Maximum value of abs(i β j) * min(arr[i], arr[j]) in an array arr[] in C++
- Maximize arr[j] β arr[i] + arr[l] β arr[k], such that i < j < k < l in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Maximize value of (arr[i] β i) β (arr[j] β j) in an array in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Rearrange an array such that βarr[j]β becomes βiβ if βarr[i]β is βjβ in C++
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++

Advertisements