GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — hackerrank-java/src/Java2dArray.java at master · anishLearnsToCode/hackerrank-java

Expand file tree

/

Java2dArray.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
39 lines (34 loc) · 1.24 KB
/

Java2dArray.java

Copy path

File metadata and controls

39 lines (34 loc) · 1.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// https://www.hackerrank.com/challenges/java-2d-array/problem
import java.util.Scanner;
public class Java2dArray {
public static void main(String[] args) {
int[][] matrix = getInput();
System.out.println(maxHourGlassSum(matrix));
}
private static int maxHourGlassSum(int[][] matrix) {
int sum = Integer.MIN_VALUE;
for (int row = 0 ; row < 4 ; row++) {
for (int column = 0 ; column < 4 ; column++) {
sum = Math.max(sum, hourGlassSum(row, column, matrix));
}
}
return sum;
}
private static int hourGlassSum(int row, int column, int[][] matrix) {
int sum = 0;
for (int index = 0 ; index < 3 ; sum += matrix[row][column + index++]);
for (int index = 0 ; index < 3 ; sum += matrix[row + 2][column + index++]);
return sum + matrix[row + 1][column + 1];
}
private static int[][] getInput() {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[6][6];
for (int row = 0 ; row < 6 ; row++) {
for (int column = 0 ; column < 6 ; column++) {
matrix[row][column] = scanner.nextInt();
}
}
scanner.close();
return matrix;
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.