Sort by

recency

|

3654 Discussions

|

  • + 0 comments

    Time complexity: O(n^2) Space complexity: O(1)

    Of course, one can assign the coordination of the 2-D array to compute hourglasses value directly.

    int compute_hourglasses(int** arr, int start_row, int start_cloumn){
        int sum = 0;
        for (int i = start_row-1; i<=start_row+1; i++){
            for (int j = start_cloumn-1; j <= start_cloumn+1; j++){
                sum += arr[i][j];
            }
        }
        sum -= (arr[start_row][start_cloumn-1] + arr[start_row][start_cloumn+1]);
        return sum;
    }
    
    int hourglassSum(int arr_rows, int arr_columns, int** arr) {
        int start_row = 1, start_cloumn = 1;
        int end_row = arr_rows-1, end_column = arr_columns-1;
        int max_res = compute_hourglasses(arr, start_row, start_cloumn);
        for (int i = start_row; i<end_row; i++){
            for (int j = start_cloumn; j< end_column; j++){
                int temp = compute_hourglasses(arr, i, j);
                if (temp > max_res) max_res = temp;
            }
        }
        return max_res;
    }
    
  • + 0 comments

    Swift Solution :-

    func hourglassSum(arr: [[Int]]) -> Int {
        // Write your code here
            var maxSum: Int?
            for rowIndex in 1..<5 {
                for elementIndex in 1..<5 {
                    let upperRow = arr[rowIndex-1]
                    let currentRow = arr[rowIndex]
                    let lowerRow = arr[rowIndex+1]
                    let sum = upperRow[elementIndex-1] + upperRow[elementIndex] + upperRow[elementIndex+1] + currentRow[elementIndex] + lowerRow[elementIndex-1] + lowerRow[elementIndex] + lowerRow[elementIndex+1]
                    if maxSum == nil {
                        maxSum = sum
                    } else if let maxSumLocal = maxSum, maxSumLocal < sum {
                        maxSum = sum
                    } else {
                        continue
                    }
                }
            }
            return maxSum ?? 0
    }
    
  • + 0 comments
     public static int hourglassSum(List<List<int>> arr)
        {
            int sum=0;
            int max = 0;
            int f =1;
            for(var i=1; i<5;i++) {
                 for(var j=1; j<5;j++) {
                    sum = arr[i][j] + arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1]
                    +arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1];
                    if( f==1) {
                        max = sum;
                        f = 0;
                    }
                    
                    if(max < sum) max = sum;
                 }
            }
            return max;
    
        }
    
  • + 0 comments

    Javascript Solution

    function hourglassSum(arr) {
        // Write your code here
        let n = arr.length - 2
        let highest = -Infinity
        for(let i = 0; i < n; i++){
            for(let j = 0; j < n; j++){
                let k = arr[i].slice(j, j + 3).reduce((x, y) => x + y, 0)
                let l = arr[i + 1][j + 1]
                let m = arr[i + 2].slice(j, j + 3).reduce((x, y) => x + y, 0)
                let sum = k + l + m
                
                highest = Math.max(sum, highest)
            }
        }
        return highest
    }
    
  • + 0 comments

    Here is my c++ solution you can watch video explanation here : https://www.youtube.com/watch?v=aJv6KLHL3Qk

    int hourglassSum(vector<vector<int>> arr) {
        int result = -63;
        for(int r = 1; r <= 4; r++){
            for(int c = 1; c <= 4; c++){
              int s = arr[r][c] + arr[r-1][c] + arr[r-1][c-1] + arr[r-1][c+1];
              s += arr[r+1][c] + arr[r+1][c-1] + arr[r+1][c+1];
              result = max(result,s);
            }
        }
        
        return result;
    }