Open In App

C++ Program For Boundary Elements of a Matrix

Last Updated : 17 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Printing Boundary Elements of a Matrix.

Given a matrix of size n x m. Print the boundary elements of the matrix. Boundary elements are those elements which are not surrounded by elements on all four directions, i.e. elements in first row, first column, last row and last column. 

Examples: 

Input:
        1 2 3 4  
        5 6 7 8
        1 2 3 4
        5 6 7 8
Output: 
         1 2 3 4 
         5     8 
         1     4 
         5 6 7 8
Explanation:The boundary elements of the
matrix is printed.

Input:
        1 2 3   
        5 6 7 
        1 2 3 
Output: 
        1 2 3   
        5   7 
        1 2 3 
Explanation:The boundary elements of the 
matrix is printed.

Approach: The idea is simple. Traverse the matrix and check for every element if that element lies on the boundary or not, if yes then print the element else print the space character. 

  • Algorithm : 
    1. Traverse the array from start to end.
    2. Assign the outer loop to point the row and the inner row to traverse the elements of row.
    3. If the element lies in the boundary of matrix, then print the element, i.e. if the element lies in 1st row, 1st column, last row, last column
    4. If the element is not boundary element print a blank space.
  • Implementation:

C++




// C++ program to print boundary element 
// of matrix.
#include <iostream>
using namespace std;
  
const int MAX = 100;
  
void printBoundary(int a[][MAX], int m, int n)
{
    for (int i = 0; i < m; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
            if (i == 0 || j == 0 || 
                i == n - 1 || j == n - 1)
                cout << a[i][j] << " ";
            else
                cout << " "
                     << " ";
        }
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int a[4][MAX] = {{1, 2, 3, 4}, 
                     {5, 6, 7, 8}, 
                     {1, 2, 3, 4}, 
                     {5, 6, 7, 8}};
    printBoundary(a, 4, 4);
    return 0;
}


Output: 

1 2 3 4 
5     8 
1     4 
5 6 7 8
  • Complexity Analysis: 
    • Time Complexity: O(n*n), where n is the size of array. 
      This is achieved by single traversal of the matrix.
    • Space Complexity: O(1). 
      Since a constant space is needed.

Finding sum of boundary elements

Given an matrix of size n x m. Find the sum of boundary elements of the matrix. Boundary elements are those elements which is not surrounded by elements on all four directions, i.e. elements in first row, first column, last row and last column. 

Examples:  

Input:
        1 2 3 4  
        5 6 7 8
        1 2 3 4
        5 6 7 8
Output: 54
Explanation:The boundary elements of the matrix 
         1 2 3 4 
         5     8 
         1     4 
         5 6 7 8
Sum = 1+2+3+4+5+8+1+4+5+6+7+8 =54
Input:
        1 2 3   
        5 6 7 
        1 2 3 
Output: 24
Explanation:The boundary elements of the matrix
        1 2 3   
        5   7 
        1 2 3  
Sum = 1+2+3+5+7+1+2+3 = 24

Approach: The idea is simple. Traverse the matrix and check for every element if that element lies on the boundary or not, if yes then add them to get the sum of all the boundary elements. 

  • Algorithm : 
    1. Create a variable to store the sum and Traverse the array from start to end.
    2. Assign the outer loop to point the row and the inner row to traverse the elements of row.
    3. If the element lies in the boundary of matrix then add the element to the sum, i.e. if the element lies in 1st row, 1st column, last row, last column
    4. print the sum.
  • Implementation:

C++




// C++ program to find sum of boundary elements
// of matrix.
#include <iostream>
using namespace std;
  
const int MAX = 100;
  
int getBoundarySum(int a[][MAX], 
                   int m, int n)
{
    long long int sum = 0;
    for (int i = 0; i < m; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
            if (i == 0)
                sum += a[i][j];
            else if (i == m - 1)
                sum += a[i][j];
            else if (j == 0)
                sum += a[i][j];
            else if (j == n - 1)
                sum += a[i][j];
        }
    }
    return sum;
}
  
// Driver code
int main()
{
    int a[][MAX] = {{1, 2, 3, 4}, 
                    {5, 6, 7, 8}, 
                    {1, 2, 3, 4}, 
                    {5, 6, 7, 8}};
    long long int sum = getBoundarySum(a, 4, 4);
    cout << "Sum of boundary elements is " << 
             sum;
    return 0;
}


Output: 

Sum of boundary elements is 54
  • Complexity Analysis: 
    • Time Complexity: O(n*n), where n is the size of the array. 
      This is achieved by single traversal of the matrix.
    • Space Complexity: O(1). 
      Since a constant space is needed.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads