Find the count of mountains in a given Matrix | C # program find the count of mountains in a given Matrix ; Function to count number of mountains in a given matrix of size n ; form another matrix with one extra layer of border elements . Border elements will contain Integer . MIN_VALUE value . ; For border elements , set value as Integer . MIN_VALUE ; For rest elements , just copy it into new matrix ; Check for mountains in the modified matrix ; check for all directions ; Driver code
using System ; class GFG { static int MAX = 100 ; static int countMountains ( int [ , ] a , int n ) { int [ , ] A = new int [ n + 2 , n + 2 ] ; int count = 0 ; for ( int i = 0 ; i < n + 2 ; i ++ ) { for ( int j = 0 ; j < n + 2 ; j ++ ) { if ( ( i == 0 ) || ( j == 0 ) || ( i == n + 1 ) || ( j == n + 1 ) ) { A [ i , j ] = int . MinValue ; } else { A [ i , j ] = a [ i - 1 , j - 1 ] ; } } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( ( A [ i , j ] > A [ i - 1 , j ] ) && ( A [ i , j ] > A [ i + 1 , j ] ) && ( A [ i , j ] > A [ i , j - 1 ] ) && ( A [ i , j ] > A [ i , j + 1 ] ) && ( A [ i , j ] > A [ i - 1 , j - 1 ] ) && ( A [ i , j ] > A [ i + 1 , j + 1 ] ) && ( A [ i , j ] > A [ i - 1 , j + 1 ] ) && ( A [ i , j ] > A [ i + 1 , j - 1 ] ) ) { count ++ ; } } } return count ; } public static void Main ( string [ ] args ) { int [ , ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int n = 3 ; Console . WriteLine ( countMountains ( a , n ) ) ; } }