Hướng dẫn hackerrank 2d array python

We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

  • Prepare

    NEW

  • Certify
  • Compete

  • Hiring developers?

  1. Prepare
  2. Data Structures
  3. Arrays
  4. 2D Array - DS
  5. Discussions

Problem

Submissions

Leaderboard

Discussions

Editorial

    You are viewing a single comment's thread. Return to all comments →

  • Hướng dẫn hackerrank 2d array python

    RishC

    6 years ago+ 75 comments

    Python3 answer

    sum = []
    
    
    for i in range(len(arr)-2):
        for j in range(len(arr)-2):
            sum.append(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])
            
    print(max(sum))
    

Homeinterview prepration kitHackerRank 2D Array - DS problem solution

In this HackerRank 2D Array - DS interview preparation kit problem you have to Calculate the hourglass sum for every hourglass arr, then print the maximum hourglass sum. The array will always be 6 x 6.

Hướng dẫn hackerrank 2d array python

Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the hourglassSum function below.
def hourglassSum(arr):
    maxSum = -63
    
    for i in range(4):
        for j in range(4):
        
            # sum of top 3 elements
            top = sum(arr[i][j:j+3])
            
            # sum of the mid element
            mid = arr[i+1][j+1]
            
            # sum of bottom 3 elements
            bottom = sum(arr[i+2][j:j+3])
            
            hourglass = top + mid + bottom
            
            if hourglass > maxSum:
                maxSum = hourglass
                
    return maxSum

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr = []

    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))

    result = hourglassSum(arr)

    fptr.write(str(result) + '\n')

    fptr.close()


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int[][] array = new int[6][6];
        for (int y = 0; y < 6; y++){
            for (int x =0; x<6; x++){
                array[x][y] = sc.nextInt();
            }
        }
        int maxHourglass = getHourglass(array, 1,1);
        for (int y=1; y<5; y++){
            for (int x=1; x<5; x++){
                int hourres = getHourglass(array, x, y);
                if (hourres > maxHourglass){
                    maxHourglass = hourres;
                }
            }
        }
        System.out.println(maxHourglass);
    }
    
    public static int getHourglass(int[][] array, int x, int y) {
        return array[x][y] + array[x-1][y-1] + array[x][y-1] + array[x+1][y-1] + array[x-1][y+1]
            + array[x][y+1] + array[x+1][y+1];
    }
}

Problem solution in C++ programming.

#include 
#include 
#include 
#include 
#include 
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int arr[6][6];
    int row, col;
    int sum;
    int best_sum;
    int first_flag=1;
    for(row=0; row<6; row++){
        for(col=0; col<6; col++){
            cin >> arr[row][col];
        }
    }
    for(row=0; row<=3; row++){
        for(col=0; col<=3; col++){
            sum = arr[row][col] + arr[row][col+1] + arr[row][col+2] + arr[row+1][col+1] + arr[row+2][col] + arr[row+2][col+1] + arr[row+2][col+2];
            if(first_flag==1){
                first_flag=0;
                best_sum=sum;
            }
            else if(sum>best_sum)
                best_sum=sum;
        }
    }
    cout << best_sum;
    return 0;
}

Problem solution in C programming.

#include 
#include 
#include 
#include 
int a[6][6];
int main() {
int i,j,sum;
    int max=-999999;
    for(i=0;i<6;i++)
        for(j=0;j<6;j++)
        scanf("%d",&a[i][j]);
        for(i=0;i<4;i++)
        for(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>max)
            max=sum;
    }
    printf("%d",max);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Problem solution in JavaScript programming.

function getSum(arr, row, col) {
    var i=0, j=0, sum=0;
        
    for (i=0; i<3; i++) {
        for (j=0; j<3; j++) {
            if ((i===1 && j===0) || (i===1 && j===2)) {
                sum += 0;
            } else {
                sum += arr[row+i][col+j];            
            }
        }
    }

    return sum;
}

function processData(input) {
    var input_arr = input.trim().split('\n');
    var max = -9999;
    
//    console.log(input_arr);
    
    // Make a two dimensional array
    for( var i=0; i