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

51,765 answers

573 users

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

1 Answer

0 votes
object GridProductFinder {

  def findGreatestProduct(grid: Array[Array[Int]]): (Int, Int, Int, Int) = {
    val rows = grid.length
    val cols = grid(0).length
    var maxProduct = 0
    var maxnumber1 = 0
    var maxnumber2 = 0
    var maxnumber3 = 0

    for (i <- 0 until rows; j <- 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(s"maxnumber1: $maxnumber1")
    println(s"maxnumber2: $maxnumber2")
    println(s"maxnumber3: $maxnumber3")

    (maxProduct, maxnumber1, maxnumber2, maxnumber3)
  }

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

    val (result, _, _, _) = findGreatestProduct(grid)
    
    println(s"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
...