Java Day 11: 2D Arrays




Given a 6x6 2D Array, A:

Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.



  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8. public class Solution {
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. int arr[][] = new int[6][6];
  12. for (int i = 0; i < 6; i++) {
  13. for (int j = 0; j < 6; j++) {
  14. arr[i][j] = in.nextInt();
  15. }
  16. }
  17. int max = -9 * 7;
  18. for (int i = 0; i < 6; i++) {
  19. for (int j = 0; j < 6; j++) {
  20. if (j + 2 < 6 && i + 2 < 6) {
  21. int 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];
  22. if (sum > max) max = sum;
  23. }
  24. }
  25. }
  26. System.out.println(max);
  27. }
  28. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution


download android app

Popular HackerRank Java Solutions








codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube