We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Cavity Map
  5. Discussions

Cavity Map

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 739 Discussions, By:

votes

Please Login in order to post a comment

  • Michael123
    7 years ago+ 21 comments

    no need to read the input as string . read it as int using "scanf("%1d",your array[i)[j]);" ...read only a single digit at a time

    46|
    Permalink
    View more Comments..
  • yashtekena
    6 years ago+ 5 comments

    Python Implementation:

    Just concentrate on (n-2)X(n-2) matrix as we need to ignore the borders

    n = int(input().strip())
    grid = []
    grid_i = 0
    for grid_i in range(n):
        grid_t = list(str(input().strip()))
        grid.append(grid_t)
    
    for i in range(1,(n-2)+1):
        for j in range(1,(n-2)+1):
            if grid[i][j]>max(grid[i-1][j],grid[i+1][j],grid[i][j-1],grid[i][j+1]):
                grid[i][j]='X'
        
    for i in range(n):
        print (''.join(grid[i]))  
    
    19|
    Permalink
    View more Comments..
  • _CAAI
    6 years ago+ 13 comments

    Instead of using two arrays, compare the center matrix (formed excluding boundary values) while printing the matrix..! ;)

    int n,i,j;
    scanf("%d",&n);
    int a[n][n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%1d",&a[i][j]);
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if( (i>=1)&&(i<n-1) && (j>=1)&&(j<n-1) )
            {
             if( (a[i][j] > a[i-1][j]) && (a[i][j] > a[i][j+1])      
             && (a[i][j] > a[i+1][j]) && (a[i][j] > a[i][j-1]) )
                printf("X");
             else
                printf("%d",a[i][j]);
            }
            else
                printf("%d",a[i][j]);
        }
        printf("\n");
    }
    
    14|
    Permalink
    View more Comments..
  • sayantanmaiti
    4 years ago+ 1 comment

    Why does Test Case #2 have spaces in the input grid? Although submitting the code that works for the Test Case #1 (one without the spaces) works fine for everything else, and gives the full 30 points,

    I kind-of wasted my time because my code was reading the spaces in the grid rather than the number itself.

    12|
    Permalink
  • cheesus
    7 years ago+ 2 comments

    the conditions are:

    1) is right or left of the current value a value that is greater or equals

    2) is above or below of the current value a value that is greater or equals

    3) is it in the first or last row or is it the first or last number

    if so it cant be a cavity

    I just created an inverse of a 2 dimensional array and checked those conditions for both matrices. Even its O(N*N) it doesnt needs a 1/10 of a second

    12|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature