Boundary elements of a matrix are the elements present in the first row, last row, first column, or last column.
- Boundary elements form the outermost layer of the matrix.
- The same boundary condition can be used to print or calculate the sum of these elements.
Examples:
Input:
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8Output:
1 2 3 4
5 8
1 4
5 6 7 8Explanation: The boundary elements of thematrix is printed.
Input:
1 2 3
5 6 7
1 2 3Output:
1 2 3
5 7
1 2 3Explanation: The boundary elements of thematrix is printed.
Approaches to Handle Boundary Elements of a Matrix
There are two approaches discussed in this article:
1. Printing Boundary Elements of a Matrix
Traverse every element of the matrix and check whether it belongs to the boundary. An element mat[i][j] is a boundary element if it belongs to the first row, last row, first column, or last column.
Algorithm
- Traverse the matrix using two nested loops.
- Check whether the current element lies in the first row, last row, first column, or last column.
- If it is a boundary element, print it.
- Otherwise, print spaces to preserve the matrix structure.
#include <iostream>
using namespace std;
const int MAX = 100;
// Function to print boundary elements
void printBoundary(int mat[][MAX], int rows, int cols)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == 0 || i == rows - 1 ||
j == 0 || j == cols - 1) {
cout << mat[i][j] << " ";
}
else {
cout << " ";
}
}
cout << '\n';
}
}
int main()
{
int mat[][MAX] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{1, 2, 3, 4},
{5, 6, 7, 8}
};
printBoundary(mat, 4, 4);
return 0;
}
Output
1 2 3 4 5 8 1 4 5 6 7 8
Explanation: The condition i == 0 || i == rows - 1 || j == 0 || j == cols - 1 identifies whether an element lies on the boundary. Boundary elements are printed, while the inner elements are replaced with spaces.
2. Finding the Sum of Boundary Elements
Given a matrix of size n × m, the task is to calculate the sum of all its boundary elements.
Example
Input:
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8Output:
54
Explanation:
The boundary elements are:
1 2 3 4 5 8 1 4 5 6 7 8
Therefore,
Sum = 1 + 2 + 3 + 4 + 5 + 8 + 1 + 4 + 5 + 6 + 7 + 8 = 54
Approach
Traverse every element of the matrix and add it to the sum if it lies on the boundary.
- Initialize sum to 0.
- Traverse every element of the matrix.
- Check whether the current element belongs to the first row, last row, first column, or last column.
- If it is a boundary element, add it to sum.
- Return and print the final sum.
#include <iostream>
using namespace std;
const int MAX = 100;
// Function to find the sum of boundary elements
long long boundarySum(int mat[][MAX], int rows, int cols)
{
long long sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == 0 || i == rows - 1 ||
j == 0 || j == cols - 1) {
sum += mat[i][j];
}
}
}
return sum;
}
int main()
{
int mat[][MAX] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{1, 2, 3, 4},
{5, 6, 7, 8}
};
cout << "Sum of boundary elements is "
<< boundarySum(mat, 4, 4);
return 0;
}
Output
Sum of boundary elements is 54
Explanation: The program traverses the entire matrix and uses the boundary condition to identify the required elements. Whenever an element belongs to the first row, last row, first column, or last column, it is added to sum.