The problem at hand can be expressed as:
On inspection of various 3x3 matrix arrangements, we have the following:
Observation 1: To minimize k the diagonal elements must contain the least elements (row). From this we can conclude that:
Observation 2: To maximize k the row minimum elements must be in one line. From this we can conclude that:
From the above two observations we can conclude that:
Thus, considering the above observations to be true, we now have a valid range of k, outside of which we can directly conclude the final result to be -1.
Let in the test case, given k be $$$k_d$$$. Then for some $$$t$$$,
$$$(n-t)$$$ minimum row elements must be in the same column for $$$\lvert f(A) \rvert = k_d$$$. So,
Therefore for an intermediate k i.e. between observations 1 and 2, first $$$(k_d - n + 1)$$$ elements in range $$$[1, n] \; \cap \; \mathbb{Z}$$$ must be in same column in A.
Conclusion
subject to
The implementation can be found here.



