Maximum product of 4 adjacent elements in matrix | Java implementation of the above approach ; Window Product for each row . ; Maximum window product for each row ; Global maximum window product ; Driver Code ; Function call
import java . io . * ; class GFG { public static int maxPro ( int [ ] [ ] a , int n , int m , int k ) { int maxi = 1 , mp = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int wp = 1 ; for ( int l = 0 ; l < k ; ++ l ) { wp *= a [ i ] [ l ] ; } mp = wp ; for ( int j = k ; j < m ; ++ j ) { wp = wp * a [ i ] [ j ] / a [ i ] [ j - k ] ; maxi = Math . max ( maxi , Math . max ( mp , wp ) ) ; } } return maxi ; } public static void main ( String [ ] args ) { int n = 6 , m = 5 , k = 4 ; int [ ] [ ] a = new int [ ] [ ] { { 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 } } ; int maxpro = maxPro ( a , n , m , k ) ; System . out . println ( maxpro ) ; } }