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 space character.
- Algorithm :
- Traverse the array from start to end.
- Assign the outer loop to point the row and the inner row to traverse the elements of row.
- 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
- If the element is not boundary element print a blank space.
-
Implementation:
C++
// C++ program to print boundary element of// matrix.#include <bits/stdc++.h>usingnamespacestd;constintMAX = 100;voidprintBoundary(inta[][MAX],intm,intn){for(inti = 0; i < m; i++) {for(intj = 0; j < n; j++) {if(i == 0 || j == 0 || i == n - 1 || j == n - 1)cout << a[i][j] <<" ";elsecout <<" "<<" ";}cout <<"\n";}}// Driver codeintmain(){inta[4][MAX] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };printBoundary(a, 4, 4);return0;}chevron_rightfilter_noneJava
// JAVA Code for Boundary elements of a MatrixclassGFG {publicstaticvoidprintBoundary(inta[][],intm,intn){for(inti =0; i < m; i++) {for(intj =0; j < n; j++) {if(i ==0)System.out.print(a[i][j] +" ");elseif(i == m -1)System.out.print(a[i][j] +" ");elseif(j ==0)System.out.print(a[i][j] +" ");elseif(j == n -1)System.out.print(a[i][j] +" ");elseSystem.out.print(" ");}System.out.println("");}}/* Driver program to test above function */publicstaticvoidmain(String[] args){inta[][] = { {1,2,3,4}, {5,6,7,8}, {1,2,3,4}, {5,6,7,8} };printBoundary(a,4,4);}}// This code is contributed by Arnav Kr. Mandal.chevron_rightfilter_nonePython
# Python program to print boundary element# of the matrix.MAX=100defprintBoundary(a, m, n):foriinrange(m):forjinrange(n):if(i==0):printa[i][j],elif(i==m-1):printa[i][j],elif(j==0):printa[i][j],elif(j==n-1):printa[i][j],else:print" ",print# Driver codea=[ [1,2,3,4], [5,6,7,8],[1,2,3,4], [5,6,7,8] ]printBoundary(a,4,4)# This code is contributed by Sachin Bishtchevron_rightfilter_noneC#
// C# Code for Boundary// elements of a MatrixusingSystem;classGFG {publicstaticvoidprintBoundary(int[, ] a,intm,intn){for(inti = 0; i < m; i++) {for(intj = 0; j < n; j++) {if(i == 0)Console.Write(a[i, j] +" ");elseif(i == m - 1)Console.Write(a[i, j] +" ");elseif(j == 0)Console.Write(a[i, j] +" ");elseif(j == n - 1)Console.Write(a[i, j] +" ");elseConsole.Write(" ");}Console.WriteLine(" ");}}// Driver CodestaticpublicvoidMain(){int[, ] a = { { 1, 2, 3, 4 },{ 5, 6, 7, 8 },{ 1, 2, 3, 4 },{ 5, 6, 7, 8 } };printBoundary(a, 4, 4);}}// This code is contributed by ajitchevron_rightfilter_nonePHP
<?php// PHP program to print// boundary element of// matrix.$MAX= 100;functionprintBoundary($a,$m,$n){global$MAX;for($i= 0;$i<$m;$i++){for($j= 0;$j<$n;$j++){if($i== 0)echo$a[$i][$j]," ";elseif($i==$m- 1)echo$a[$i][$j]," ";elseif($j== 0)echo$a[$i][$j]," ";elseif($j==$n- 1)echo$a[$i][$j]," ";elseecho" "," ";}echo"\n";}}// Driver code$a=array(array( 1, 2, 3, 4 ),array( 5, 6, 7, 8 ),array( 1, 2, 3, 4 ),array( 5, 6, 7, 8 ));printBoundary($a, 4, 4);// This code is contributed// by akt_mit?>chevron_rightfilter_none
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.
- Time Complexity: O(n*n), where n is the size of array.
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 :
- Create a variable to store the sum and Traverse the array from start to end.
- Assign the outer loop to point the row and the inner row to traverse the elements of row.
- If the element lies in the boundary of matrix then add th element to the sum, i.e. if the element lies in 1st row, 1st column, last row, last column
- print the sum.
-
Implementation:
C++
// C++ program to find sum of boundary elements// of matrix.#include <bits/stdc++.h>usingnamespacestd;constintMAX = 100;intgetBoundarySum(inta[][MAX],intm,intn){longlongintsum = 0;for(inti = 0; i < m; i++) {for(intj = 0; j < n; j++) {if(i == 0)sum += a[i][j];elseif(i == m - 1)sum += a[i][j];elseif(j == 0)sum += a[i][j];elseif(j == n - 1)sum += a[i][j];}}returnsum;}// Driver codeintmain(){inta[][MAX] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };longlongintsum = getBoundarySum(a, 4, 4);cout <<"Sum of boundary elements is "<< sum;return0;}chevron_rightfilter_noneJava
// JAVA Code for Finding sum of boundary elementsclassGFG {publicstaticlonggetBoundarySum(inta[][],intm,intn){longsum =0;for(inti =0; i < m; i++) {for(intj =0; j < n; j++) {if(i ==0)sum += a[i][j];elseif(i == m -1)sum += a[i][j];elseif(j ==0)sum += a[i][j];elseif(j == n -1)sum += a[i][j];}}returnsum;}/* Driver program to test above function */publicstaticvoidmain(String[] args){inta[][] = { {1,2,3,4}, {5,6,7,8}, {1,2,3,4}, {5,6,7,8} };longsum = getBoundarySum(a,4,4);System.out.println("Sum of boundary elements"+" is "+ sum);}}// This code is contributed by Arnav Kr. Mandal.chevron_rightfilter_nonePython
# Python program to print boundary element# of the matrix.MAX=100defprintBoundary(a, m, n):sum=0foriinrange(m):forjinrange(n):if(i==0):sum+=a[i][j]elif(i==m-1):sum+=a[i][j]elif(j==0):sum+=a[i][j]elif(j==n-1):sum+=a[i][j]returnsum# Driver codea=[ [1,2,3,4], [5,6,7,8],[1,2,3,4], [5,6,7,8] ]sum=printBoundary(a,4,4)print"Sum of boundary elements is",sum# This code is contributed by Sachin Bishtchevron_rightfilter_noneC#
// C# Code for Finding sum// of boundary elementsusingSystem;classGFG {publicstaticlonggetBoundarySum(int[, ] a,intm,intn){longsum = 0;for(inti = 0; i < m; i++) {for(intj = 0; j < n; j++) {if(i == 0)sum += a[i, j];elseif(i == m - 1)sum += a[i, j];elseif(j == 0)sum += a[i, j];elseif(j == n - 1)sum += a[i, j];}}returnsum;}// Driver CodestaticpublicvoidMain(){int[, ] a = { { 1, 2, 3, 4 },{ 5, 6, 7, 8 },{ 1, 2, 3, 4 },{ 5, 6, 7, 8 } };longsum = getBoundarySum(a, 4, 4);Console.WriteLine("Sum of boundary"+" elements is "+ sum);}}// This code is contributed by ajitchevron_rightfilter_nonePHP
<?php// PHP program to find// sum of boundary// elements of matrix.functiongetBoundarySum($a,$m,$n){$sum= 0;for($i= 0;$i<$m;$i++){for($j= 0;$j<$n;$j++){if($i== 0)$sum+=$a[$i][$j];elseif($i==$m- 1)$sum+=$a[$i][$j];elseif($j== 0)$sum+=$a[$i][$j];elseif($j==$n- 1)$sum+=$a[$i][$j];}}return$sum;}// Driver code$a=array(array(1, 2, 3, 4),array(5, 6, 7, 8),array(1, 2, 3, 4),array(5, 6, 7, 8));$sum= getBoundarySum($a, 4, 4);echo"Sum of boundary elements is ",$sum;// This code is contributed by ajit?>chevron_rightfilter_none
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.
- Time Complexity: O(n*n), where n is the size of the array.
This article is contributed by Sarthak Kohli. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


