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,857 questions

51,778 answers

573 users

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

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:

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

90 * 99 * 90 * 83 = 
66557700

*/

 



answered Nov 3, 2023 by avibootz
...