Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to find max product of 4 adjacent numbers in the same direction in a 20×20 grid with Node.js

1 Answer

0 votes
function printAlignedGrid(grid) {
    const columnWidths = grid[0].map((_, colIndex) =>
        Math.max(...grid.map(row => String(row[colIndex]).length)
    ));
  
    grid.forEach(row => {
        let rowStr = '';
        row.forEach((cell, colIndex) => {
            const formattedCell = String(cell).padStart(columnWidths[colIndex]);
            rowStr += formattedCell + '  '; 
        });
      
        console.log(rowStr);
    });
}
 
function generateRandomInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}
 
function generateRandomGrid(size) {
    let grid = Array(size).fill(0).map(()=>new Array(size).fill(0));
     
    for (let i = 0; i < size; i++) {
        for (let j = 0; j < size; j++) {
            grid[i][j] = generateRandomInteger(1, 100);
        }
    }
    return grid;
}

function FindMaxProduct(grid, size) {
        let max = 0, product = 0;
        let n1 = 0, n2 = 0, n3 = 0, n4 = 0;
           
        for (let i = 0; i < size; i++) {
            for (let j = 0; j < size - 3; j++) {
                product = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3];
                if (product > max) {
                    n1 = grid[i][j]; n2 = grid[i][j + 1]; n3 = grid[i][j + 2]; n4 = grid[i][j + 3];
                    max = product;
                }
            }
        }
                      
        for (let i = 0; i < size; i++) {
            for (let j = 0; j < size - 3; j++) {
                product = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i];
                if (product > max) {
                    n1 = grid[i][j]; n2 = grid[j + 1][i]; n3 = grid[j + 2][i]; n4 = grid[j + 2][i];
                    max = product;
                }
            }
        }
              
        for (let i = 0; i < size - 3; i++) {
            for (let j = 0; j < size - 3; j++) {
                product = grid[j][i] * grid[j + 1][i + 1] * grid[j + 2][i + 2] * grid[j + 3][i + 3];
                if (product > max) {
                    n1 = grid[j][i]; n2 = grid[j + 1][i + 1]; n3 = grid[j + 2][i + 2]; n4 = grid[j + 3][i + 3];
                    max = product;
                }
            }
        }
              
        for (let i = 0; i < size - 3; i++) {
            for (let j = 3; j < size; j++) {
                product = grid[j][i] * grid[j - 1][i + 1] * grid[j - 2][i + 2] * grid[j - 3][i + 3];
                if (product > max) {
                    n1 = grid[j][i]; n2 = grid[j - 1][i + 1]; n3 = grid[j - 2][i + 2]; n4 = grid[j - 3][i + 3];
                    max = product;
                }
            }
        }
                       
        console.log("\n" + n1 + " * " + n2 + " * " + n3 + " * " + n4 + " = ");
    
        return max;
    }
 

const grid = generateRandomGrid(20);
 
printAlignedGrid(grid);

console.log(FindMaxProduct(grid, 20));

 
 
 
 
 
/*
run:

83  51  14  61  60  31  13  58  54   17  66  57  33  15  51  20  58  98  41   1  
79  79  33  60  96  73  85   6  81   89  82  24   1  50  75  35  32  48  52  96  
48  82  30   2  21   3  25  57  84   38  14  19  13  58  10   5  47  20  81  87  
44  93  56  90  13  68  75  26  33   60   1  55  16  85  73   9  79  31  87  27  
60   3  83  40  72  53  23  34  40   14  48  49  16  10  55  84  29   2  63  21  
97   1   8  44  54  57  33   8  89   37  12  13  43  28  36  45  10   3  29  73  
69  22  88   6  98  20  82   4  93  100  91  18  48  42  70  27  40  66  96  98  
85  80  63  64  45  31  87   6  70   93  30   5  78  72  87  17  61  16  56  51  
18   9  28  98  49  42   1  56  85   98  52  84  72  34  21  55  15  18  56  65  
97   7  69  43  69  83  80   6   2   79  41  84  22  28  74  73  32  83  97  87  
88  69  31  19  76  76  55  93  85   50  82  15   3  19   9  58  43   3  15  12  
60  72  22  78  74  31  42  78  97   86  35   5  92   3  85  68  47  39  46  96  
56   6  58  28  66  70  46  66  70  100  25   3  87  63  54  29  33  69  59  32  
60  54  22  81   8  51  22  63  97   29  29  64  85  74  93   2  70  36  10  50  
72  44  19  23  63  26  80   6  98   74  72  22  48  75  40  74  29  67   9  72  
 7  35  18  15  81  17  58  24  95   97  72  97  21  80  45  40  92  51  87   9  
35  87  60  56  91  18  85  47  25   49  46  17  89  80  27  65  90  90  56  92  
56   9  13  43  77  63  83  18  12   55  56  87  16  56  42  95  54  60  92  25  
54  56  75  54  29  60  59  54  51   89  40   9   7  11  72  25  93  43  54  30  
70  20  10   9  50   3  10  38  98   20  97  62  42   1  66   9  70  30  18  59  

80 * 93 * 97 * 100 = 
72168000 

*/

 



answered Nov 3, 2023 by avibootz
...