Sort by

recency

|

1922 Discussions

|

  • + 0 comments

    in java

    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 { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<List<Integer>> arr = new ArrayList<>();
    
        for (int i = 0; i < 6; i++) {
            String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            List<Integer> arrRowItems = new ArrayList<>();
    
            for (int j = 0; j < 6; j++) {
                int arrItem = Integer.parseInt(arrRowTempItems[j]);
                arrRowItems.add(arrItem);
            }
    
            arr.add(arrRowItems);
        }
        int m, max = Integer.MIN_VALUE;
        for (int i=0;i<4;i++)
        {
          for (int j=0;j<4;j++)
          {
            m=arr.get(i).get(j)+arr.get(i).get(j+1)+arr.get(i).get(j+2)+arr.get(i+1).get(j+1)+arr.get(i+2).get(j)+arr.get(i+2).get(j+1)+arr.get(i+2).get(j+2) ;
            max=Math.max(m,max);
            }
        }
        System.out.println(max);
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    Here's my javascript solution:-

    function main() {
    
        let arr = Array(6);
    
        for (let i = 0; i < 6; i++) {
            arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        }
        
        console.log(maxHourglassSum(arr));
    }
    
    function maxHourglassSum(arr) {
        const rows = arr.length;
        const cols = arr[0].length;
    
        // Handle cases where the array is smaller than 3x3
        if (rows < 3 || cols < 3) return null;
    
        let maxSum = -Infinity; // Initialize to the smallest possible value
    
        // Loop through the top-left corners of the possible hourglasses
        for (let i = 0; i <= rows - 3; i++) {
            for (let j = 0; j <= cols - 3; j++) {
                // Calculate the hourglass sum
                let sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] // Top row
                        + arr[i + 1][j + 1]                         // Middle element
                        + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]; // Bottom row
    
                // Update maxSum if the current hourglass sum is greater
                if (sum > maxSum) {
                    maxSum = sum;
                }
            }
        }
    
        return maxSum;
    }
    
  • + 0 comments

    In C++

    int main() {

    vector<vector<int>> arr(6);
    
    int maxHourGlassSum = -63;
    
    for (int i = 0; i < 6; i++) {
        arr[i].resize(6);
    
        string arr_row_temp_temp;
        getline(cin, arr_row_temp_temp);
    
        vector<string> arr_row_temp = split(rtrim(arr_row_temp_temp));
    
        for (int j = 0; j < 6; j++) {
            int arr_row_item = stoi(arr_row_temp[j]);
    
            arr[i][j] = arr_row_item;
        }
    }
    
    for(int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
    
            int hourGlassSum = 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];
    
            maxHourGlassSum = max(maxHourGlassSum, hourGlassSum);
        }
    }
    
    cout << maxHourGlassSum;
    
    return 0;
    

    }

  • + 0 comments

    the exercise is very confuse, the explanation too

  • + 0 comments

    python

    if __name__ == '__main__':
    
        arr = []
    
        for _ in range(6):
            arr.append(list(map(int, input().rstrip().split())))
            
        row= len(arr)
        p=row-2
        n=[]
        
        while len(n)<=(p*p):
            for i in range(p):
                for j in range(p):
                    
                    summ=(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])
                    
                    n.append(summ)
        
        print(max(n))