Maximum value of difference of a pair of elements and their Index | Practice | GeeksforGeeks

Given an array arr[] of N positive integers. Find maximum value of |arr[i] – arr[j]| + |i – j|, (0 <= i, j <= N – 1)
 
Example 1:
Input:
N = 4 
arr[] = {1, 2, 3, 1}
Output:
4
Explanation:
Choosing i=0 and j=2, will result in
|1-3|+|0-2| = 4, which is the maximum
possible value.

Example 2:
Input:
N = 3 
A[] = {1, 1, 1}
Output:
2
Explanation:
Choosing i=0 and j=2, will result in
|1-1|+|0-2| = 2, which is the maximum
possible value.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxValue() which takes an Integer N and an array arr of size N as input and returns the maximum possoble value of |arr[i] – arr[j]| + |i – j|.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 <= N <= 105
0 <= arr[i] <= 105
+1
diyer1711001 month ago
 
Here is a python code for reference -
    def maxValue(self, arr, N):
        a=[]
        b=[]
        for i in range(N):
            a.append(arr[i]+i)
            b.append(-arr[i]+i)

        mx=max(a)
        mn=min(a)
        mx1=max(b)
        mn1=min(b)
        return max(mx-mn,mx1-mn1)
+3
msf_venom0031 month ago
self note
property of mod will be used
after opening mode we have two cases
|x| = x , x >0
|x| = -x , x <0
 
as two mod values are used so it will be four cases 
 
when the first value is > 0 , and second is > 0
 
|A[i]-A[j]| = A[i]-A[j]
and
|i-j| = i- j 
 
which can be rewritten as (A[i]+i) - (A[j]+j)
similarly for second  case (-A[i]+i)- (-A[j]+j) (when first is <0 and and other is greater than 0. other two cases will match with it after rearranging , so in conclusion we need to 
maximize the A[i]+i and -A[i]+i and also find it minimum value so differnce will be largest at end we will have two max and 2 min and ans will be 
 
max( max1-min2,max2-min2)
int maxValue(int arr[], int N) {
        int max1 = INT_MIN,min1=INT_MAX;
        int max2 = INT_MIN,min2=INT_MAX;
        
        for(int i=0;i<N;++i){
            max1 = max(arr[i]+i,max1);
            min1 = min(arr[i]+i,min1);
            max2 = max(-arr[i]+i,max2);
            min2 = min(-arr[i]+i,min2);
            
        }
        return max((max1-min1),(max2-min2));
    }
 
 
 
+1
akshatshubhra2 months ago
C++ | 0ms| Easy Approach|
O(N)
O(N)
‘’'
class Solution { public:   int maxValue(int arr[], int N) {       // code here       int brr[N];       for(int i=0;i<N;i++){           brr[i]=arr[i]-i;           arr[i]+=i;       }       int a=*max_element(arr,arr+N);       int b=*min_element(arr,arr+N);       int c=*max_element(brr,brr+N);       int d=*min_element(brr,brr+N);       return max(a-b,c-d);   }};
‘’'
+7
badgujarsachin835 months ago
int maxValue(int arr[], int N) {
        // code here
      int min1=INT_MAX, min2=INT_MAX,max1=INT_MIN,max2=INT_MIN;
      for(int i=0;i<N;i++){
          min1=min(min1,i+arr[i]);
          max1=max(max1,i+arr[i]);
          min2=min(min2,i-arr[i]);
          max2=max(max2,i-arr[i]);
      }
      return max(max1-min1,max2-min2);
    }
+1
punitkumarojha6 months ago
Simple O(n) time approach with O(1) space
 
class Solution {
  public:
    int maxValue(int a[], int n) {
        int mn=a[0],mx=a[0];
        int ans=0;
        for(int i=1;i<n;i++){
            ans=max({ans,i+a[i]-mn,i-a[i]+mx});
            mn=min(mn,a[i]+i);
            mx=max(mx,a[i]-i);
        }
        return ans;
    }
};
-3
abhishekpanwar6976 months ago
int maxValue(int a[], int n) {    int p[n],q[n];    for(int i=0;i<n;i++)    {        p[i]=a[i]+i;        q[i]=a[i]-i;    }    sort(p,p+n);    sort(q,q+n);    int x=p[n-1]-p[0];    int y=q[n-1]-q[0];    return max(x,y);   }
0
rakeshthala6 months ago
NO extra space reqd.
class Solution { public:   int maxValue(int arr[], int N) {       // code here       int min1=INT_MAX,max1=INT_MIN,min2=INT_MAX,max2=INT_MIN;       for(int i=0; i<N;i++)       {           min1 = min(min1,i+arr[i]);           max1 = max(max1,i+arr[i]);           min2 = min(min2,i-arr[i]);           max2 = max(max2,i-arr[i]);       }       return max(max1-min1,max2-min2);   }};
0
Satyam8 months ago
Satyam
JAVA SOLUTION//class Solution {    static int maxValue(int[] arr, int N) {      int min1=Integer.MAX_VALUE;      int max1=Integer.MIN_VALUE;      int min2=Integer.MAX_VALUE;      int max2=Integer.MIN_VALUE;
      for(int i=0;i<n;i++){ if(arr[i]+i<min1)="" min1="arr[i]+i;" if(arr[i]+i="">max1)          max1=arr[i]+i;          if(arr[i]-i<min2) min2="arr[i]-i;" if(arr[i]-i="">max2)          max2=arr[i]-i;      }      return Math.max(Math.abs(max1-min1),Math.abs(max2-min2));    }};
-2
Gourav Modi10 months ago
Gourav Modi
https://ide.geeksforgeeks.o...
0
Gourav Modi
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.