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.
int max = -100;
for (int x=0;x<4;x++){
for (int y=0;y<4;y++){
int sum = 0;
sum += arr.get(x).get(y);
sum += arr.get(x).get(y+1);
sum += arr.get(x).get(y+2);
sum += arr.get(x+1).get(y+1);
sum += arr.get(x+2).get(y);
sum += arr.get(x+2).get(y+1);
sum += arr.get(x+2).get(y+2);
if (sum > max) max = sum;
}
}
System.out.println(max);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java 2D Array
You are viewing a single comment's thread. Return to all comments →
My solution is:
int max = -100; for (int x=0;x<4;x++){ for (int y=0;y<4;y++){ int sum = 0; sum += arr.get(x).get(y); sum += arr.get(x).get(y+1); sum += arr.get(x).get(y+2); sum += arr.get(x+1).get(y+1); sum += arr.get(x+2).get(y); sum += arr.get(x+2).get(y+1); sum += arr.get(x+2).get(y+2); if (sum > max) max = sum;
} } System.out.println(max);