- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Place N^2 numbers in matrix such that every row has an equal sum in C++

In this problem, we are given an integer value N. our task is to print numbers within the range (1, N^{2}) in a 2D matrix of size NxN in such a way that the sum elements of each row are equal.

Let’s take an example to understand the problem,

**Input** − N = 4

**Output** −

1 6 11 16 2 7 12 13 3 8 9 14 4 5 10 15

Sum of elements in each row is 34

To solve this method, we need to place each element in the matrix in such a way that the total in each row is equal. For this, we will use the greedy approach and row by row to place the correct elements in place making the sum equal.

For this, we will initially feed all elements in the matrix and then create a new matrix that has elements of the previous matrix using this formula,

resultMat[i][j] = prevMat[j][(i+j)%n]

## Example

The below code shows the implementation of our solution,

#include<iostream> using namespace std; int main(){ int n = 4,i,j; cout<<"Matrix of size : "<<n<<" in which sum of elements of all rows is equal is :\n"; int prevMat[n][n], resultMat[n][n] ; int c = 1; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) prevMat[i][j] = c++; } for (i = 0; i < n; i++) { for (<) { resultMat[i][j] = prevMat[j][((i+j)%n)]; } } for (i = 0;i<n;i++) { for (j=0; j<n; j++) { cout<<resultMat[i][j]<<"\t"; } cout<<endl; } }

## Output

Matrix of size : 4 in which sum of elements of all rows is equal is : 1 6 11 16 2 7 12 13 3 8 9 14 4 5 10 15

- Related Questions & Answers
- Print N lines of numbers such that every pair among numbers has a GCD K
- Print an N x M matrix such that each row and column has all the vowels in it in C++
- Ways to place items in n^2 positions such that no row/column contains more than one in C++
- Count pairs in an array such that both elements has equal set bits in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Print n numbers such that their sum is a perfect square
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Reconstruct a 2-Row Binary Matrix in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- How to find the row that has maximum number of duplicates in an R matrix?
- Find an element in array such that sum of left array is equal to sum of right array using c++
- Place k elements such that minimum distance is maximized in C++
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming

Advertisements