How to find the greatest product of 3 adjacent numbers in the same (any) direction in a grid with C++

1 Answer

0 votes
#include <iostream>
#include <vector>

int findGreatestProduct(const std::vector<std::vector<int>>& grid) {
    int maxProduct = 0;
    int rows = grid.size();
    int cols = grid[0].size();
    int maxnumber1, maxnumber2, maxnumber3;

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            // Horizontal (right)
            if (j + 2 < cols) {
                int product = grid[i][j] * grid[i][j + 1] * grid[i][j + 2];
                if (product > maxProduct) {
                    maxnumber1 = grid[i][j];
                    maxnumber2 = grid[i][j + 1];
                    maxnumber3 = grid[i][j + 2];
                }
                maxProduct = std::max(maxProduct, product);
            }

            // Vertical (down)
            if (i + 2 < rows) {
                int product = grid[i][j] * grid[i + 1][j] * grid[i + 2][j];
                if (product > maxProduct) {
                    maxnumber1 = grid[i][j];
                    maxnumber2 = grid[i + 1][j];
                    maxnumber3 = grid[i + 2][j];
                }
                maxProduct = std::max(maxProduct, product);
               
            }

            // Diagonal (down-right)
            if (i + 2 < rows && j + 2 < cols) {
                int product = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2];
                if (product > maxProduct) {
                    maxnumber1 = grid[i][j];
                    maxnumber2 = grid[i + 1][j + 1];
                    maxnumber3 = grid[i + 2][j + 2];
                }
                maxProduct = std::max(maxProduct, product);
                
            }

            // Diagonal (down-left)
            if (i + 2 < rows && j - 2 >= 0) {
                int product = grid[i][j] * grid[i + 1][j - 1] * grid[i + 2][j - 2];
                if (product > maxProduct) {
                    maxnumber1 = grid[i][j];
                    maxnumber2 = grid[i + 1][j - 1];
                    maxnumber3 = grid[i + 2][j - 2];
                }
                maxProduct = std::max(maxProduct, product);

            }
        }
    }
    
    std::cout << "maxnumber1: " << maxnumber1 << std::endl;
    std::cout << "maxnumber2: " << maxnumber2 << std::endl;
    std::cout << "maxnumber3: " << maxnumber3 << std::endl;

    return maxProduct;
}

int main() {
    std::vector<std::vector<int>> grid = {
        { 1,  2,  3,  4,  5,  6,  7},
        { 8,  9, 10, 11, 12, 13, 14},
        {49, 49, 99, 40, 17, 81, 18},
        {44, 20, 45, 35, 14,  0, 61},
        {26, 97, 17, 78, 80, 96, 83},
        {16,  7, 97, 57, 32, 16, 27},
        {60, 74, 31, 49, 71, 48, 86}
    };

    int result = findGreatestProduct(grid);
    
    std::cout << "Greatest product of 3 adjacent numbers: " << result << std::endl;
}



/*
run:

maxnumber1: 80
maxnumber2: 96
maxnumber3: 83
Greatest product of 3 adjacent numbers: 637440

*/

 



answered Jul 26, 2025 by avibootz
...