For a more general formula, you could take a look at @rwightman’s median pooling implementation 4.
This would calculate the padding for a 2-dimensional input, so you could remove one spatial dim for your use case.