Python TypeError in GFG Minimize Heights II – missing positional argument ‘k’

10 hours ago 1
ARTICLE AD BOX

I am trying to solve the “Minimize the Heights II” problem from GeeksforGeeks using Python. The problem gives you a sequence of tower heights and requires modifying each tower height by either increasing or decreasing it by k exactly once and finding the minimum possible difference between the tallest and shortest towers.

I implemented a greedy approach where I sort the array and try to minimize the height difference by adjusting tower heights. However, when I submit the solution, I get a runtime error.

Problem Input

k = 2 arr = [1, 5, 8, 10]

Error Message

TypeError: Solution.getMinDiff() missing 1 required positional argument: '

Class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[n-1] - arr[0] small = arr[0] + k big = arr[n-1] - k if small > big: small, big = big, small for i in range(1, n-1): subtract = arr[i] - k add = arr[i] + k if subtract >= small or add <= big: continue if big - subtract <= add - small: small = subtract else: big = add return min(ans, big - small)

I verified the algorithm logic and tested locally by manually passing n. The code works locally but fails on GeeksforGeeks submission. I suspect the issue is related to function parameters or how the driver code calls the function.

The output for input [1, 5, 8, 10] with k = 2 should be 5.

Can someone explain why this error occurs and how to fix the function signature correctly for GFG?

Read Entire Article