- 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

# C++ code to find out the sum of the special matrix elements

Suppose, we are given a square matrix of dimensions n * n. The following values of the matrix are called special elements −

Values that are in the main diagonal.

Values that are in the second diagonal.

Values of the row that has exactly (n - 1 / 2) rows above it and the same number of rows below it.

Values of the column that has exactly (n - 1 / 2) columns at its left and right.

We find out the sum of these special values in the matrix.

So, if the input is like n = 4, mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}, then the output will be 107.

## Steps

To solve this, we will follow these steps −

res := 0 for initialize i := 0, when i < n, update (increase i by 1), do: for initialize j := 0, when j < n, update (increase j by 1), do: if i is same as j or i is same as n / 2 or j is same as n/ 2 or i + j is same as n - 1, then: res := res + mat[i, j] print(res)

## Example

Let us see the following implementation to get better understanding

#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, vector<vector<int>> mat) { int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++){ if (i == j || i == n / 2 || j == n / 2 || i + j == n - 1) res += mat[i][j]; } cout << res << endl; } int main() { int n = 4; vector<vector<int>> mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; solve(n, mat); return 0; }

## Input

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

## Output

107

- Related Questions & Answers
- Python Program to find out the determinant of a given special matrix
- How to find the sum of all elements of a given matrix using Numpy?
- C++ code to find total elements in a matrix
- Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++
- How to find the sum of anti-diagonal elements in a matrix in R?
- Program to find out the sum of evenly spaced elements in an array in Python
- How to Find out the source code of a transaction in SAP?
- C++ code to find out the total amount of sales we made
- C Program to print the sum of boundary elements of a matrix
- Program to Find Out the Special Nodes in a Tree in Python
- Program to find out the sum of the number of divisor of the divisors in Python
- C++ code to find out number of battery combos
- Program to find out the number of special numbers in a given range in Python
- Program to find the sum of elements that forms a Z shape on matrix in Python
- C++ code to find total number of digits in special numbers

Advertisements