Sort by

recency

|

690 Discussions

|

  • + 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);
        }
    }
    
  • + 0 comments
    private static void calculateSum(List<List<Integer>> arr) {
        int max = Integer.MIN_VALUE;
        for(int rowidx=0;rowidx<=3;rowidx++) {
            for(int colidx=0;colidx<=3;colidx++) {
                List<Integer> row1 = arr.get(rowidx);
                List<Integer> row2 = arr.get(rowidx+1);
                List<Integer> row3 = arr.get(rowidx+2);
                int row1sum = row1.get(colidx) + row1.get(colidx+1) + row1.get(colidx+2);
                int row2sum = row2.get(colidx+1);
                int row3sum = row3.get(colidx) + row3.get(colidx+1) + row3.get(colidx+2);
                max = Math.max(row1sum + row2sum + row3sum, max);
            }
        }
        System.out.println(max);
    }
    
  • + 0 comments

    Here is Java 2D Array solution - https://programmingoneonone.com/hackerrank-java-2d-array-problem-solution.html

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