Sort by

recency

|

693 Discussions

|

  • + 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();
    }
    

    }

  • + 0 comments

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

        // 6x6 2D Array input using List of List
        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);
        }
    
        bufferedReader.close();
    
        // Initialize maxSum to minimum possible integer value
        int maxSum = Integer.MIN_VALUE;
    
        // Loop through all possible hourglass starting positions
        for (int i = 0; i < 4; i++) {  // rows
            for (int j = 0; j < 4; j++) {  // columns
    
                // Calculate sum of current hourglass
                int currentSum = 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);
    
                // Update maxSum if needed
                if (currentSum > maxSum) {
                    maxSum = currentSum;
                }
            }
        }
    
        // Output the result
        System.out.println(maxSum);
    }
    

    }

  • + 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);
                }
            });
    
            bufferedReader.close();
            
            int largestHourglassValue = Integer.MIN_VALUE;
            
            for (int i=0; i < 4; i++) {
                for (int j=0; j < 4; j++) {
                    int sum = arr.get(i).subList(j, j+3)
                        .stream().mapToInt(Integer::intValue).sum();
                    sum += arr.get(i+1).get(j+1);
                    sum += arr.get(i+2).subList(j, j+3)
                        .stream().mapToInt(Integer::intValue).sum();
    
                    if (sum > largestHourglassValue) {
                        largestHourglassValue = sum;
                    }
                }            
            }
            
            System.out.print(largestHourglassValue);
        }
    }