Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,210 views
in Online Assessments by Expert (34,270 points) | 1,210 views

1 Answer

1 like 0 dislike

Fill Matrix

 

Given a NxN matrix. Fill the integers from 1 to n*n to this matrix that makes the sum of each row, each column and two diagonals equal.

 

Example 1:

 

Input: n = 2
Output: null
Explanation: We need to fill [1, 2, 3, 4] into a 2x2 matrix, which is not possible so return null.

 

Example 2:

 

Input: n = 3
Output:
[[8, 3, 4],
 [1, 5, 9],
 [6, 7, 2]]
Explanation: We need to fill [1, 2, 3 ... 9] into a 3x3 matrix. There is one way to do it
Each row [8, 3, 4] [1, 5, 9] [6, 7, 2] sum is 15.
Each column [8, 1, 6] [3, 5, 7] [4, 9, 2] sum is 15.
The two diagonals [8, 5, 2] [4, 5, 6] sum is 15.
by Expert (34,270 points)