codeparrot/xlcost-text-to-code huggingface dataset:
Construct the full k | C # program to build full k - ary tree from its preorder traversal and to print the postorder traversal of the tree . ; Structure of a node of an n - ary tree ; Utility function to create a new tree node with k children ; Function to build full k - ary tree ; For null tree ; For adding k children to a node ; Check if ind is in range of array Check if height of the tree is greater than 1 ; Recursively add each child ; Function to find the height of the tree ; Function to print postorder traversal of the tree ; Driver Code
using System ; using System . Collections . Generic ; class GFG { class Node { public int key ; public List < Node > child ; } ; static Node newNode ( int value ) { Node nNode = new Node ( ) ; nNode . key = value ; nNode . child = new List < Node > ( ) ; return nNode ; } static int ind ; static Node BuildKaryTree ( int [ ] A , int n , int k , int h ) { if ( n <= 0 ) return null ; Node nNode = newNode ( A [ ind ] ) ; if ( nNode == null ) { Console . WriteLine ( " Memory ▁ error " ) ; return null ; } for ( int i = 0 ; i < k ; i ++ ) { if ( ind < n - 1 && h > 1 ) { ind ++ ; nNode . child . Add ( BuildKaryTree ( A , n , k , h - 1 ) ) ; } else { nNode . child . Add ( null ) ; } } return nNode ; } static Node BuildKaryTree_1 ( int [ ] A , int n , int k , int iN ) { int height = ( int ) Math . Ceiling ( Math . Log ( ( double ) n * ( k - 1 ) + 1 ) / Math . Log ( ( double ) k ) ) ; ind = iN ; return BuildKaryTree ( A , n , k , height ) ; } static void postord ( Node root , int k ) { if ( root == null ) return ; for ( int i = 0 ; i < k ; i ++ ) postord ( root . child [ i ] , k ) ; Console . Write ( root . key + " ▁ " ) ; } public static void Main ( String [ ] args ) { int ind = 0 ; int k = 3 , n = 10 ; int [ ] preorder = { 1 , 2 , 5 , 6 , 7 , 3 , 8 , 9 , 10 , 4 } ; Node root = BuildKaryTree_1 ( preorder , n , k , ind ) ; Console . WriteLine ( " Postorder ▁ traversal ▁ of ▁ " + " constructed ▁ full ▁ k - ary ▁ tree ▁ is : ▁ " ) ; postord ( root , k ) ; Console . WriteLine ( ) ; } }
Form coils in a matrix | C # program to print 2 coils of a 4 n x 4 n matrix . ; Print coils in a matrix of size 4 n x 4 n ; Number of elements in each coil ; Let us fill elements in coil 1. ; First element of coil1 4 * n * 2 * n + 2 * n ; ; Fill remaining m - 1 elements in coil1 [ ] ; Fill elements of current step from down to up ; Next element from current element ; Fill elements of current step from up to down . ; get coil2 from coil1 ; Print both coils ; Driver code
using System ; class GFG { static void printCoils ( int n ) { int m = 8 * n * n ; int [ ] coil1 = new int [ m ] ; coil1 [ 0 ] = 8 * n * n + 2 * n ; int curr = coil1 [ 0 ] ; int nflg = 1 , step = 2 ; int index = 1 ; while ( index < m ) { for ( int i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = ( curr - 4 * n * nflg ) ; if ( index >= m ) break ; } if ( index >= m ) break ; for ( int i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = curr + nflg ; if ( index >= m ) break ; } nflg = nflg * ( - 1 ) ; step += 2 ; } int [ ] coil2 = new int [ m ] ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) coil2 [ i ] = 16 * n * n + 1 - coil1 [ i ] ; Console . Write ( " Coil ▁ 1 ▁ : ▁ " ) ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) Console . Write ( coil1 [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE Coil ▁ 2 ▁ : ▁ " ) ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) Console . Write ( coil2 [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 1 ; printCoils ( n ) ; } }
Sum of matrix in which each element is absolute difference of its row and column numbers | C # program to find sum of matrix in which each element is absolute difference of its corresponding row and column number row . ; Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Generate matrix ; Compute sum ; Driver Code
using System ; public class GFG { static int findSum ( int n ) { int [ , ] arr = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) arr [ i , j ] = Math . Abs ( i - j ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += arr [ i , j ] ; return sum ; } static public void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( findSum ( n ) ) ; } }
Sum of matrix in which each element is absolute difference of its row and column numbers | C # program to find sum of matrix in which each element is absolute difference of its corresponding row and column number row . ; Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code
using System ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += i * ( n - i ) ; return 2 * sum ; } static public void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( findSum ( n ) ) ; } }
Sum of matrix in which each element is absolute difference of its row and column numbers | C # program to find sum of matrix in which each element is absolute difference of its corresponding row and column number row . ; Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code
using System ; public class GFG { static int findSum ( int n ) { n -- ; int sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; } static public void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( findSum ( n ) ) ; } }
Sum of both diagonals of a spiral odd | C # program to find sum of diagonals of spiral matrix ; function returns sum of diagonals ; as order should be only odd we should pass only odd - integers ; Driver code
using System ; class GFG { static int spiralDiaSum ( int n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; } public static void Main ( String [ ] args ) { int n = 7 ; Console . Write ( spiralDiaSum ( n ) ) ; } }
Find perimeter of shapes formed with 1 s in binary matrix | C # program to find perimeter of area coverede by 1 in 2D matrix consisits of 0 ' s ▁ and ▁ 1' s ; Find the number of covered side for mat [ i ] [ j ] . ; UP ; LEFT ; DOWN ; RIGHT ; Returns sum of perimeter of shapes formed with 1 s ; Traversing the matrix and finding ones to calculate their contribution . ; Driver code
public class GFG { public const int R = 3 ; public const int C = 5 ; public static int numofneighbour ( int [ ] [ ] mat , int i , int j ) { int count = 0 ; if ( i > 0 && mat [ i - 1 ] [ j ] == 1 ) { count ++ ; } if ( j > 0 && mat [ i ] [ j - 1 ] == 1 ) { count ++ ; } if ( i < R - 1 && mat [ i + 1 ] [ j ] == 1 ) { count ++ ; } if ( j < C - 1 && mat [ i ] [ j + 1 ] == 1 ) { count ++ ; } return count ; } public static int findperimeter ( int [ ] [ ] mat ) { int perimeter = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { perimeter += ( 4 - numofneighbour ( mat , i , j ) ) ; } } } return perimeter ; } public static void Main ( string [ ] args ) { int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 0 , 1 , 0 , 0 , 0 } , new int [ ] { 1 , 1 , 1 , 0 , 0 } , new int [ ] { 1 , 0 , 0 , 0 , 0 } } ; Console . WriteLine ( findperimeter ( mat ) ) ; } }
Construct Binary Tree from String with bracket representation | C # program to cona binary tree from the given String ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node ; This funtcion is here just to test ; function to return the index of close parenthesis ; Inbuilt stack ; if open parenthesis , push it ; if close parenthesis ; if stack is empty , this is the required index ; if not found return - 1 ; function to contree from String ; Base case ; new root ; if next char is ' ( ' find the index of its complement ' ) ' ; if index found ; call for left subtree ; call for right subtree ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void preOrder ( Node node ) { if ( node == null ) return ; Console . Write ( " { 0 } ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } static int findIndex ( String str , int si , int ei ) { if ( si > ei ) return - 1 ; Stack < char > s = new Stack < char > ( ) ; for ( int i = si ; i <= ei ; i ++ ) { if ( str [ i ] == ' ( ' ) s . Push ( str [ i ] ) ; else if ( str [ i ] == ' ) ' ) { if ( s . Peek ( ) == ' ( ' ) { s . Pop ( ) ; if ( s . Count == 0 ) return i ; } } } return - 1 ; } static Node treeFromString ( String str , int si , int ei ) { if ( si > ei ) return null ; Node root = newNode ( str [ si ] - '0' ) ; int index = - 1 ; if ( si + 1 <= ei && str [ si + 1 ] == ' ( ' ) index = findIndex ( str , si + 1 , ei ) ; if ( index != - 1 ) { root . left = treeFromString ( str , si + 2 , index - 1 ) ; root . right = treeFromString ( str , index + 2 , ei - 1 ) ; } return root ; } public static void Main ( String [ ] args ) { String str = "4(2(3 ) ( 1 ) ) (6(5 ) ) " ; Node root = treeFromString ( str , 0 , str . Length - 1 ) ; preOrder ( root ) ; } }
Continuous Tree | C # Code to check if the tree is continuos or not ; Node structure ; Function to check if the tree is continuos or not ; If root is Null then tree isn 't Continuos ; BFS Traversal ; Move to left child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Move to right child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Driver Code ; Constructing the Tree ; Function Call
using System ; using System . Collections . Generic ; class GFG { public class node { public int val ; public node left ; public node right ; public node ( ) { this . val = 0 ; this . left = null ; this . right = null ; } public node ( int x ) { this . val = x ; this . left = null ; this . right = null ; } public node ( int x , node left , node right ) { this . val = x ; this . left = left ; this . right = right ; } } ; static bool continuous ( node root ) { if ( root == null ) return false ; int flag = 1 ; Queue < node > Q = new Queue < node > ( ) ; Q . Enqueue ( root ) ; node temp ; while ( Q . Count != 0 ) { temp = Q . Peek ( ) ; Q . Dequeue ( ) ; if ( temp . left != null ) { if ( Math . Abs ( temp . left . val - temp . val ) == 1 ) Q . Enqueue ( temp . left ) ; else { flag = 0 ; break ; } } if ( temp . right != null ) { if ( Math . Abs ( temp . right . val - temp . val ) == 1 ) Q . Enqueue ( temp . right ) ; else { flag = 0 ; break ; } } } if ( flag != 0 ) return true ; else return false ; } public static void Main ( String [ ] args ) { node root = new node ( 3 ) ; root . left = new node ( 2 ) ; root . right = new node ( 4 ) ; root . left . left = new node ( 1 ) ; root . left . right = new node ( 3 ) ; root . right . right = new node ( 5 ) ; if ( continuous ( root ) ) Console . Write ( " True STRNEWLINE " ) ; else Console . Write ( " False STRNEWLINE " ) ; } }
Print matrix in diagonal pattern | C # program to print matrix in diagonal order ; Initialize indexes of element to be printed next ; Direction is initially from down to up ; Traverse the matrix till all elements get traversed ; If isUp = true then traverse from downward to upward ; Set i and j according to direction ; If isUp = 0 then traverse up to down ; Set i and j according to direction ; Revert the isUp to change the direction ; Driver code
using System ; class GFG { static int MAX = 100 ; static void printMatrixDiagonal ( int [ , ] mat , int n ) { int i = 0 , j = 0 ; bool isUp = true ; for ( int k = 0 ; k < n * n ; ) { if ( isUp ) { for ( ; i >= 0 && j < n ; j ++ , i -- ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; k ++ ; } if ( i < 0 && j <= n - 1 ) i = 0 ; if ( j == n ) { i = i + 2 ; j -- ; } } else { for ( ; j >= 0 && i < n ; i ++ , j -- ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; k ++ ; } if ( j < 0 && i <= n - 1 ) j = 0 ; if ( i == n ) { j = j + 2 ; i -- ; } } isUp = ! isUp ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int n = 3 ; printMatrixDiagonal ( mat , n ) ; } }
Print matrix in diagonal pattern | C # program to print matrix in diagonal order ; Driver code ; Initialize matrix ; n - size mode - switch to derive up / down traversal it - iterator count - increases until it reaches n and then decreases ; 2 n will be the number of iterations
using System ; public class MatrixDiag { public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 , mode = 0 , it = 0 , lower = 0 ; for ( int t = 0 ; t < ( 2 * n - 1 ) ; t ++ ) { int t1 = t ; if ( t1 >= n ) { mode ++ ; t1 = n - 1 ; it -- ; lower ++ ; } else { lower = 0 ; it ++ ; } for ( int i = t1 ; i >= lower ; i -- ) { if ( ( t1 + mode ) % 2 == 0 ) { Console . WriteLine ( mat [ i , t1 + lower - i ] ) ; } else { Console . WriteLine ( mat [ t1 + lower - i , i ] ) ; } } } } }
Maximum difference of sum of elements in two rows in a matrix | C # program to find maximum difference of sum of elements of two rows ; Function to find maximum difference of sum of elements of two rows such that second row appears before first row . ; auxiliary array to store sum of all elements of each row ; calculate sum of each row and store it in rowSum array ; calculating maximum difference of two elements such that rowSum [ i ] < rowsum [ j ] ; if current difference is greater than previous then update it ; if new element is less than previous minimum element then update it so that we may get maximum difference in remaining array ; Driver code
using System ; class GFG { static int maxRowDiff ( int [ , ] mat , int m , int n ) { int [ ] rowSum = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += mat [ i , j ] ; rowSum [ i ] = sum ; } int max_diff = rowSum [ 1 ] - rowSum [ 0 ] ; int min_element = rowSum [ 0 ] ; for ( int i = 1 ; i < m ; i ++ ) { if ( rowSum [ i ] - min_element > max_diff ) max_diff = rowSum [ i ] - min_element ; if ( rowSum [ i ] < min_element ) min_element = rowSum [ i ] ; } return max_diff ; } public static void Main ( ) { int m = 5 , n = 4 ; int [ , ] mat = { { - 1 , 2 , 3 , 4 } , { 5 , 3 , - 2 , 1 } , { 6 , 7 , 2 , - 3 } , { 2 , 9 , 1 , 4 } , { 2 , 1 , - 2 , 0 } } ; Console . Write ( maxRowDiff ( mat , m , n ) ) ; } }
Total coverage of all zeros in a binary matrix | C # program to get total coverage of all zeros in a binary matrix ; Returns total coverage of all zeros in mat [ ] [ ] ; looping for all rows of matrix ; 1 is not seen yet ; looping in columns from left to right direction to get left ones ; If one is found from left ; If 0 is found and we have found a 1 before . ; Repeat the above process for right to left direction . ; Traversing across columms for up and down directions . ; 1 is not seen yet ; Driver code to test above methods
using System ; class GFG { static int R = 4 ; static int C = 4 ; static int getTotalCoverageOfMatrix ( int [ , ] mat ) { int res = 0 ; for ( int i = 0 ; i < R ; i ++ ) { bool isOne = false ; for ( int j = 0 ; j < C ; j ++ ) { if ( mat [ i , j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } isOne = false ; for ( int j = C - 1 ; j >= 0 ; j -- ) { if ( mat [ i , j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } } for ( int j = 0 ; j < C ; j ++ ) { bool isOne = false ; for ( int i = 0 ; i < R ; i ++ ) { if ( mat [ i , j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } isOne = false ; for ( int i = R - 1 ; i >= 0 ; i -- ) { if ( mat [ i , j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } } return res ; } static public void Main ( ) { int [ , ] mat = { { 0 , 0 , 0 , 0 } , { 1 , 0 , 0 , 1 } , { 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 } } ; Console . WriteLine ( getTotalCoverageOfMatrix ( mat ) ) ; } }
Count all sorted rows in a matrix | C # program to find number of sorted rows ; static int MAX = 100 ; Function to count all sorted rows in a matrix ; Initialize result ; Start from left side of matrix to count increasing order rows ; Check if there is any pair of element that are not in increasing order . ; If the loop didn 't break (All elements of current row were in increasing order) ; Start from right side of matrix to count increasing order rows ( reference to left these are in decreasing order ) ; Check if there is any pair ofs element that are not in decreasing order . ; Note c > 1 condition is required to make sure that a single column row is not counted twice ( Note that a single column row is sorted both in increasing and decreasing order ) ; Driver code
using System ; class GFG { static int sortedCount ( int [ , ] mat , int r , int c ) { int result = 0 ; for ( int i = 0 ; i < r ; i ++ ) { int j ; for ( j = 0 ; j < c - 1 ; j ++ ) if ( mat [ i , j + 1 ] <= mat [ i , j ] ) break ; if ( j == c - 1 ) result ++ ; } for ( int i = 0 ; i < r ; i ++ ) { int j ; for ( j = c - 1 ; j > 0 ; j -- ) if ( mat [ i , j - 1 ] <= mat [ i , j ] ) break ; if ( c > 1 && j == 0 ) result ++ ; } return result ; } public static void Main ( ) { int m = 4 , n = 5 ; int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 4 , 3 , 1 , 2 , 6 } , { 8 , 7 , 6 , 5 , 4 } , { 5 , 7 , 8 , 9 , 10 } } ; Console . WriteLine ( sortedCount ( mat , m , n ) ) ; } }
Queries in a Matrix | C # implementation of program ; Fills initial values in rows [ ] and cols [ ] ; Fill rows with 1 to m - 1 ; Fill columns with 1 to n - 1 ; Function to perform queries on matrix m -- > number of rows n -- > number of columns ch -- > type of query x -- > number of row for query y -- > number of column for query ; perform queries ; swap row x with y ; swap column x with y ; Print value at ( x , y ) ; Driver code ; row [ ] is array for rows and cols [ ] is array for columns ; Fill initial values in rows [ ] and cols [ ]
using System ; class GFG { static void preprocessMatrix ( int [ ] rows , int [ ] cols , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { rows [ i ] = i ; } for ( int i = 0 ; i < n ; i ++ ) { cols [ i ] = i ; } } static void queryMatrix ( int [ ] rows , int [ ] cols , int m , int n , char ch , int x , int y ) { int tmp ; switch ( ch ) { case ' R ' : swap ( rows , x - 1 , y - 1 ) ; break ; case ' C ' : swap ( cols , x - 1 , y - 1 ) ; break ; case ' P ' : Console . Write ( " value ▁ at ▁ ( { 0 } , ▁ { 1 } ) ▁ = ▁ { 2 } STRNEWLINE " , x , y , rows [ x - 1 ] * n + cols [ y - 1 ] + 1 ) ; break ; } return ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( ) { int m = 1234 , n = 5678 ; int [ ] rows = new int [ m ] ; int [ ] cols = new int [ n ] ; preprocessMatrix ( rows , cols , m , n ) ; queryMatrix ( rows , cols , m , n , ' R ' , 1 , 2 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 1 , 1 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 2 , 1 ) ; queryMatrix ( rows , cols , m , n , ' C ' , 1 , 2 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 1 , 1 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 2 , 1 ) ; } }
Maximum XOR value in matrix | C # program to Find maximum XOR value in matrix either row / column wise ; maximum number of row and column function return the maximum xor value that is either row or column wise ; for row xor and column xor ; traverse matrix ; xor row element ; for each column : j is act as row & i act as column xor column element ; update maximum between r_xor , c_xor ; return maximum xor value ; Driver code
using System ; class GFG { static int maxXOR ( int [ , ] mat , int N ) { int r_xor , c_xor ; int max_xor = 0 ; for ( int i = 0 ; i < N ; i ++ ) { r_xor = 0 ; c_xor = 0 ; for ( int j = 0 ; j < N ; j ++ ) { r_xor = r_xor ^ mat [ i , j ] ; c_xor = c_xor ^ mat [ j , i ] ; } if ( max_xor < Math . Max ( r_xor , c_xor ) ) max_xor = Math . Max ( r_xor , c_xor ) ; } return max_xor ; } public static void Main ( ) { int N = 3 ; int [ , ] mat = { { 1 , 5 , 4 } , { 3 , 7 , 2 } , { 5 , 9 , 10 } } ; Console . Write ( " maximum ▁ XOR ▁ value ▁ : ▁ " + maxXOR ( mat , N ) ) ; } }
Maximum mirrors which can transfer light from bottom to right | C # program to find how many mirror can transfer light from bottom to right ; method returns number of mirror which can transfer light from bottom to right ; To store first obstacles horizontaly ( from right ) and vertically ( from bottom ) ; initialize both array as - 1 , signifying no obstacle ; looping matrix to mark column for obstacles ; mark rightmost column with obstacle ; looping matrix to mark rows for obstacles ; mark leftmost row with obstacle ; Initialize result ; if there is not obstacle on right or below , then mirror can be placed to transfer light ; if i > vertical [ j ] then light can from bottom if j > horizontal [ i ] then light can go to right ; uncomment this code to print actual mirror position also cout << i << " ▁ " << j << endl ; ; Driver code ; B - Blank O - Obstacle
using System ; class GFG { static int maximumMirrorInMatrix ( String [ ] mat , int N ) { int [ ] horizontal = new int [ N ] ; int [ ] vertical = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { horizontal [ i ] = - 1 ; vertical [ i ] = - 1 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == ' B ' ) { continue ; } horizontal [ i ] = j ; break ; } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == ' B ' ) { continue ; } vertical [ j ] = i ; break ; } } int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i > vertical [ j ] && j > horizontal [ i ] ) { res ++ ; } } } return res ; } public static void Main ( String [ ] args ) { int N = 5 ; String [ ] mat = { " BBOBB " , " BBBBO " , " BBBBB " , " BOOBO " , " BBBOB " } ; Console . WriteLine ( maximumMirrorInMatrix ( mat , N ) ) ; } }
Direction at last square block | C # program to tell the Current direction in R x C grid ; Function which tells the Current direction ; Driver code
using System ; class GFG { static void direction ( int R , int C ) { if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) { Console . WriteLine ( " Left " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) { Console . WriteLine ( " Up " ) ; return ; } if ( R == C && R % 2 != 0 && C % 2 != 0 ) { Console . WriteLine ( " Right " ) ; return ; } if ( R == C && R % 2 == 0 && C % 2 == 0 ) { Console . WriteLine ( " Left " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 != 0 && R < C ) { Console . WriteLine ( " Right " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 != 0 && R > C ) { Console . WriteLine ( " Down " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 == 0 && R < C ) { Console . WriteLine ( " Left " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 == 0 && R > C ) { Console . WriteLine ( " Up " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 != 0 && R > C ) { Console . WriteLine ( " Down " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R < C ) { Console . WriteLine ( " Right " ) ; return ; } } static public void Main ( ) { int R = 3 , C = 1 ; direction ( R , C ) ; } }