Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vaishnavim141003 committed Oct 31, 2023
1 parent 5b8a315 commit 9ebce99
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sliding Cost #147
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9ebce99

Please sign in to comment.