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.
I have done the following with the complexity of O(n).. Hope, it will help one of those who are here just to have some more optimized code..All thanks to the AlMighty, who have given me the power to do this ,#Allhamdulillah.
public static void SumHourGlass(int [][] hourGlass)
{
int maximum = 0;
int a = 0, b = 1, c = 2;
int d = 1;
int e = 0, f = 1, g = 2;
int row1 = 0;
int row2 = 1;
int row3 = 2;
for (int i = 1; i <= 16; i++)
{
int sum = hourGlass[row1][a] + hourGlass[row1][b] + hourGlass[row1][c] + hourGlass[row2][d] + hourGlass[row3][e] + hourGlass[row3][f] + hourGlass[row3][g];
if (i == 1)
{
maximum = sum;
}
if (i > 1)
{
if (sum > maximum)
{
maximum = sum;
}
}
if (i % 4 != 0)
{
a++;
b++;
c++;
d++;
e++;
f++;
g++;
}
else
{
a = 0;
b = 1;
c = 2;
d = 1;
e = 0;
f = 1;
g = 2;
row1++;
row2++;
row3++;
}
}
Console.WriteLine(maximum);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 11: 2D Arrays
You are viewing a single comment's thread. Return to all comments →
I have done the following with the complexity of O(n).. Hope, it will help one of those who are here just to have some more optimized code..All thanks to the AlMighty, who have given me the power to do this ,#Allhamdulillah. public static void SumHourGlass(int [][] hourGlass) { int maximum = 0; int a = 0, b = 1, c = 2; int d = 1; int e = 0, f = 1, g = 2; int row1 = 0; int row2 = 1; int row3 = 2; for (int i = 1; i <= 16; i++) { int sum = hourGlass[row1][a] + hourGlass[row1][b] + hourGlass[row1][c] + hourGlass[row2][d] + hourGlass[row3][e] + hourGlass[row3][f] + hourGlass[row3][g];