totunhienvinhhung126
Rating
-
Bài tập
1
Điểm
125
Rating #
-
Điểm #
26093
Giới thiệu
def find_minimum_sum(arr):
# Sort the array first
n = len(arr)
arr.sort()
# Find median
if n % 2 == 0:
# If even length, any value between two middle elements works
# We'll use the left middle element
s = arr[n//2 - 1]
else:
# If odd length, use middle element
s = arr[n//2]
# Calculate minimum sum of absolute differences
min_sum = 0
for x in arr:
min_sum += abs(x - s)
return s, min_sum
Example usage
arr = [1, 8, 3, 4, 9, 2]
s, result = find_minimum_sum(arr)
print(f"Optimal value s = {s}")
print(f"Minimum sum = {result}")