From 9ebce9977ce33d74483b5e8349a6b0a3364ead73 Mon Sep 17 00:00:00 2001 From: vaishnavim141003 <111603962+vaishnavim141003@users.noreply.github.com> Date: Tue, 31 Oct 2023 12:45:32 +0530 Subject: [PATCH] Create Sliding Cost #147 --- Sliding Cost #147 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Sliding Cost #147 diff --git a/Sliding Cost #147 b/Sliding Cost #147 new file mode 100644 index 0000000..5ee3ff3 --- /dev/null +++ b/Sliding Cost #147 @@ -0,0 +1,20 @@ +n, k = map(int, input().split()) +arr = list(map(int, input().split())) + +# Function to calculate the cost of making elements in the window equal +def calculate_window_cost(window): + median = sorted(window)[len(window) // 2] # Calculate the median element + cost = 0 + for elem in window: + cost += abs(elem - median) # Calculate cost as the absolute difference from the median + return cost + +costs = [] + +# Calculate the cost for each window of size k +for i in range(n - k + 1): + window = arr[i:i + k] + window_cost = calculate_window_cost(window) + costs.append(window_cost) + +print(*costs)