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

×

# Hackerrank 2D Array-DS Help

 0 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 using namespace std; int main() { int a; 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<

 1 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;  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 <
 1 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 using namespace std; int main() { int a; 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<
 1 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- 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) 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) 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. 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. answered 23 Feb '17, 21:19 15.5k●1●20●66 accept rate: 18%
 3 plz upvote need to ask questions.. answered 23 Feb '17, 21:25 0★montycs 105●7 accept rate: 0%
 toggle preview community wiki:
Preview

### Follow this question

By Email:

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

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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