Maximum product of 4 adjacent elements in matrix | Javascript implementation of the above approach ; Window Product for each row . ; Maximum window product for each row ; Global maximum window product ; Driver Code ; Function call
function maxPro ( a , n , m , k ) { let maxi = 1 , mp = 1 ; for ( let i = 0 ; i < n ; ++ i ) { let wp = 1 ; for ( let l = 0 ; l < k ; ++ l ) { wp *= a [ i ] [ l ] ; } mp = wp ; for ( let j = k ; j < m ; ++ j ) { wp = wp * a [ i ] [ j ] / a [ i ] [ j - k ] ; maxi = Math . max ( maxi , Math . max ( mp , wp ) ) ; } } return maxi ; } let n = 6 , m = 5 , k = 4 ; let a = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 , 9 , 1 ] , [ 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 1 , 0 ] , [ 9 , 6 , 4 , 2 , 3 ] , [ 1 , 1 , 2 , 1 , 1 ] ] let maxpro = maxPro ( a , n , m , k ) ; document . write ( maxpro ) ;