0

Why i am not getting the correct answer?

I am solving hour glass challenge from hacker and came up with the following code but it is not giving the correct answer even i checked on internet so they are also giving me the same code, but it is giving an incorrect answer. https://code.sololearn.com/craBDgGMW8R5/?ref=app

18th Jul 2020, 7:45 AM
Abhishek Dimri
Abhishek Dimri - avatar
3 ответов
+ 2
Hi! I have found your error. You placed the "if (max < sum) {...}" outside of the second "for" loop, but you need to compare the maximum value for every sum, not only for the last hourglass pattern on each row! Take a look here: for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]; if (max < sum) { max = sum; } } } I run your modified code on HackerRank (without a submission) with their test cases and with my custom input case and it has passed successfully all cases.
19th Jul 2020, 11:06 AM
Vasile Eftodii
Vasile Eftodii - avatar
+ 1
Abhishek Dimri, please post a link to the challenge description and what result do you expect to be correct. At least for an input as: 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 your code returns "max = 35" which is the correct result and it is correctly printed on standard output using "cout << result;".
18th Jul 2020, 10:47 AM
Vasile Eftodii
Vasile Eftodii - avatar