HackerRank Java Arrays- 2D Array - DS



https://www.hackerrank.com/challenges/2d-array/problem?isFullScreen=true



  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 scan = new Scanner(System.in);
  11. int arr[][] = new int[6][6];
  12. for (int row = 0; row < 6; row++) {
  13. for (int col = 0; col < 6; col++) {
  14. arr[row][col] = scan.nextInt();
  15. }
  16. }
  17. scan.close();
  18. System.out.println(maxHourglass(arr));
  19. }
  20. public static int maxHourglass(int [][] arr) {
  21. int max = Integer.MIN_VALUE;
  22. for (int row = 0; row < 4; row++) {
  23. for (int col = 0; col < 4; col++) {
  24. int sum = findSum(arr, row, col);
  25. max = Math.max(max, sum);
  26. }
  27. }
  28. return max;
  29. }
  30. private static int findSum(int [][] arr, int r, int c) {
  31. int sum = arr[r+0][c+0] + arr[r+0][c+1] + arr[r+0][c+2]
  32. + arr[r+1][c+1] +
  33. arr[r+2][c+0] + arr[r+2][c+1] + arr[r+2][c+2];
  34. return sum;
  35. }
  36. }

download android app