You are not logged in. Please login at www.codechef.com to post your questions!

×

Hackerrank 2D Array-DS Help

I was solving this problem: 2D Array My approach includes brute-force approach of selecting each element and checking it's neighbouring elements if they are same or not. But somehow my code is giving wrong answer. Here's my code:

#include <iostream>

using namespace std;

int main()
{
    int a[6][6];
    for(int i=0;i<6;i++)
        for(int j=0;j<6;j++)
            cin>>a[i][j];
    int sum,ans=0;
    for(int i=1;i<5;i++)
    {
        for(int j=1;j<5;j++)
        {
            if(a[i][j] != a[i-1][j] || 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] || a[i][j] != a[i+1][j+1])
               sum = a[i][j]+a[i-1][j]+a[i][j-1]+a[i-1][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1];
               ans = max(sum, ans);
        }
    }
    cout<<ans;
    return 0;
}

Please Help!

asked 23 Feb '17, 20:18

aniketsanadhya's gravatar image

1★aniketsanadhya
115210
accept rate: 0%


Your loop and your hourglass indices are incorrect. Also no need of inequality check as we only need to find maximum sum hourglass. Its a one liner problem.

My solution for reference.

int main(){

short a[6][6];

register int i,j;

int maxsum=-63,sum=0;

for(i=0;i<6;i++) {

for(j=0;j<6;j++) {

cin>>a[i][j]; } } for(i=0;i<4;i++) {

for(j=0;j<4;j++) {

sum=a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2];

maxsum=max(maxsum,sum); } }

cout <<maxsum;

return 0; }

link

answered 23 Feb '17, 20:40

inovation123's gravatar image

4★inovation123
4537
accept rate: 10%

I have edited your code and now it's passing 7 cases out of 7, take a look.. If you initialize ans = 0 your code fails on 3rd and 7th test case. If you take ans = -6 your code passes all test cases but fails on test case no. 7.For ans =-18 you can pass all the test cases.

#include <iostream>

using namespace std;

int main()
{
   int a[6][6];
   for(int i=0;i<6;i++)
      for(int j=0;j<6;j++)
          cin>>a[i][j];
  int sum,ans=-18;
  for(int i=0;i<4;i++)
  {
     for(int j=0;j<4;j++)
     {
            sum = a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2];
            ans = max(sum, ans);
     }
 }
   cout<<ans;
   return 0;
 }
link

answered 23 Feb '17, 20:59

only4's gravatar image

4★only4
1.5k212
accept rate: 17%

edited 23 Feb '17, 21:19

It was a simple problem (saying cause I solved it in a single try). Since the answer is given, I will just give a few tips-

  1. Such problems are simple, but we MUST make sure to thoroughly test the corner test cases. Eg- Your hourglass may work in middle, but is it working correctly for Extreme rows? Extreme columns? (Meaning Row 1, Final Row etc.) . It is a good habit to check your code at corner elements while dealing with such a problem (i.e. 4 corner elements of 2D matrix)
  2. The loop control variable MUST be thought of carefully. Make sure it is not going out of bounds! (Sometimes system doesn't give out of bound exception, but gives a garbage value during run time. This causes a LOT of confusion as such where the code went wrong)
  3. Make sure you test your code thoroughly. Make sure to brainstorm over corner cases. Because these practice problems are meant to sharpen your skills, algo AND test-case thinking ability. That's why hackerrank doesn't give test cases for free. It shows them to you for 5 hackos, which you must earn by correctly solving questions.
  4. If possible, include comments or tell about the approach you're using while asking. Not necessary, but makes our job easier to see. For example, I didn't get why you did the inequality check and had to spend some time thinking what your possible approach was. AFAIK you tried to sum the elements only if any one of those was different. What I guess is you wanted to save time. But what my basic instincts tell me (might be wrong) is that it then needs to do 6-7 checks in if else to save 1 operation of calculating sum. I mean, it actually has no significant difference on time. Yes, if that if else had some lengthy loop in it, then your approach would surely save time, but I don't think inequality check is needed here.
link

answered 23 Feb '17, 21:19

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066
accept rate: 18%

plz upvote need to ask questions..

link

answered 23 Feb '17, 21:25

montycs's gravatar image

0★montycs
1057
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×310

question asked: 23 Feb '17, 20:18

question was seen: 1,102 times

last updated: 23 Feb '17, 21:25