Sort by

recency

|

681 Discussions

|

  • + 0 comments

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;

    public class Solution { public static int maxHourglassSum(int[][] arr) { int maxSum = Integer.MIN_VALUE;

        for (int i = 0; i < 4; i++) { // we go from row 0 to 3
            for (int j = 0; j < 4; j++) { // the same with col we go 0 to 3
                int hourglassSum = (
                    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]
                );
    
                maxSum = Math.max(maxSum, hourglassSum);
            }
        }
        return maxSum;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] arr = new int[6][6];
    
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                arr[i][j] = scanner.nextInt();
            }
        }
    
        scanner.close();
        System.out.println(maxHourglassSum(arr));
    }
    

    }

  • + 0 comments
     public static int maxHourGlassValue(List<List<Integer>> arr){
            Integer result = Integer.MIN_VALUE;
            for(int i = 0; i < arr.size(); i++){
                List<Integer> inner_list = arr.get(i);
                for(int j = 0; j < inner_list.size(); j++){
                    try{
                        List<Integer> horizon_1 = arr.get(i-1);
                        List<Integer> horizon_2 = arr.get(i+1);
                        int calculated_value = inner_list.get(j) + horizon_1.get(j) + horizon_1.get(j-1) + horizon_1.get(j+1) + horizon_2.get(j) + horizon_2.get(j-1) + horizon_2.get(j+1);
                        if(calculated_value > result) result=calculated_value;
                    }catch(Exception e){
                        
                    }
                }
            }
            return result;
        }
        
    

    Avoided IndexOutOfBounds with try catch

  • + 0 comments

    My efficient java solution

        private static void printLargestSumOfHourglasses(List<List<Integer>> arr) {
            int result = -63;
            for (int row = 0; row < 4; row++) {
                for (int col = 0; col < 4; col++) {
                    int current = 0;
                    current += arr.get(row).subList(col, col + 3).stream().mapToInt(x -> x).sum();
                    current += arr.get(row + 1).get(col + 1);
                    current += arr.get(row + 2).subList(col, col + 3).stream().mapToInt(x -> x).sum();
                    
                    result = Math.max(result, current);
                }
            }
            System.out.println(result);
        }
    
  • + 1 comment

    int biggestSum = Integer.MIN_VALUE;

         for(int i = 0; i< arr.size()-2 ;i++){
            List<Integer>  firstRow = arr.get(i);
            List<Integer> secondRow = arr.get(i+1);
            List<Integer> ThirdRow = arr.get(i+2);
    
             for(int j =0; j<arr.size()-2; j++){
              int  hourGlassSum = 
              firstRow.get(j) + firstRow.get(j+1) + firstRow.get(j+2)+
              secondRow.get(j+1)+
              ThirdRow.get(j)+ThirdRow.get(j+1)+ThirdRow.get(j+2);
    
                biggestSum= Math.max(hourGlassSum, biggestSum);
             }  
        }
    
              System.out.println(biggestSum);
    
  • + 0 comments

    public class Solution {

    public static void main(String[] args) throws IOException {
             int sum;
        int maxsum = Integer.MIN_VALUE;
        int [][] a = new int[6][6];
        Scanner sc = new Scanner(System.in);
        for(int i=0; i<6; i++){
            for(int j=0; j<6; j++){
                a[i][j]=sc.nextInt();
            }
        }
            for (int i = 0; i < 6; i++) {
                for (int j = 0; j < 6; j++) {
                    if(i > 1 && j > 1) {
                        sum = a[i][j]
                                + a[i][j - 1]
                                + a[i][j - 2]
                                + a[i - 1][j - 1]
                                + a[i - 2][j]
                                + a[i - 2][j - 1]
                                + a[i - 2][j - 2];
                        if (sum > maxsum) {
                            maxsum = sum;
                        }
                    }
                }
            }
    

    //
    System.out.println(maxsum); } }