# Construct a matrix with sum equal to the sum of diagonal elements

Given an integer N, the task is to construct a matrix of size N2 using positive and negative integers and excluding 0, such that the sum of the matrix is equal to the sum of the diagonal of the matrix.
Examples:

Input: N = 2
Output:
1 -2
2 4
Explanation:
Diagonal sum = (1 + 4) = 5
Matrix sum = (1 – 2 + 2 + 4) = 5
Input: N = 5
Output:
1 2 3 5 10
3 1 4 -9 1
-19 6 1 5 -8
4 -7 2 1 12
-17 1 1 1 1
Explanation:
Diagonal sum = (1 + 1 + 1 + 1 + 1) = 5
Matrix sum = 5

Approach:
The approach to solving the problem is to traverse all indices of the matrix and print a positive element(say y) at the N diagonal positions and equally distribute a single-valued positive and negative integer(say x and -x) in the remaining N2 – N positions.
Below is the implementation of the above approach:

## C++

 `// C++ program to implement ` `// the above approach ` `#include   ` `using` `namespace` `std; ` ` `  `// Function to construct matrix with ` `// diagonal sum equal to matrix sum ` `void` `constructmatrix(``int` `N) ` `{ ` `    ``bool` `check = ``true``; ` ` `  `    ``for` `(``int` `i = 0; i < N; i++) { ` ` `  `        ``for` `(``int` `j = 0; j < N; j++) { ` ` `  `            ``// If diagonal position ` `            ``if` `(i == j) { ` `                ``cout << 1 << ``" "``; ` `            ``} ` ` `  `            ``else` `if` `(check) { ` ` `  `                ``// Positve element ` `                ``cout << 2 << ``" "``; ` `                ``check = ``false``; ` `            ``} ` `            ``else` `{ ` ` `  `                ``// Negative element ` `                ``cout << -2 << ``" "``; ` `                ``check = ``true``; ` `            ``} ` `        ``} ` ` `  `        ``cout << endl; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 5; ` ` `  `    ``constructmatrix(5); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to implement ` `// the above approach ` `public` `class` `Main { ` ` `  `    ``// Function to construct matrix with ` `    ``// diagonal sum equal to matrix sum ` `    ``public` `static` `void` `constructmatrix(``int` `N) ` `    ``{ ` `        ``boolean` `check = ``true``; ` ` `  `        ``for` `(``int` `i = ``0``; i < N; i++) { ` ` `  `            ``for` `(``int` `j = ``0``; j < N; j++) { ` ` `  `                ``// If diagonal position ` `                ``if` `(i == j) { ` `                    ``System.out.print(``"1 "``); ` `                ``} ` `                ``else` `if` `(check) { ` ` `  `                    ``// Positve element ` `                    ``System.out.print(``"2 "``); ` `                    ``check = ``false``; ` `                ``} ` `                ``else` `{ ` `                    ``// Negative element ` `                    ``System.out.print(``"-2 "``); ` `                    ``check = ``true``; ` `                ``} ` `            ``} ` ` `  `            ``System.out.println(); ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `N = ``5``; ` ` `  `        ``constructmatrix(``5``); ` `    ``} ` `} `

## Python3

 `# Python3 program to implement  ` `# the above approach  ` ` `  `# Function to construct matrix with  ` `# diagonal sum equal to matrix sum  ` `def` `constructmatrix(N):  ` `     `  `    ``check ``=` `bool``(``True``)  ` ` `  `    ``for` `i ``in` `range``(N): ` `        ``for` `j ``in` `range``(N): ` ` `  `            ``# If diagonal position  ` `            ``if` `(i ``=``=` `j):  ` `                ``print``(``1``, end ``=` `" "``) ` ` `  `            ``elif` `(check):  ` ` `  `                ``# Positve element  ` `                ``print``(``2``, end ``=` `" "``) ` `                ``check ``=` `bool``(``False``)  ` `                 `  `            ``else``: ` ` `  `                ``# Negative element  ` `                ``print``(``-``2``, end ``=` `" "``) ` `                ``check ``=` `bool``(``True``)  ` ` `  `        ``print``() ` ` `  `# Driver code ` `N ``=` `5` `constructmatrix(``5``) ` ` `  `# This code is contributed by divyeshrabadiya07 `

## C#

 `// C# program to implement ` `// the above approach ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to construct matrix with ` `// diagonal sum equal to matrix sum ` `public` `static` `void` `constructmatrix(``int` `N) ` `{ ` `    ``bool` `check = ``true``; ` ` `  `    ``for``(``int` `i = 0; i < N; i++) ` `    ``{ ` `        ``for``(``int` `j = 0; j < N; j++) ` `        ``{ ` `             `  `            ``// If diagonal position ` `            ``if` `(i == j) ` `            ``{ ` `                ``Console.Write(``"1 "``); ` `            ``} ` `            ``else` `if` `(check) ` `            ``{ ` `                 `  `                ``// Positve element ` `                ``Console.Write(``"2 "``); ` `                ``check = ``false``; ` `            ``} ` `            ``else` `            ``{ ` `                 `  `                ``// Negative element ` `                ``Console.Write(``"-2 "``); ` `                ``check = ``true``; ` `            ``} ` `        ``} ` `        ``Console.WriteLine(); ` `    ``} ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `N = 5; ` ` `  `    ``constructmatrix(N); ` `} ` `} ` ` `  `// This code is contributed by piyush3010  `

Output:

```1 2 -2 2 -2
2 1 -2 2 -2
2 -2 1 2 -2
2 -2 2 1 -2
2 -2 2 -2 1
```

Time Complexity: O(N2
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.