- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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, N2) 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 Articles
- Print an N x M matrix such that each row and column has all the vowels in it in C++
- Print N lines of numbers such that every pair among numbers has a GCD K
- Ways to place items in n^2 positions such that no row/column contains more than one in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Count pairs in an array such that both elements has equal set bits in C++
- Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix using c++
- Print n numbers such that their sum is a perfect square
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Count of elements of an array present in every row of NxM matrix in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- How to find the row that has maximum number of duplicates in an R matrix?
- Check if a number from every row can be selected such that xor of the numbers is greater than zero in C++
- Minimum numbers needed to express every integer below N as a sum in C++

Advertisements