Maximum absolute difference of value and index sums in C

We are given an array of integers. The task is to calculate the maximum absolute difference of value and index sums. That is for each pair of indexes (i,j) in an array, we have to calculate |Arr[i] - Arr[j]| + |i - j| and find the maximum such sum possible. Here |A| means absolute value of A.

Syntax

int maxAbsDiff(int arr[], int n);
// Returns maximum value of |arr[i] - arr[j]| + |i - j| for all pairs (i,j)

Example 1: Finding Maximum Absolute Difference

Let's implement the brute force approach to find maximum absolute difference −

#include <stdio.h>
#include <stdlib.h>

int maxAbsDiff(int arr[], int n) {
    int result = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            int absDiff = abs(arr[i] - arr[j]) + abs(i - j);
            if (absDiff > result)
                result = absDiff;
        }
    }
    return result;
}

int main() {
    int arr[] = {1, 2, 4, 5};
    int n = 4;
    
    printf("Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br>");
    
    printf("Maximum absolute difference: %d<br>", maxAbsDiff(arr, n));
    return 0;
}
Array: 1 2 4 5 
Maximum absolute difference: 7

Example 2: Step-by-Step Calculation

This example shows how the calculation works for each pair −

#include <stdio.h>
#include <stdlib.h>

int maxAbsDiffWithSteps(int arr[], int n) {
    int result = 0;
    printf("Calculating |arr[i] - arr[j]| + |i - j| for all pairs:<br>");
    
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            int valueDiff = abs(arr[i] - arr[j]);
            int indexDiff = abs(i - j);
            int absDiff = valueDiff + indexDiff;
            
            printf("(%d,%d): |%d - %d| + |%d - %d| = %d + %d = %d<br>", 
                   i, j, arr[i], arr[j], i, j, valueDiff, indexDiff, absDiff);
            
            if (absDiff > result)
                result = absDiff;
        }
    }
    return result;
}

int main() {
    int arr[] = {10, 20, 21};
    int n = 3;
    
    int maxDiff = maxAbsDiffWithSteps(arr, n);
    printf("\nMaximum absolute difference: %d<br>", maxDiff);
    return 0;
}
Calculating |arr[i] - arr[j]| + |i - j| for all pairs:
(0,0): |10 - 10| + |0 - 0| = 0 + 0 = 0
(0,1): |10 - 20| + |0 - 1| = 10 + 1 = 11
(0,2): |10 - 21| + |0 - 2| = 11 + 2 = 13
(1,1): |20 - 20| + |1 - 1| = 0 + 0 = 0
(1,2): |20 - 21| + |1 - 2| = 1 + 1 = 2
(2,2): |21 - 21| + |2 - 2| = 0 + 0 = 0

Maximum absolute difference: 13

How It Works

  • For each pair of indices (i,j), calculate |arr[i] - arr[j]| + |i - j|
  • Keep track of the maximum sum encountered
  • The nested loops ensure all unique pairs are considered
  • Time complexity: O(n²), Space complexity: O(1)

Key Points

  • We only need to consider pairs where j ? i to avoid duplicates
  • The absolute value function abs() requires #include <stdlib.h>
  • Same index pairs (i,i) always contribute 0 to the sum

Conclusion

The maximum absolute difference problem finds the largest sum of value and index differences between any two elements. The brute force approach checks all pairs in O(n²) time to find the optimal solution.

Updated on: 2026-03-15T13:02:16+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements