Find the count of mountains in a given Matrix
Last Updated :
14 May, 2021
Given a 2D square matrix of size N X N, the task is to count the number of mountains in the matrix.
An element in a matrix is said to be a mountain when all the surrounding elements (in all 8 directions) are smaller than the given element.
Examples:
Input: matrix =
{ { 4, 5, 6 },
{ 2, 1, 3 },
{ 7, 8, 9 } }
Output: 1
Explanation
Only mountain element = 9.
All the neighbouring elements
1, 3 and 8 are smaller than 9.
Input: matrix =
{ { 7, 8, 9 },
{ 1, 2, 3 },
{ 4, 5, 6 } }
Output: 2
Explanation
Mountain elements = 6 (2, 3 and 5)
and 9 (8, 2, and 3)
Approach: The idea is to iterate through the matrix and at the same time check neighbouring elements in all possible 8 directions. If the element is greater than all of them then increment the counter variable. Finally, return the counter.
- Create an auxiliary array of size (N + 2) X (N + 2).
- Fill all the border elements with INT_MIN value
- In the remaining array space of N X N, copy the original matrix
- Now check if an element is greater than the elements in all the 8 directions.
- Count the number of such elements and print it.
For example:
If matrix =
{ { 7, 8, 9 },
{ 1, 2, 3 },
{ 4, 5, 6 } }
The auxiliary array would be
{ { 0, 0, 0, 0, 0 },
{ 0, 7, 8, 9, 0 },
{ 0, 1, 2, 3, 0 },
{ 0, 4, 5, 6, 0 },
{ 0, 0, 0, 0, 0 } }
Below is the implementation of above approach :
C++
// C++ program find the count of
// mountains in a given Matrix
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Function to count number of mountains
// in a given matrix of size n
int countMountains(int a[][MAX], int n)
{
int A[n + 2][n + 2];
int count = 0;
// form another matrix with one extra
// layer of border elements. Border
// elements will contain INT_MIN value.
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)) {
// For border elements,
// set value as INT_MIN
A[i][j] = INT_MIN;
}
else {
// For rest elements, just copy
// it into new matrix
A[i][j] = a[i - 1][j - 1];
}
}
}
// Check for mountains in the modified matrix
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
// check for all directions
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;
}
// Driver code
int main()
{
int a[][MAX] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
cout << countMountains(a, n);
return 0;
}
Java
// Java program find the count of
// mountains in a given Matrix
import java.util.*;
class GFG{
static int MAX = 100;
// Function to count number of mountains
// in a given matrix of size n
static int countMountains(int a[][], int n)
{
int [][]A = new int[n + 2][n + 2];
int count = 0;
// form another matrix with one extra
// layer of border elements. Border
// elements will contain Integer.MIN_VALUE value.
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)) {
// For border elements,
// set value as Integer.MIN_VALUE
A[i][j] = Integer.MIN_VALUE;
}
else {
// For rest elements, just copy
// it into new matrix
A[i][j] = a[i - 1][j - 1];
}
}
}
// Check for mountains in the modified matrix
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
// check for all directions
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;
}
// Driver code
public static void main(String[] args)
{
int a[][] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
System.out.print(countMountains(a, n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program find the count of
# mountains in a given Matrix
MAX = 100
# Function to count number of mountains
# in a given matrix of size n
def countMountains(a, n):
A = [[0 for i in range(n+2)] for i in range(n+2)]
count = 0
# form another matrix with one extra
# layer of border elements. Border
# elements will contain INT_MIN value.
for i in range(n+2):
for j in range(n+2):
if ((i == 0) or (j == 0) or
(i == n + 1) or (j == n + 1)):
# For border elements,
# set value as INT_MIN
A[i][j] = float('-inf')
else:
# For rest elements, just copy
# it into new matrix
A[i][j] = a[i - 1][j - 1]
# Check for mountains in the modified matrix
for i in range(n + 1):
for j in range(n + 1):
if ((A[i][j] > A[i - 1][j]) and
(A[i][j] > A[i + 1][j]) and
(A[i][j] > A[i][j - 1]) and
(A[i][j] > A[i][j + 1]) and
(A[i][j] > A[i - 1][j - 1]) and
(A[i][j] > A[i + 1][j + 1]) and
(A[i][j] > A[i - 1][j + 1]) and
(A[i][j] > A[i + 1][j - 1])):
count = count + 1
return count
# Driver code
a = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ]
n = 3
print(countMountains(a, n))
# This code is contributed by Sanjit_Prasad
C#
// C# program find the count of
// mountains in a given Matrix
using System;
class GFG{
static int MAX = 100;
// Function to count number of mountains
// in a given matrix of size n
static int countMountains(int [,]a, int n)
{
int [,]A = new int[n + 2,n + 2];
int count = 0;
// form another matrix with one extra
// layer of border elements. Border
// elements will contain Integer.MIN_VALUE value.
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)) {
// For border elements,
// set value as Integer.MIN_VALUE
A[i,j] = int.MinValue;
}
else {
// For rest elements, just copy
// it into new matrix
A[i,j] = a[i - 1,j - 1];
}
}
}
// Check for mountains in the modified matrix
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
// check for all directions
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;
}
// Driver code
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));
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
//Javascript program find the count of
// mountains in a given Matrix
// Function to count number of mountains
// in a given matrix of size n
function countMountains(a, n)
{
var A= new Array(n+2).fill(0).map(() => new Array(n+2).fill(0));
var count = 0;
// form another matrix with one extra
// layer of border elements. Border
// elements will contain INT_MIN value.
for (var i = 0; i < n + 2; i++) {
for (var j = 0; j < n + 2; j++) {
if ((i == 0) || (j == 0)
|| (i == n + 1)
|| (j == n + 1)) {
// For border elements,
// set value as INT_MIN
A[i][j] = Number.MIN_VALUE;
}
else {
// For rest elements, just copy
// it into new matrix
A[i][j] = a[i - 1][j - 1];
}
}
}
// Check for mountains in the modified matrix
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= n; j++) {
// check for all directions
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;
}
var a = [[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
var n = 3;
document.write( countMountains(a, n));
//This code is contributed by SoumikMondal
</script>
Performance Analysis:
- Time Complexity: In the above approach, we are doing two iterations. First one is on (N + 2) X (N + 2) elements to create the auxiliary matrix. Second one on N X N elements to find actual mountain element, so the time complexity is O(N X N).
- Auxiliary Space Complexity: In the above approach, we are using an auxiliary matrix of size (N + 2) X (N + 2), so Auxiliary space complexity is O(N *N).
Similar Reads
Count of submatrix with sum X in a given Matrix
Given a matrix of size N x M and an integer X, the task is to find the number of sub-squares in the matrix with sum of elements equal to X.Examples: Input: N = 4, M = 5, X = 10, arr[][]={{2, 4, 3, 2, 10}, {3, 1, 1, 1, 5}, {1, 1, 2, 1, 4}, {2, 1, 1, 1, 3}} Output: 3 Explanation: {10}, {{2, 4}, {3, 1}
15+ min read
Count of unique rows in a given Matrix
Given a 2D matrix arr of size N*M containing lowercase English letters, the task is to find the number of unique rows in the given matrix. Examples: Input: arr[][]= { {'a', 'b', 'c', 'd'}, {'a', 'e', 'f', 'r'}, {'a', 'b', 'c', 'd'}, {'z', 'c', 'e', 'f'} }Output: 2Explanation: The 2nd and the 4th row
10 min read
Count inversion pairs in a matrix
Given a matrix A of size NxN, we need to find the number of inversion pairs in it. Inversion count in a matrix is defined as the number of pairs satisfying the following conditions : x1 ? x2y1 ? y2A[x2][y2] < A[x1][y1] Constraints : 1 ? Ai,j ? 1091 ? N ? 103 Examples: For simplicity, let's take a
14 min read
Find all occurrences of a given word in a matrix
Given a 2D grid of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said be found in a direction if all characters match in this direction (not in zig-zag form). The solution should print all coordinates if a cycle is
12 min read
Count of palindromic plus paths in a given Matrix
Given an N x M matrix of integers, the task is to count the number of palindromic pluses in the array. Palindromic plus is formed when a palindromic sub-row and palindromic sub-column cross each other at the middle element. Examples: Input: matrix = [[1, 2, 1], [2, 3, 2], [3, 2, 1]] Output: 1 Explan
6 min read
Number of cells in a matrix that satisfy the given condition
Given an N * N grid consisting of empty cells (denoted by '1') and obstacles (denoted by '0'), the task is to find the number of empty cells in which a mirror can be placed to view the east-side view of grid from the south-side. Examples: Input: mat[][] = { {1, 1, 1}, {1, 1, 0}, {1, 0, 1}} Output: 2
11 min read
Queries for bitwise AND in the given matrix
Given an N * N matrix mat[][] consisting of non-negative integers and some queries consisting of top-left and bottom-right corner of the sub-matrix, the task is to find the bit-wise AND of all the elements of the sub-matrix given in each query. Examples: Input: mat[][] = { {1, 2, 3}, {4, 5, 6}, {7,
11 min read
Find regions with most common region size in a given boolean matrix
Given a boolean 2D array, arr[][] of size N*M where a group of connected 1s forms an island. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally. The task is to find the position of the top left corner of all the regions with the most common
12 min read
Find N in the given matrix that follows a pattern
Given an infinite matrix filled with the natural numbers as shown below: 1 2 4 7 . . . 3 5 8 . . . . 6 9 . . . . . 10 . . . . . . . . . . . . Also, given an integer N and the task is to find the row and the column of the integer N in the given matrix.Examples: Input: N = 5 Output: 2 2 5 is present i
8 min read
Find row with maximum and minimum number of zeroes in given Matrix
Given a 2D matrix containing only zeroes and ones, where each row is sorted. The task is to find the row with the maximum number of 0s and the row with minimum number of 0s.Example: Input: mat[][] = { {0, 1, 1, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}, {0, 0, 0, 0}} Output: Row with min zeroes: 3 Row with max
11 min read