Competitive ProgrammingHard

Maximum sum absolute difference

Detailed guide and Python implementation for the 'Maximum sum absolute difference' problem.

Problem Statement

Hard

Write a function max_sum_abs_diff(arr) that returns the maximum sum of absolute differences of consecutive elements in any circular permutation of the array arr.

Constraints
  • 2 <= len(arr) <= 1000
  • -10^4 <= arr[i] <= 10^4

Examples

Example 1
Input
max_sum_abs_diff([1, 2, 4, 8])
Output
18
Explanation

The circular permutation [1, 8, 2, 4] yields |1-8| + |8-2| + |2-4| + |4-1| = 7 + 6 + 2 + 3 = 18.

Example 2
Input
max_sum_abs_diff([10, 12])
Output
4
Explanation

The only circular permutation is [10, 12] yielding |10-12| + |12-10| = 2 + 2 = 4.

Need a Hint?
Consider using Greedy-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.