Find number of cavities in a matrix

Count the number of the cavity in the 2d matrix, a cavity is defined as all the surrounding number are greater than the mid number.

Examples:

Input : a = {{4, 5, 6}, {7, 1, 5}, {4, 5, 6}}
Output : 1

Input : a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output : 1

Source :Ola Interview Experience Set 13



Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program find number of cavities in a matrix
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 100;
  
int countCavities(int a[][MAX], int n)
{
    int A[n + 2][n + 2];
    int coun = 0;
  
    // form another matrix with one extra layer of 
    // boundary elements.
    // Boundary elements will contain max 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))
                A[i][j] = INT_MAX;
            else
                A[i][j] = a[i - 1][j - 1];
        }
    }
  
    // Check for cavities 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]))
                coun++;
        }
    }
  
    return coun;
}
  
int main()
{
    int a[][MAX] = { { 4, 5, 6 }, { 7, 1, 5 }, { 4, 5, 6 } };
    int n = 3;
    cout << countCavities(a, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program find number of cavities in a matrix
class GfG { 
  
static int MAX = 100
  
static int countCavities(int a[][], int n) 
    int A[][] = new int[n + 2][n + 2]; 
    int coun = 0
  
    // form another matrix with one extra layer of 
    // boundary elements. 
    // Boundary elements will contain max 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)) 
                A[i][j] = Integer.MAX_VALUE; 
            else
                A[i][j] = a[i - 1][j - 1]; 
        
    
  
    // Check for cavities 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])) 
                coun++; 
        
    
  
    return coun; 
  
public static void main(String[] args) 
    int a[][] = new int[][]{{ 4, 5, 6 }, { 7, 1, 5 }, { 4, 5, 6 }}; 
    int n = 3
System.out.println(countCavities(a, n)); 
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program find number of cavities in a matrix
using System;
  
class GfG 
  
    static int MAX = 100; 
  
    static int countCavities(int [,]a, int n) 
    
        int [,]A = new int[n + 2, n + 2]; 
        int coun = 0; 
  
        // form another matrix with one extra layer of 
        // boundary elements. 
        // Boundary elements will contain max 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)) 
                    A[i, j] = int.MaxValue; 
                else
                    A[i, j] = a[i - 1, j - 1]; 
            
        
  
        // Check for cavities 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])) 
                    coun++; 
            
        
        return coun; 
    
  
    public static void Main(String[] args) 
    
        int [,]a = new int[,]{{ 4, 5, 6 }, { 7, 1, 5 }, { 4, 5, 6 }}; 
        int n = 3; 
        Console.WriteLine(countCavities(a, n)); 
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


PHP

Output:

1

Optimizations We can avoid use of extra space and extra conditions by following below steps.
1) Explicitly check for four corner elements, remaining elements of first row, last row, first column and last column.
2) Check for remaining elements using above logic.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.