Minimum product in a grid of adjacent elements

Given an N x M grid. The task is to find the minimum product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the matrix.

Examples:

Input : mat[][] = {1, 2, 3, 4,
                   5, 6, 7, 8,
                   9, 10, 11, 12}  
Output : 700 

2*5*7*10 gives output as 700 which is the smallest 
product possible 

Input :{7, 6, 7, 9
        1, 2, 3, 4
        1, 2, 3, 6,
        5, 6, 7, 1}   

Output: 36  

Approach: Traverse in the matrix apart from first row, last row, first column and last column. Compute the product of the four adjacent numbers which are at mat[i-1][j], mat[i+1][j], mat[i][j+1] and mat[i][j-1]. On each computation, if the product thus formed is less than the previous minimum found, then replace the minimum variable with the computed product.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the minimum product
// of adjacent elements
#include <bits/stdc++.h>
using namespace std;
const int N = 3;
const int M = 4;
  
// Function to return the minimum
// product of adjacent elements
int minimumProduct(int mat[N][M])
{
  
    // initial minimum
    int minimum = INT_MAX;
  
    // Traverse in the matrix
    // except the first, last row
    // first and last coloumn
    for (int i = 1; i < N - 1; i++) {
        for (int j = 1; j < M - 1; j++) {
            // product the adjacent elements
            int p = mat[i - 1][j] * mat[i + 1][j]
                    * mat[i][j + 1] * mat[i][j - 1];
  
            // if the product is less than
            // the previously computed minimum
            if (p < minimum)
                minimum = p;
        }
    }
  
    return minimum;
}
  
// Driver Code
int main()
{
    int mat[][4] = { { 1, 2, 3, 4 },
                     { 4, 5, 6, 7 },
                     { 7, 8, 9, 12 } };
  
    cout << minimumProduct(mat);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find 
// the minimum product
// of adjacent elements
import java.io.*;
  
class GFG
{
static int N = 3;
static int M = 4;
  
// Function to return the 
// minimum product of 
// adjacent elements
static int minimumProduct(int mat[][])
{
  
    // initial minimum
    int minimum = Integer.MAX_VALUE;
  
    // Traverse in the matrix
    // except the first, last row
    // first and last coloumn
    for (int i = 1; i < N - 1; i++) 
    {
        for (int j = 1; j < M - 1; j++) 
        {
            // product the 
            // adjacent elements
            int p = mat[i - 1][j] * 
                    mat[i + 1][j] *
                    mat[i][j + 1] * 
                    mat[i][j - 1];
  
            // if the product is less 
            // than the previously 
            // computed minimum
            if (p < minimum)
                minimum = p;
        }
    }
  
    return minimum;
}
  
// Driver Code
public static void main (String[] args)
{
    int mat[][] = {{1, 2, 3, 4},
                   {4, 5, 6, 7},
                   {7, 8, 9, 12}};
  
    System.out.println(minimumProduct(mat));
}
}
  
// This code is contributed
// by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the minimum 
# product of adjacent elements
import sys
  
N = 3
M = 4
  
# Function to return the minimum
# product of adjacent elements
def minimumProduct(mat):
      
    # initial minimum
    minimum = sys.maxsize
  
    # Traverse in the matrix except 
    # the first, last row first 
    # and last coloumn
    for i in range(1, N - 1, 1):
        for j in range(1, M - 1, 1):
              
            # product the adjacent elements
            p = (mat[i - 1][j] * mat[i + 1][j] *
                 mat[i][j + 1] * mat[i][j - 1])
  
            # if the product is less than
            # the previously computed minimum
            if (p < minimum):
                minimum = p
      
    return minimum
  
# Driver Code
if __name__ == '__main__':
    mat = [[1, 2, 3, 4],    
           [4, 5, 6, 7],
           [7, 8, 9, 12]]
  
    print(minimumProduct(mat))
      
# This code is contributed by
# Shashank_Sharma

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find 
// the minimum product
// of adjacent elements
using System;
  
class GFG
{
static int N = 3;
static int M = 4;
  
// Function to return the 
// minimum product of 
// adjacent elements
static int minimumProduct(int [,]mat)
{
  
    // initial minimum
    int minimum = int.MaxValue;
  
    // Traverse in the matrix
    // except the first, last row
    // first and last coloumn
    for (int i = 1; 
             i < N - 1; i++) 
    {
        for (int j = 1; 
                 j < M - 1; j++) 
        {
            // product the 
            // adjacent elements
            int p = mat[i - 1, j] * 
                    mat[i + 1, j] *
                    mat[i, j + 1] * 
                    mat[i, j - 1];
  
            // if the product is less 
            // than the previously 
            // computed minimum
            if (p < minimum)
                minimum = p;
        }
    }
  
    return minimum;
}
  
// Driver Code
public static void Main ()
{
    int [,]mat = {{1, 2, 3, 4},
                  {4, 5, 6, 7},
                  {7, 8, 9, 12}};
  
    Console.WriteLine(minimumProduct(mat));
}
}
  
// This code is contributed
// by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the minimum 
// product of adjacent elements
$N = 3;
$M = 4;
  
// Function to return the minimum
// product of adjacent elements
function minimumProduct($mat)
{
    global $N;
    global $M;
  
    // initial minimum
    $minimum = PHP_INT_MAX;
  
    // Traverse in the matrix
    // except the first, last row
    // first and last coloumn
    for ($i = 1; $i < $N - 1; $i++)
    {
        for ($j = 1; $j < $M - 1; $j++) 
        {
            // product the adjacent elements
            $p = $mat[$i - 1][$j] * $mat[$i + 1][$j] * 
                 $mat[$i][$j + 1] * $mat[$i][$j - 1];
  
            // if the product is less than the
            // previously computed minimum
            if ($p < $minimum)
                $minimum = $p;
        }
    }
  
    return $minimum;
}
  
// Driver Code
$mat = array(array(1, 2, 3, 4),
             array(4, 5, 6, 7),
             array(7, 8, 9, 12));
  
echo minimumProduct($mat);
  
// This code is contributed by Sach_Code
?>

chevron_right


Output:

384

Time Complexity: O(N*M)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.