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

51,766 answers

573 users

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

1 Answer

0 votes
package main

import (
    "fmt"
)

func findGreatestProduct(grid [][]int) int {
    maxProduct := 0
    maxnumber1, maxnumber2, maxnumber3 := 0, 0, 0
    rows := len(grid)
    cols := len(grid[0])

    for i := 0; i < rows; i++ {
        for j := 0; j < cols; j++ {
            // Horizontal (right)
            if j+2 < cols {
                product := grid[i][j] * grid[i][j+1] * grid[i][j+2]
                if product > maxProduct {
                    maxProduct = product
                    maxnumber1 = grid[i][j]
                    maxnumber2 = grid[i][j+1]
                    maxnumber3 = grid[i][j+2]
                }
            }
            // Vertical (down)
            if i+2 < rows {
                product := grid[i][j] * grid[i+1][j] * grid[i+2][j]
                if product > maxProduct {
                    maxProduct = product
                    maxnumber1 = grid[i][j]
                    maxnumber2 = grid[i+1][j]
                    maxnumber3 = grid[i+2][j]
                }
            }
            // Diagonal (down-right)
            if i+2 < rows && j+2 < cols {
                product := grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2]
                if product > maxProduct {
                    maxProduct = product
                    maxnumber1 = grid[i][j]
                    maxnumber2 = grid[i+1][j+1]
                    maxnumber3 = grid[i+2][j+2]
                }
            }
            // Diagonal (down-left)
            if i+2 < rows && j-2 >= 0 {
                product := grid[i][j] * grid[i+1][j-1] * grid[i+2][j-2]
                if product > maxProduct {
                    maxProduct = product
                    maxnumber1 = grid[i][j]
                    maxnumber2 = grid[i+1][j-1]
                    maxnumber3 = grid[i+2][j-2]
                }
            }
        }
    }

    fmt.Println("maxnumber1:", maxnumber1)
    fmt.Println("maxnumber2:", maxnumber2)
    fmt.Println("maxnumber3:", maxnumber3)
    
    return maxProduct
}

func main() {
    grid := [][]int{
        {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},
    }

    result := findGreatestProduct(grid)
    fmt.Println("Greatest product of 3 adjacent numbers:", result)
}



/*
run:

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

*/

 



answered Jul 26, 2025 by avibootz
...