Consider a N X N matrix where each element is row number divide by column number (integer division), i.e. mat[i][j] = floor((i+1)/(j+1)) where 0 <= i < n and 0 <= j < n. The task is to find the sum of all matrix element.
Input : N = 2 Output : 4 2 X 2 matrix with given constraint: 1 0 2 1 Sum of matrix element: 4 Input : N = 3 Output : 9
Method 1 (Brute Force):
Run two loops, one for row and another for column and find integer part of (i / j) and add to the answer.
Below is the implementation of this approach:
Method 2 (Efficient):
Let N = 9, matrix will be
Observe, for each jth column
mat[i][k] = 0, for 1 <= k < j, 1 <= i <= N
mat[i][k] = 1, for j <= k < 2*j, 1 <= i <= N
mat[i][k] = 2, for 2*j <= k < 3*j, 1 <= i <= N
and so on.
So, in each column i, there are i – 1 zeroes, followed by i times 1, followed by i times 2 and so on.
We traverse matrix column by column and sum elements.
Below is the implementation of this approach
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Minimum element of each row and each column in a matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Maximum sum of a Matrix where each value is from a unique row and column
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements
- Minimum operations required to make each row and column of matrix equals
- Print an N x M matrix such that each row and column has all the vowels in it
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Find a Square Matrix such that sum of elements in every row and column is K
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Find the original matrix when largest element in a row and a column are given
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Print all elements in sorted order from row and column wise sorted matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Find all matrix elements which are minimum in their row and maximum in their column
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Replace every matrix element with maximum of GCD of row or column