Back to Practice Dashboard
Competitive ProgrammingHard

Maximum sum absolute difference

Learn how to solve the 'Maximum sum absolute difference' problem. This detailed resource details brute force and optimized approaches.

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?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor