We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Meanwhile, I'm sitting here failing to read the full prompt and trying to come up with a solution where I can find the cost of getting the magic matrix for any arbitrary n hahaha. Started myself thinking about how the magic matrix always has left and right eigenvector (1,..,1) meaning that X1 = s1 and X^T1 = s1 for some integer s in [1,n] and the cost is essentially the 1-norm of the difference ||X-A||. Turns out we can reframe the problem as
min{X \in {1,..,n}^{nxn}} ||X-A|| s.t. X1 = X^T1 = trace(X)1 = trace(X\cdot J)1 \in {1, ..., n^2}, where J_{i,j} = 0 for i != n-j and J_{i,n-i} = 1.
Using the new target sum_i sum_j Z_{i,j} with the contraints Z_{i,j} >= X_{i,j} - A_{i,j}, Z_{i,j} >= A_{i,j} - A_{i,j} and Z_{i,j} >= 0 we can come up with a pretty doable optimization problem up to n=10 I think.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
Meanwhile, I'm sitting here failing to read the full prompt and trying to come up with a solution where I can find the cost of getting the magic matrix for any arbitrary n hahaha. Started myself thinking about how the magic matrix always has left and right eigenvector (1,..,1) meaning that X1 = s1 and X^T1 = s1 for some integer s in [1,n] and the cost is essentially the 1-norm of the difference ||X-A||. Turns out we can reframe the problem as min{X \in {1,..,n}^{nxn}} ||X-A|| s.t. X1 = X^T1 = trace(X)1 = trace(X\cdot J)1 \in {1, ..., n^2}, where J_{i,j} = 0 for i != n-j and J_{i,n-i} = 1. Using the new target sum_i sum_j Z_{i,j} with the contraints Z_{i,j} >= X_{i,j} - A_{i,j}, Z_{i,j} >= A_{i,j} - A_{i,j} and Z_{i,j} >= 0 we can come up with a pretty doable optimization problem up to n=10 I think.