Given an integer square matrix of odd dimensions (3 * 3, 5 * 5). The task is to find the product of the middle row & middle column elements.
Examples:
Input: mat[][] =
{{2, 1, 7},
{3, 7, 2},
{5, 4, 9}}
Output: Product of middle row = 42
Product of middle column = 28
Explanation: Product of Middle row elements (3*7*2)
Product of Middle Column elements (1*7*4)
Input: mat[][] =
{ {1, 3, 5, 6, 7},
{3, 5, 3, 2, 1},
{1, 2, 3, 4, 5},
{7, 9, 2, 1, 6},
{9, 1, 5, 3, 2}}
Output: Product of middle row = 120
Product of middle column = 450
Approach:
As the given matrix is of odd dimensions, so the middle row and column will be always at the n/2th. So, Run a loop from i = 0 to N and produce all the elements of the middle row
i.e. row_prod *= mat[n / 2][i]. Similarly, the product of elements of the middle column will be col_prod *= mat[i][n / 2].
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
const int MAX = 100;
void middleProduct( int mat[][MAX], int n)
{
int row_prod = 1, col_prod = 1;
for ( int i = 0; i < n; i++) {
row_prod *= mat[n / 2][i];
col_prod *= mat[i][n / 2];
}
cout << "Product of middle row = "
<< row_prod << endl;
cout << "Product of middle column = "
<< col_prod;
}
int main()
{
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
middleProduct(mat, 3);
return 0;
}
|
C
#include <stdio.h>
#define MAX 100
void middleProduct( int mat[][MAX], int n)
{
int row_prod = 1, col_prod = 1;
for ( int i = 0; i < n; i++) {
row_prod *= mat[n / 2][i];
col_prod *= mat[i][n / 2];
}
printf ( "Product of middle row = %d\n" ,row_prod);
printf ( "Product of middle column = %d\n" ,col_prod);
}
int main()
{
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
middleProduct(mat, 3);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
static void middleProduct( int mat[][], int n)
{
int row_prod = 1 , col_prod = 1 ;
for ( int i = 0 ; i < n; i++) {
row_prod *= mat[n / 2 ][i];
col_prod *= mat[i][n / 2 ];
}
System.out.print( "Product of middle row = "
+ row_prod);
System.out.print( "Product of middle column = "
+ col_prod);
}
public static void main (String[] args) {
int mat[][] = { { 2 , 1 , 7 },
{ 3 , 7 , 2 },
{ 5 , 4 , 9 } };
middleProduct(mat, 3 );
}
}
|
Python3
MAX = 100
def middleProduct(mat, n):
row_prod = 1
col_prod = 1
for i in range (n) :
row_prod * = mat[n / / 2 ][i]
col_prod * = mat[i][n / / 2 ]
print ( "Product of middle row = " ,
row_prod)
print ( "Product of middle column = " ,
col_prod)
if __name__ = = "__main__" :
mat = [[ 2 , 1 , 7 ],
[ 3 , 7 , 2 ],
[ 5 , 4 , 9 ]]
middleProduct(mat, 3 )
|
C#
using System;
class GFG {
static void middleProduct( int [,]mat, int n)
{
int row_prod = 1, col_prod = 1;
for ( int i = 0; i < n; i++) {
row_prod *= mat[n / 2,i];
col_prod *= mat[i,n / 2];
}
Console.WriteLine( "Product of middle row = "
+ row_prod);
Console.WriteLine( "Product of middle column = "
+ col_prod);
}
public static void Main () {
int [,]mat = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
middleProduct(mat, 3);
}
}
|
PHP
<?php
$MAX = 100;
function middleProduct( $mat , $n )
{
$row_prod = 1; $col_prod = 1;
for ( $i = 0; $i < $n ; $i ++)
{
$row_prod *= $mat [ $n / 2][ $i ];
$col_prod *= $mat [ $i ][ $n / 2];
}
echo "Product of middle row = " .
$row_prod . "\n" ;
echo "Product of middle column = " .
$col_prod ;
}
$mat = array ( array ( 2, 1, 7 ),
array ( 3, 7, 2 ),
array ( 5, 4, 9 ));
middleProduct( $mat , 3);
?>
|
Javascript
<script>
let MAX = 100;
function middleProduct(mat,n)
{
let row_prod = 1, col_prod = 1;
for (let i = 0; i < n; i++) {
row_prod *= mat[(Math.floor(n / 2))][i];
col_prod *= mat[i][(Math.floor(n / 2))];
}
document.write( "Product of middle row = "
+ row_prod+ "<br>" );
document.write( "Product of middle column = "
+ col_prod+ "<br>" );
}
let mat = [[ 2, 1, 7 ],
[ 3, 7, 2 ],
[ 5, 4, 9 ]];
middleProduct(mat, 3);
</script>
|
OutputProduct of middle row = 42
Product of middle column = 28
Time Complexity: O(n)
Auxiliary Space: O(1), as constant space is used