• LQDOJ
  • Trang chủ
  • Bài tập
  • Bài nộp
  • Thành viên
  • Kỳ thi
  • Nhóm
  • Giới thiệu
    • Status
    • Khóa học
    • Đề xuất ý tưởng
    • Proposal Problem
    • Proposal Contest
    • Tools
    • Báo cáo tiêu cực
    • Báo cáo lỗi

Tiếng Việt

Tiếng Việt
English

Đăng nhập

Đăng ký

totunhienvinhhung126

  • Giới thiệu
  • Bài tập
  • Bài nộp

Rating
-
Bài tập
1
Điểm
525
Rating #
-
Điểm #
31949

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}")


«    »
Thứ 2
Thứ 3
Thứ 4
Thứ 5
Thứ 6
Thứ 7
CN
Ít
Nhiều

proudly powered by DMOJ| developed by LQDJudge team