hacker rank java 27th problem(java 2d array)
28. java 2d array
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 {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[][] a = new int[6][6];
int max_sum =Integer.MIN_VALUE;
int sum=0;
for(int i=0;i<6;i++){
for (int j = 0; j < 6; j++) {
a[i][j] = scanner.nextInt();
}
}
for(int i=0;i<6;i++){
for (int j = 0; j < 6; j++) {
if(i<4&&j<4){
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>max_sum){
max_sum= sum;
}
}
}
System.out.println(max_sum);
}
}
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 {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[][] a = new int[6][6];
int max_sum =Integer.MIN_VALUE;
int sum=0;
for(int i=0;i<6;i++){
for (int j = 0; j < 6; j++) {
a[i][j] = scanner.nextInt();
}
}
for(int i=0;i<6;i++){
for (int j = 0; j < 6; j++) {
if(i<4&&j<4){
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>max_sum){
max_sum= sum;
}
}
}
System.out.println(max_sum);
}
}
Comments