Given two integers N and K, the task is to find an N x N square matrix such that sum of every row and column should be equal to K. Note that there can be multiple such matrices possible. Print any one of them.
Input: N = 3, K = 15
2 7 6
9 5 1
4 3 8
Input: N = 3, K = 7
7 0 0
0 7 0
0 0 7
Approach: An N x N matrix such that each left diagonal element is equal to K and rest elements are 0 will satisfy the given condition. In this way, the sum of the elements of the each row and column will be equal to K.
Below is the implementation of the above approach:
7 0 0 0 7 0 0 0 7
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Product of middle row and column in an odd square matrix
- Sum of matrix element where each elements is integer division of row and column
- Find column with maximum sum in a Matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- How to access elements of a Square Matrix
- Find maximum element of each column in a matrix
- Print all the sub diagonal elements of the given square matrix
- Find pair with maximum difference in any column of a Matrix
- Find if a binary matrix exists with given row and column sums
- Find the original matrix when largest element in a row and a column are given
- Print all the super diagonal elements of the given square matrix
- Row-wise common elements in two diagonals of a square matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.