Day 11 : 2D Arrays
Solution In Java 8:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a[][] = new int[6][6];
int sum = 0;
for(int i=0; i < 6; i++){
for(int j=0; j < 6; j++){
a[i][j] = in.nextInt();
}
}
int max = -99999;
for(int i = 0 ; i < 4 ; i++){
for(int j= 0 ; j < 4 ; j++){
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 > 0)
{
if(sum > max){
max =sum;
}
}
else{
if(max < sum ){
max = sum;
}
}
}
}
System.out.println(max);
}
}
Solution In Python 3:
import sys arr = [] maxi = -9999 for arr_i in xrange(6): arr_temp = map(int,raw_input().strip().split(' ')) arr.append(arr_temp) for i in range(4): for j in range(4): addition = 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] if(addition > 0): if(addition > maxi): maxi = addition else: if(maxi < addition): maxi = addition print(maxi)
0 comments:
Post a Comment