Find a Square Matrix such that sum of elements in every row and column is K
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.
Examples:
Input: N = 3, K = 15
Output:
2 7 6
9 5 1
4 3 8
Input: N = 3, K = 7
Output:
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:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to print the// required matrixvoid printMatrix(int n, int k){ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // Print k for the left // diagonal elements if (i == j) cout << k << " "; // Print 0 for the rest else cout << "0 "; } cout << "\n"; }}// Driver codeint main(){ int n = 3, k = 7; printMatrix(n, k); return (0);} |
Java
// Java implementation of the approachimport java.util.*;class GFG{// Function to print the required matrixstatic void printMatrix(int n, int k){ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // Print k for the left // diagonal elements if (i == j) System.out.print(k + " "); // Print 0 for the rest else System.out.print("0 "); } System.out.print("\n"); }}// Driver codepublic static void main(String[] args){ int n = 3, k = 7; printMatrix(n, k);}}// This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach# Function to print the# required matrixdef printMatrix(n, k) : for i in range(n) : for j in range(n) : # Print k for the left # diagonal elements if (i == j) : print(k, end = " "); # Print 0 for the rest else: print("0", end = " "); print();# Driver codeif __name__ == "__main__" : n = 3; k = 7; printMatrix(n, k);# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System; class GFG{// Function to print the required matrixstatic void printMatrix(int n, int k){ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // Print k for the left // diagonal elements if (i == j) Console.Write(k + " "); // Print 0 for the rest else Console.Write("0 "); } Console.Write("\n"); }}// Driver codepublic static void Main(String[] args){ int n = 3, k = 7; printMatrix(n, k);}}// This code is contributed by PrinciRaj1992 |
Javascript
<script>// javascript implementation of the approach// Function to print the required matrixfunction printMatrix(n , k){ for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { // Print k for the left // diagonal elements if (i == j) document.write(k + " "); // Prvar 0 for the rest else document.write("0 "); } document.write("</br>"); }}// Driver codevar n = 3, k = 7;printMatrix(n, k);// This code is contributed by 29AjayKumar</script> |
7 0 0 0 7 0 0 0 7
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



