Sort by

recency

|

1930 Discussions

|

  • + 0 comments

    Javascript: Make sure to understand the question properly. I misunderstood it the first time.

    var sum_hourglass_array = [];
    var sum;
    
    //k is vertical
    //j is horizontal
    
    for (let k=0; k<6; k++){
        let row = arr[k];
        for(let j=0; j<6;j++){
            if ((j+2) <=5 && (k+2)<=5){
                sum = arr[k][j] + arr[k][j+1] + arr[k][j+2] + arr[k+1][j+1] + arr[k+2][j] + arr[k+2][j+1] + arr[k+2][j+2];
                sum_hourglass_array.push(sum);
            }
        } 
    }
    console.log(Math.max.apply(Math,sum_hourglass_array));
    
  • + 0 comments

    C#

            List<List<int>> arr = new List<List<int>>();
            int max = -63; // minValue for this problem/scope
            
            for (int i = 0; i < 6; i++)
            {
                arr.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList());
            }
            
            for(int row = 0; row < 4; row++)
            {
                for(int col = 0; col < 4; col++)
                {
                    int sum = 0;
                    
                    sum =   arr[row][col] + arr[row][col+1] + arr[row][col+2];
                    sum +=  arr[row+1][col+1];
                    sum +=  arr[row+2][col] + arr[row+2][col+1] + arr[row+2][col+2];
                    
                    if(sum > max) max = sum;
                }
            }
            
            Console.WriteLine(max);
    
  • + 0 comments

    C

    int main() {

    int** arr = malloc(6 * sizeof(int*));
    
    for (int i = 0; i < 6; i++) {
        *(arr + i) = malloc(6 * (sizeof(int)));
    
        char** arr_item_temp = split_string(rtrim(readline()));
    
        for (int j = 0; j < 6; j++) {
            int arr_item = parse_int(*(arr_item_temp + j));
    
            *(*(arr + i) + j) = arr_item;
        }
    }
    
    int countk = 0;
    int countl = 0;
    int sum = 0;
    int sum_max = -63;
    int k = 0;
    int l = 0;
    
    for (int i = 0; i < 4; i++) {
    
        for (int j = 0; j < 4; j++) {
                countk = 0;
                for (k=i; k < i+3; k++) {
                    countl = 0;
                    countk++;
                    for (l=j; l < j+3; l++) {
                        countl++;
                        if(countk==2){
                            if (countl==2) {
                            sum+= *(*(arr + k)+l);
                            }
                        }else {
                            sum+= *(*(arr + k)+l);
                        }         
                    }//end for l
                }//end for k
            if(sum>sum_max){
                sum_max=sum;
            }
            sum=0;    
        }//end for j
    }//end for i 
    
    printf("%i",sum_max);
    
    return 0;
    

    }

  • + 0 comments

    🐍 If you want to solve in your favorite IDE - you can copy """Sample Input""" there and make 2D array from it

    sample_input = """0 -4 -6 0 -7 -6 -1 -2 -6 -8 -3 -1 -8 -4 -2 -8 -8 -6 -3 -1 -2 -5 -7 -4 -3 -5 -3 -6 -6 -6 -3 -6 0 -8 -6 -7"""

    arr = [list(map(int, line.split())) for line in inpuT.split("\n")]

  • + 0 comments

    FOR JAVA

    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 answer = Integer.MIN_VALUE;
         for (int i = 0; i <= 3; i++) { 
            for (int j = 0; j <= 3; j++) { 
                int sum = 0;
    
                    sum += arr.get(i).get(j);     
                    sum += arr.get(i).get(j + 1); 
                    sum += arr.get(i).get(j + 2); 
    
                    sum += arr.get(i + 1).get(j + 1);
    
                    sum += arr.get(i + 2).get(j);     
                    sum += arr.get(i + 2).get(j + 1); 
                    sum += arr.get(i + 2).get(j + 2); 
    
                    answer = Math.max(sum, answer);
            }
        }
        System.out.println(answer);
    
        bufferedReader.close();
    }
    

    }