- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum value of |arr[i] – arr[j] - + |i – j| in C++
In this problem, we are given an array of n integers. Our task is to create a program that will find the maximum value of |arr[i]-arr[j]| + |i-j|.
Let’s take an example to understand the problem,
Input − array = {4, 1, 2}
Output − 4
Explanation −
|arr[0] - arr[1]|+|0-1| = |4-1| + |-1| = 3+1 = 4 |arr[0] - arr[2]|+|0-2| = |4-2| + |-2| = 2+2 = 4 |arr[1] - arr[2 ]|+|1-2| = |1-2| + |1-2| = 1+1 = 2
To solve this problem, a simple approach will be using the brute force approach which will be using two loops and finding the max difference.
But an efficient approach will be using the properties of the absolute function,
Let’s decode the equation and find the solution,
arr[i] - arr[j] + i - j = (arr[i] + i) - (arr[j] + j) arr[i] - arr[j] - i + j = (arr[i] - i) - (arr[j] - j) -arr[i] + arr[j] + i - j = -{(arr[i]-i) -(arr[j]-j)} -arr[i] + arr[j] - i + j = -{(arr[i]+i) - (arr[j]+j)}
First and forth are the same and the second and fourth are the same. Using this we will create two arrays that will store values arr[i]+- i.
array1 will store values arr[i] + i
array2 will store values arr[i] - i
So, we will find the maximum of two values that are
max ((max(array1)-min(array1)), (max(array2)-min(array2)))
Example
Program to show the implementation of our solution,
#include<iostream> using namespace std; int maxDiff(int arr[], int n) { int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans = max(ans, abs(arr[i] - arr[j]) + abs(i - j)); return ans; } int main() { int array[] = { 5, 7, 1, 2 }; int n = sizeof(array) / sizeof(array[0]); cout<<"The maximum value of |arr[i] - arr[j]| + |i-j| is "<<maxDiff(array, n); return 0; }
Output
The maximum value of |arr[i] - arr[j]| + |i-j| is 7
- Related Articles
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(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 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++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- 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++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++
