codeparrot/xlcost-text-to-code huggingface dataset:
Rotate the matrix right by K times | C # program to rotate a matrix right by k times ; size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver code ; rotate matrix by k ; display rotated matrix
using System ; class GFG { static int M = 3 ; static int N = 3 ; static void rotateMatrix ( int [ , ] matrix , int k ) { int [ ] temp = new int [ M ] ; k = k % M ; for ( int i = 0 ; i < N ; i ++ ) { for ( int t = 0 ; t < M - k ; t ++ ) temp [ t ] = matrix [ i , t ] ; for ( int j = M - k ; j < M ; j ++ ) matrix [ i , j - M + k ] = matrix [ i , j ] ; for ( int j = k ; j < M ; j ++ ) matrix [ i , j ] = temp [ j - k ] ; } } static void displayMatrix ( int [ , ] matrix ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) Console . Write ( matrix [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ , ] matrix = { { 12 , 23 , 34 } , { 45 , 56 , 67 } , { 78 , 89 , 91 } } ; int k = 2 ; rotateMatrix ( matrix , k ) ; displayMatrix ( matrix ) ; } }
Program to check Involutory Matrix | C # Program to implement involutory matrix . ; Function for matrix multiplication . ; Function to check involutory matrix . ; multiply function call . ; Driver function . ; Function call . If function return true then if part will execute otherwise else part will execute .
using System ; class GFG { static int N = 3 ; static void multiply ( int [ , ] mat , int [ , ] res ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { res [ i , j ] = 0 ; for ( int k = 0 ; k < N ; k ++ ) res [ i , j ] += mat [ i , k ] * mat [ k , j ] ; } } } static bool InvolutoryMatrix ( int [ , ] mat ) { int [ , ] res = new int [ N , N ] ; multiply ( mat , res ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j && res [ i , j ] != 1 ) return false ; if ( i != j && res [ i , j ] != 0 ) return false ; } } return true ; } public static void Main ( ) { int [ , ] mat = { { 1 , 0 , 0 } , { 0 , - 1 , 0 } , { 0 , 0 , - 1 } } ; if ( InvolutoryMatrix ( mat ) ) Console . WriteLine ( " Involutory ▁ Matrix " ) ; else Console . WriteLine ( " Not ▁ Involutory ▁ Matrix " ) ; } }
Interchange elements of first and last rows in matrix | C # code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; Driver code ; input in the array ; printing the interchanged matrix
using System ; class GFG { public static void interchangeFirstLast ( int [ ] [ ] m ) { int rows = m . Length ; for ( int i = 0 ; i < m [ 0 ] . Length ; i ++ ) { int t = m [ 0 ] [ i ] ; m [ 0 ] [ i ] = m [ rows - 1 ] [ i ] ; m [ rows - 1 ] [ i ] = t ; } } public static void Main ( string [ ] args ) { int [ ] [ ] m = new int [ ] [ ] { new int [ ] { 8 , 9 , 7 , 6 } , new int [ ] { 4 , 7 , 6 , 5 } , new int [ ] { 3 , 2 , 1 , 8 } , new int [ ] { 9 , 9 , 7 , 7 } } ; interchangeFirstLast ( m ) ; for ( int i = 0 ; i < m . Length ; i ++ ) { for ( int j = 0 ; j < m [ 0 ] . Length ; j ++ ) { Console . Write ( m [ i ] [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } }
Construct Binary Tree from given Parent Array representation | C # program to construct a binary tree from parent array ; A binary tree node ; Creates a node with key as ' i ' . If i is root , then it changes root . If parent of i is not created , then it creates parent first ; If this node is already created ; Create a new node and set created [ i ] ; If ' i ' is root , change root pointer and return ; If parent is not created , then create parent first ; Find parent pointer ; If this is first child of parent ; If second child ; Creates tree from parent [ 0. . n - 1 ] and returns root of the created tree ; Create an array created [ ] to keep track of created nodes , initialize all entries as NULL ; For adding new line in a program ; Utility function to do inorder traversal ; Driver Code
using System ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class GFG { public Node root ; public virtual void createNode ( int [ ] parent , int i , Node [ ] created ) { if ( created [ i ] != null ) { return ; } created [ i ] = new Node ( i ) ; if ( parent [ i ] == - 1 ) { root = created [ i ] ; return ; } if ( created [ parent [ i ] ] == null ) { createNode ( parent , parent [ i ] , created ) ; } Node p = created [ parent [ i ] ] ; if ( p . left == null ) { p . left = created [ i ] ; } else { p . right = created [ i ] ; } } public virtual Node createTree ( int [ ] parent , int n ) { Node [ ] created = new Node [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { created [ i ] = null ; } for ( int i = 0 ; i < n ; i ++ ) { createNode ( parent , i , created ) ; } return root ; } public virtual void newLine ( ) { Console . WriteLine ( " " ) ; } public virtual void inorder ( Node node ) { if ( node != null ) { inorder ( node . left ) ; Console . Write ( node . key + " ▁ " ) ; inorder ( node . right ) ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; int [ ] parent = new int [ ] { - 1 , 0 , 0 , 1 , 1 , 3 , 5 } ; int n = parent . Length ; Node node = tree . createTree ( parent , n ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " constructed ▁ tree ▁ " ) ; tree . inorder ( node ) ; tree . newLine ( ) ; } }
Row wise sorting in 2D array | C # code to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver Code
using System ; class GFG { static int sortRowWise ( int [ , ] m ) { for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) { for ( int k = 0 ; k < m . GetLength ( 1 ) - j - 1 ; k ++ ) { if ( m [ i , k ] > m [ i , k + 1 ] ) { int t = m [ i , k ] ; m [ i , k ] = m [ i , k + 1 ] ; m [ i , k + 1 ] = t ; } } } } for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) Console . Write ( m [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }
Program for Markov matrix | C # code to check Markov Matrix ; outer loop to access rows and inner to access columns ; Find sum of current row ; Driver Code ; Matrix to check ; calls the function check ( )
using System ; class GFG { static bool checkMarkov ( double [ , ] m ) { for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) sum = sum + m [ i , j ] ; if ( sum != 1 ) return false ; } return true ; } static void Main ( ) { double [ , ] m = new double [ , ] { { 0 , 0 , 1 } , { 0.5 , 0 , 0.5 } , { 1 , 0 , 0 } } ; if ( checkMarkov ( m ) ) Console . WriteLine ( " ▁ yes ▁ " ) ; else Console . WriteLine ( " ▁ no ▁ " ) ; } }
Program to check diagonal matrix and scalar matrix | Program to check matrix is diagonal matrix or not . ; Function to check matrix is diagonal matrix or not . ; condition to check other elements except main diagonal are zero or not . ; Driver function
using System ; class GFG { static int N = 4 ; static bool isDiagonalMatrix ( int [ , ] mat ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i , j ] != 0 ) ) return false ; return true ; } public static void Main ( ) { int [ , ] mat = { { 4 , 0 , 0 , 0 } , { 0 , 7 , 0 , 0 } , { 0 , 0 , 5 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isDiagonalMatrix ( mat ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Program to check diagonal matrix and scalar matrix | Program to check matrix is scalar matrix or not . ; Function to check matrix is scalar matrix or not . ; Check all elements except main diagonal are zero or not . ; Check all diagonal elements are same or not . ; Driver function ; Function call
using System ; class GFG { static int N = 4 ; static bool isScalarMatrix ( int [ , ] mat ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i , j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i , i ] != mat [ i + 1 , i + 1 ] ) return false ; return true ; } public static void Main ( ) { int [ , ] mat = { { 2 , 0 , 0 , 0 } , { 0 , 2 , 0 , 0 } , { 0 , 0 , 2 , 0 } , { 0 , 0 , 0 , 2 } } ; if ( isScalarMatrix ( mat ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Construct a Binary Tree from Postorder and Inorder | C # program to construct a tree using inorder and postorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Class Index created to implement pass by reference of Index ; Recursive function to construct binary of size n from Inorder traversal in [ ] and Postrder traversal post [ ] . Initial values of inStrt and inEnd should be 0 and n - 1. The function doesn 't do any error checking for cases where inorder and postorder do not form a tree  ; Base case ; Pick current node from Postrder traversal using postIndex and decrement postIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; This function mainly initializes index of root and calls buildUtil ( ) ; Function to find index of value in arr [ start ... end ] . The function assumes that value is postsent in in [ ] ; This funtcion is here just to test ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public class Index { public int index ; } class GFG { public virtual Node buildUtil ( int [ ] @in , int [ ] post , int inStrt , int inEnd , Index pIndex ) { if ( inStrt > inEnd ) { return null ; } Node node = new Node ( post [ pIndex . index ] ) ; ( pIndex . index ) -- ; if ( inStrt == inEnd ) { return node ; } int iIndex = search ( @in , inStrt , inEnd , node . data ) ; node . right = buildUtil ( @in , post , iIndex + 1 , inEnd , pIndex ) ; node . left = buildUtil ( @in , post , inStrt , iIndex - 1 , pIndex ) ; return node ; } public virtual Node buildTree ( int [ ] @in , int [ ] post , int n ) { Index pIndex = new Index ( ) ; pIndex . index = n - 1 ; return buildUtil ( @in , post , 0 , n - 1 , pIndex ) ; } public virtual int search ( int [ ] arr , int strt , int end , int value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) { break ; } } return i ; } public virtual void preOrder ( Node node ) { if ( node == null ) { return ; } Console . Write ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; int [ ] @in = new int [ ] { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int [ ] post = new int [ ] { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = @in . Length ; Node root = tree . buildTree ( @in , post , n ) ; Console . WriteLine ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁ " ) ; tree . preOrder ( root ) ; } }
Check given matrix is magic square or not | C # program to check whether a given matrix is magic matrix or not ; Returns true if mat [ ] [ ] is magic square , else returns false . ; calculate the sum of the prime diagonal ; the secondary diagonal ; For sums of Rows ; check if every row sum is equal to prime diagonal sum ; For sums of Columns ; check if every column sum is equal to prime diagonal sum ; Driver Code
using System ; class GFG { static int N = 3 ; static bool isMagicSquare ( int [ , ] mat ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i , i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i , N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) rowSum += mat [ i , j ] ; if ( rowSum != sum ) return false ; } for ( int i = 0 ; i < N ; i ++ ) { int colSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) colSum += mat [ j , i ] ; if ( sum != colSum ) return false ; } return true ; } public static void Main ( ) { int [ , ] mat = new int [ , ] { { 2 , 7 , 6 } , { 9 , 5 , 1 } , { 4 , 3 , 8 } } ; if ( isMagicSquare ( mat ) ) Console . WriteLine ( " Magic ▁ Square " ) ; else Console . WriteLine ( " Not ▁ a ▁ magic " + " ▁ Square " ) ; } }
Count sub | C # implementation to count sub - matrices having sum divisible by the value ' k ' ; function to count all sub - arrays divisible by k ; create auxiliary hash array to count frequency of remainders ; Traverse original array and compute cumulative sum take remainder of this current cumulative sum and increase count by 1 for this remainder in mod [ ] array ; as the sum can be negative , taking modulo twice ; Initialize result ; Traverse mod [ ] ; If there are more than one prefix subarrays with a particular mod value . ; add the subarrays starting from the arr [ i ] which are divisible by k itself ; function to count all sub - matrices having sum divisible by the value ' k ' ; Variable to store the final output ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' ; Count number of subarrays in temp [ ] having sum divisible by ' k ' and then add it to ' tot _ count ' ; required count of sub - matrices having sum divisible by ' k ' ; Driver code
using System ; class GFG { static int subCount ( int [ ] arr , int n , int k ) { int [ ] mod = new int [ k ] ; int cumSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumSum += arr [ i ] ; mod [ ( ( cumSum % k ) + k ) % k ] ++ ; } int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( mod [ i ] > 1 ) result += ( mod [ i ] * ( mod [ i ] - 1 ) ) / 2 ; result += mod [ 0 ] ; return result ; } static int countSubmatrix ( int [ , ] mat , int n , int k ) { int tot_count = 0 ; int left , right , i ; int [ ] temp = new int [ n ] ; for ( left = 0 ; left < n ; left ++ ) { for ( right = left ; right < n ; right ++ ) { for ( i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i , right ] ; tot_count += subCount ( temp , n , k ) ; } } return tot_count - 3 ; } static void Main ( ) { int [ , ] mat = new int [ , ] { { 5 , - 1 , 6 } , { - 2 , 3 , 8 } , { 7 , 4 , - 9 } } ; int n = 3 , k = 4 ; Console . Write ( " Count = " countSubmatrix ( mat , n , k ) ) ; } }
Minimum operations required to make each row and column of matrix equals | C # Program to Find minimum number of operation required such that sum of elements on each row and column becomes same ; Function to find minimum operation required to make sum of each row and column equals ; Initialize the sumRow [ ] and sumCol [ ] array to 0 ; Calculate sumRow [ ] and sumCol [ ] array ; Find maximum sum value in either row or in column ; Find minimum increment required in either row or column ; Add difference in corresponding cell , sumRow [ ] and sumCol [ ] array ; Update the count variable ; If ith row satisfied , increment ith value for next iteration ; If jth column satisfied , increment jth value for next iteration ; Utility function to print matrix ; Driver program
using System ; class GFG { static int findMinOpeartion ( int [ , ] matrix , int n ) { int [ ] sumRow = new int [ n ] ; int [ ] sumCol = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) { sumRow [ i ] += matrix [ i , j ] ; sumCol [ j ] += matrix [ i , j ] ; } int maxSum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { maxSum = Math . Max ( maxSum , sumRow [ i ] ) ; maxSum = Math . Max ( maxSum , sumCol [ i ] ) ; } int count = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { int diff = Math . Min ( maxSum - sumRow [ i ] , maxSum - sumCol [ j ] ) ; matrix [ i , j ] += diff ; sumRow [ i ] += diff ; sumCol [ j ] += diff ; count += diff ; if ( sumRow [ i ] == maxSum ) ++ i ; if ( sumCol [ j ] == maxSum ) ++ j ; } return count ; } static void printMatrix ( int [ , ] matrix , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) Console . Write ( matrix [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ , ] matrix = { { 1 , 2 } , { 3 , 4 } } ; Console . WriteLine ( findMinOpeartion ( matrix , 2 ) ) ; printMatrix ( matrix , 2 ) ; } }
Count frequency of k in a matrix of size n where matrix ( i , j ) = i + j | C # program to find the frequency of k in matrix in matrix where m ( i , j ) = i + j ; Driver function
using System ; public class GfG { public static int find ( int n , int k ) { if ( n + 1 >= k ) return ( k - 1 ) ; else return ( 2 * n + 1 - k ) ; } public static void Main ( ) { int n = 4 , k = 7 ; int freq = find ( n , k ) ; if ( freq < 0 ) Console . WriteLine ( " ▁ element " + " ▁ not ▁ exist ▁ " ) ; else Console . WriteLine ( " ▁ Frequency " + " ▁ of ▁ " + k + " ▁ is ▁ " + freq ) ; } }
Given 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . . . k ' s print them in zig zag way . | C # program to print given number of 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; function that prints given number of 1 ' s , ▁ ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; two - dimensional array to store numbers . ; for even row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index ; for odd row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index . ; printing the stored elements . ; Driver function
using System ; public class GfG { public static void ZigZag ( int rows , int columns , int [ ] numbers ) { int k = 0 ; int [ , ] arr = new int [ rows , columns ] ; for ( int i = 0 ; i < rows ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < columns && numbers [ k ] > 0 ; j ++ ) { arr [ i , j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } else { for ( int j = columns - 1 ; j >= 0 && numbers [ k ] > 0 ; j -- ) { arr [ i , j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } } for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < columns ; j ++ ) Console . Write ( arr [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int rows = 4 ; int columns = 5 ; int [ ] Numbers = new int [ ] { 3 , 4 , 2 , 2 , 3 , 1 , 5 } ; ZigZag ( rows , columns , Numbers ) ; } }
Number of cells a queen can move with obstacles on the chessborad | C # program to find number of cells a queen can move with obstacles on the chessborad ; Return the number of position a Queen can move . ; d11 , d12 , d21 , d22 are for diagnoal distances . r1 , r2 are for vertical distance . c1 , c2 are for horizontal distance . ; Initialise the distance to end of the board . ; For each obstacle find the Minimum distance . If obstacle is present in any direction , distance will be updated . ; Driver code ; Chessboard size ; number of obstacles ; Queen x position ; Queen y position ; x position of obstacles ; y position of obstacles
using System ; class GFG { static int numberofPosition ( int n , int k , int x , int y , int [ ] obstPosx , int [ ] obstPosy ) { int d11 , d12 , d21 , d22 , r1 , r2 , c1 , c2 ; d11 = Math . Min ( x - 1 , y - 1 ) ; d12 = Math . Min ( n - x , n - y ) ; d21 = Math . Min ( n - x , y - 1 ) ; d22 = Math . Min ( x - 1 , n - y ) ; r1 = y - 1 ; r2 = n - y ; c1 = x - 1 ; c2 = n - x ; for ( int i = 0 ; i < k ; i ++ ) { if ( x > obstPosx [ i ] && y > obstPosy [ i ] && x - obstPosx [ i ] == y - obstPosy [ i ] ) d11 = Math . Min ( d11 , x - obstPosx [ i ] - 1 ) ; if ( obstPosx [ i ] > x && obstPosy [ i ] > y && obstPosx [ i ] - x == obstPosy [ i ] - y ) d12 = Math . Min ( d12 , obstPosx [ i ] - x - 1 ) ; if ( obstPosx [ i ] > x && y > obstPosy [ i ] && obstPosx [ i ] - x == y - obstPosy [ i ] ) d21 = Math . Min ( d21 , obstPosx [ i ] - x - 1 ) ; if ( x > obstPosx [ i ] && obstPosy [ i ] > y && x - obstPosx [ i ] == obstPosy [ i ] - y ) d22 = Math . Min ( d22 , x - obstPosx [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] < y ) r1 = Math . Min ( r1 , y - obstPosy [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] > y ) r2 = Math . Min ( r2 , obstPosy [ i ] - y - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] < x ) c1 = Math . Min ( c1 , x - obstPosx [ i ] - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] > x ) c2 = Math . Min ( c2 , obstPosx [ i ] - x - 1 ) ; } return d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2 ; } public static void Main ( ) { int n = 8 ; int k = 1 ; int Qposx = 4 ; int Qposy = 4 ; int [ ] obstPosx = { 3 } ; int [ ] obstPosy = { 5 } ; Console . WriteLine ( numberofPosition ( n , k , Qposx , Qposy , obstPosx , obstPosy ) ) ; } }
Maximum product of 4 adjacent elements in matrix | C # program to find out the maximum product in the matrix which four elements are adjacent to each other in one direction ; Function to find max product ; iterate the rows ; iterate the columns ; check the maximum product in horizontal row . ; check the maximum product in vertical row . ; check the maximum product in diagonal going through down - right ; check the maximum product in diagonal going through up - right ; Driver Code
using System ; public class GFG { static int n = 5 ; static int FindMaxProduct ( int [ , ] arr , int n ) { int max = 0 , result ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( j - 3 ) >= 0 ) { result = arr [ i , j ] * arr [ i , j - 1 ] * arr [ i , j - 2 ] * arr [ i , j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 ) { result = arr [ i , j ] * arr [ i - 1 , j ] * arr [ i - 2 , j ] * arr [ i - 3 , j ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 3 ) >= 0 ) { result = arr [ i , j ] * arr [ i - 1 , j - 1 ] * arr [ i - 2 , j - 2 ] * arr [ i - 3 , j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 1 ) <= 0 ) { result = arr [ i , j ] * arr [ i - 1 , j + 1 ] * arr [ i - 2 , j + 2 ] * arr [ i - 3 , j + 3 ] ; if ( max < result ) max = result ; } } } return max ; } static public void Main ( ) { int [ , ] arr = { { 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 } } ; Console . Write ( FindMaxProduct ( arr , n ) ) ; } }
Maximum product of 4 adjacent elements in matrix | C # implementation of the above approach ; Window Product for each row . ; Maximum window product for each row ; Global maximum window product ; Driver Code ; Function call
using System ; 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 ; } static public void Main ( ) { int n = 6 , m = 5 , k = 4 ; int [ , ] 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 } } ; int maxpro = maxPro ( a , n , m , k ) ; Console . WriteLine ( maxpro ) ; } }