Sort by

recency

|

695 Discussions

|

  • + 0 comments

    static void printMaxHourglassSum(List> arr){ //System.out.println("arr " + (arr.get(0)).get(0));

        ArrayList<Integer> arrSum = new ArrayList<>();
        for(int row=0; row<6; row++){
            for(int col=0; col<6; col++){
                //System.out.println("row: " + row + ", col: " + col);
                int skipIdx = 0;
                int temp = 0;
                if(row<=3 && col<=3)
                {
                    for(int i=row; i<row+3; i++){
                        for(int j=col; j<col+3; j++){
                            skipIdx++;
                            //System.out.println("idx: " + idx);
                            // System.out.println("i: " + i 
                            //     + ", j: " + j 
                            //     + "= " + (arr.get(i)).get(j));
    
                            if(skipIdx==4 ||skipIdx==6){
                                continue;
                            }
    
                            temp += (arr.get(i)).get(j);
                        }
                    }
                    //System.out.println("temp: " + temp);
                    arrSum.add(temp);
                }
            }
        }
    
        for(int i=0; i<arrSum.size(); i++){
            //System.out.println("arrSum: " + arrSum.get(i));
        }
    
        Collections.sort(arrSum);
    
        System.out.println(arrSum.get(arrSum.size()-1));
    }
    
  • + 0 comments

    All cases are OK, but the "congrulations" message does not appear.

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<List<Integer>> arr = new ArrayList<>();
    
        for (int i = 0; i < 6; i++) {
            String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            List<Integer> arrRowItems = new ArrayList<>();
    
            for (int j = 0; j < 6; j++) {
                int arrItem = Integer.parseInt(arrRowTempItems[j]);
                arrRowItems.add(arrItem);
            }
    
            arr.add(arrRowItems);
        }
        int maxSum = Integer.MIN_VALUE;
        for (int i = 0; i < 5; i++) {    
    
            for (int j = 0; j < 5; j++){ 
    
                if(i+2 < arr.size() && j+2 < arr.get(i).size()){
    
                    int sum = arr.get(i).get(j) + arr.get(i).get(j+1) 
                            + arr.get(i).get(j+2) + arr.get(i+1).get(j+1)
                            + arr.get(i+2).get(j) + arr.get(i+2).get(j+1) 
                            + arr.get(i+2).get(j+2);
    
                                maxSum = Math.max(maxSum, sum);
                }
            }
        }
        System.out.print(maxSum);
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    import java.util.*;

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] arr = new int[6][6];

        // Reading 6x6 array
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
    
        int maxSum = Integer.MIN_VALUE; // To handle negative numbers too
    
        // Loop through possible hourglass centers
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                int sum = arr[i][j]                // middle
                        + arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1] // top
                        + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1]; // bottom
    
                maxSum = Math.max(maxSum, sum);
            }
        }
    
        System.out.println(maxSum);
    }
    

    }

  • + 0 comments

    Using a matrix template to make this far more readable (and reusable):

    public class Solution {
        private static void prettyPrint(List<List<Integer>> arr, int xOffset, int yOffset, int size) {
            for (int x=0; x<size; x++) {
                for (int y=0; y<size; y++) {
                    System.out.print(arr.get(x + xOffset).get(y + yOffset) + " ");
                }
                System.out.println();
            }
        }
        
        private static int[][] hourglass = { {1, 1, 1},
                                             {0, 1, 0},
                                             {1, 1, 1} };
        
        private static int sumHourglass(List<List<Integer>> arr, int xOffset, int yOffset) {
            int sum = 0;
            for (int x=0; x<3; x++) {
                for (int y=0; y<3; y++) {
                    if (hourglass[x][y]==1) {
                        sum += arr.get(x + xOffset).get(y + yOffset);
                    }
                }
            }
            
            return sum;
        }
        
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
            List<List<Integer>> arr = new ArrayList<>();
    
            final int n = 6;
            IntStream.range(0, n).forEach(i -> {
                try {
                    arr.add(
                        Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                            .map(Integer::parseInt)
                            .collect(toList())
                    );
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
    
            bufferedReader.close();
            
            int max = Integer.MIN_VALUE;
            for (int x=0; x<=n-3; x++) {
                for (int y=0; y<=n-3; y++) {
    //                prettyPrint(arr, x, y, 3);
                    final int sum = sumHourglass(arr, x, y);
    //                System.out.println(sum);
                    if (sum>max) max = sum;
                }
            }
            
            System.out.println(max);
    
        }
    }
    
  • + 0 comments

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<List<Integer>> arr = new ArrayList<>();
    
        IntStream.range(0, 6).forEach(i -> {
            try {
                arr.add(
                    Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                        .map(Integer::parseInt)
                        .collect(toList())
                );
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int sum = arr.get(i).get(j) + arr.get(i).get(j + 1) + arr.get(i).get(j + 2) + arr.get(i + 1).get(j + 1)
                        + arr.get(i + 2).get(j) + arr.get(i + 2).get(j + 1) + arr.get(i + 2).get(j + 2);
                      max=Math.max(max,sum);
    
            }
        }
        System.out.println(max);
        bufferedReader.close();
    }
    

    }