-1

I got the task where I must find the H shaped region which has the biggest sum of numbers in it. Under 'H' shaped region, tha task meant this, consisting of 7 elements and never changing:

x x
xxx
x x

The matrix's size must be 33 or bigger than that, and I don't have to work with rotated 'H' shape. However, it can move upwards and downwards if the matrix is that big (for example a 46 matrix).

I thought of first counting a "maximum" value, starting from the [0][0] element. However, I can't figure out how to move this region-counting along. Could you help me out, please?

Here's my code so far:

#include<iostream>

int main(){
    
    int n = 3;
    int m = 4;
    
    int mtx[n][m] = {
        1,1,1,3,
        1,1,1,3,
        1,1,1,3
    };
    
    //counting the maximum H value
    int max = 0;
    
    for(int i = 0; i < n; i++){
        max += mtx[i][0];
    }
    
    for(int i = 0; i < n; i++){
        max += mtx[i][2];
    }
    
    max += mtx[1][1];
    
    
    int counter = 0;
    int j = 0;
    int k = 0;
    
    //finding if there is bigger
    while(counter >max){
        
        //questioned area, not sure what to do here
        
        if(counter < max){
            max = counter;
        }
    }
    
    return 0;
}
mscookiee
  • 1
  • 2
  • 1
    [Variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) (like your array `mtx`) [are not part of standard C++](https://stackoverflow.com/questions/67488270/returning-struct-with-multiple-variable-length-arrays-from-function-in-c). You need to make the `n` and `m` variable compile-time constants. – Some programmer dude Jun 22 '21 at 09:36
  • your approach is not quite clear. How does it help to know a minimum when you are acutally looking for a maximum? – 463035818_is_not_an_ai Jun 22 '21 at 09:39
  • @463035818_is_not_a_number ohh sorry, typed it wrong, my bad ^^' I corrected that – mscookiee Jun 22 '21 at 09:43
  • you changed `min` to `max` in the code, but the question still talks about finding a minimum. And `if (counter < max) max = counter;` is still looking for a minimum, irrespective of the variables name – 463035818_is_not_an_ai Jun 22 '21 at 09:44
  • Introduce a function `calc_h(x, y)`, which will return the sum of the h-shaped region with the top-left corner in the `(x, y)`. Run this function in a loop. Pick the maximum value. – Mikhail Jun 22 '21 at 09:44
  • 1
    I recommend writing a function that returns the sum of an arbitrary H shaped region, and building the program around it – n. m. could be an AI Jun 22 '21 at 09:45
  • @Mikhail that's what my idea was, as well, I just don't know how to build up the H shape in the counting and make the entire counting-area step one sideways or downwards ^^' – mscookiee Jun 22 '21 at 09:49
  • Since the maximum height and width of `H` will be 3, then it can only move until `n-3` and `m-3` in vertical and horizontal directions respectively. You can use two for loops and call the code posted above at each point. – srt1104 Jun 22 '21 at 10:02
  • I'm not quite sure whether the approach - to search for a max value first - does provide any added value. Imagine a matrix filled with 99s, with a 100 in the middle (the max. value) which is surrounded by a ring of 1s. Finding the 100 and starting there is a wrong trace... – Scheff's Cat Jun 22 '21 at 10:03

1 Answers1

0

As mentioned in a comment, knowing the single maximum element in the matrix does not help to find the maximum H shape. A concrete counter example:

0 1 0 5 
9 1 1 5
0 1 0 5

Maximum element is 9 but maximum sum H shape is

  1 0 5
  1 1 5
  1 0 5

You would need to add more information to know whether the maximum element is part of the maximum H: Only if max_element + 6*min_element > 7*second_smallest_element you can be sure that max_element is part of the biggest sum H. This condition could be refined, but it cannot be made such that the biggest element is always part of the biggest sum H, because thats not true in general (see counter example above).

As suggested in another comment, you should write a function that given coordinates of the upper left corner calculates the sum of elements in the H shape:

#include <iostream>
#include <array>

int H_sum(const std::array<std::array<int,4>,3>& matrix, int x0,int y0){
    // A E
    // BDF
    // C G
    
    int sum = matrix[x0][y0];   // A
    sum += matrix[x0+1][y0];    // B
    sum += matrix[x0+2][y0];    // C
    sum += matrix[x0+1][y0+1];  // D
    sum += matrix[x0][y0+2];    // E
    sum += matrix[x0+1][y0+2];  // F
    sum += matrix[x0+2][y0+2];  // G
    return sum;
}

int main() {
      std::array<std::array<int,4>,3> mtx{
        1,1,1,3,
        1,1,1,3,
        1,1,1,3
    };
    for (const auto& row : mtx){
        for (const auto& e : row){
            std::cout << e;
        }
        std::cout << "\n";
    }
    std::cout << H_sum(mtx,0,0);
}

This is of course only something to get you started. Next you have to carefully consider what are the maximum indices you can pass to H_sum without going out-of-bounds. Then write a nested loop to scan all positions of the H and remember the maximum value encountered.

Last but not least, what I described so far is a brute force approach. You calculate sum for all possible H shapes, remember the maximum, and are done. Maybe there is a clever trick to avoid adding all elements multiple times (for example in a larger matrix, the right leg of one H is the left leg of a different H). Though before applying such tricks and trying to be clever I strongly suggest to write something that is perhaps slow but correct, easy to read and verify.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185