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 Kotlin

1 Answer

0 votes
fun findGreatestProduct(grid: Array<IntArray>): Quad<Int, Int, Int, Int> {
    val rows = grid.size
    val cols = grid[0].size
    var maxProduct = 0
    var maxnumber1 = 0
    var maxnumber2 = 0
    var maxnumber3 = 0

    for (i in 0 until rows) {
        for (j in 0 until cols) {
            // Horizontal (right)
            if (j + 2 < cols) {
                val 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) {
                val 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) {
                val 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) {
                val 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]
                }
            }
        }
    }

    println("maxnumber1: $maxnumber1")
    println("maxnumber2: $maxnumber2")
    println("maxnumber3: $maxnumber3")

    return Quad(maxProduct, maxnumber1, maxnumber2, maxnumber3)
}

// Custom data class to represent a 4-element tuple
data class Quad<A, B, C, D>(val first: A, val second: B, val third: C, val fourth: D)

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

    val result = findGreatestProduct(grid)
    
    println("Greatest product of 3 adjacent numbers: ${result.first}")
}


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

*/

 



answered Jul 26, 2025 by avibootz
...