codeparrot/xlcost-text-to-code huggingface dataset:
Construct a Binary Tree from Postorder and Inorder | C # program to contree using inorder and postorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to conbinary of size n from Inorder traversal in [ ] and Postorder 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 Postorder 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 , con left and right subtress ; This function mainly creates an unordered_map , then calls buildTreeUtil ( ) ; Store indexes of all items so that we we can quickly find later ; Index in postorder ; Helper function that allocates a new node ; This funtcion is here just to test ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node buildUtil ( int [ ] init , int [ ] post , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; int curr = post [ index ] ; Node node = newNode ( curr ) ; ( index ) -- ; if ( inStrt == inEnd ) return node ; int iIndex = mp [ curr ] ; node . right = buildUtil ( init , post , iIndex + 1 , inEnd ) ; node . left = buildUtil ( init , post , inStrt , iIndex - 1 ) ; return node ; } static Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; static int index ; static Node buildTree ( int [ ] init , int [ ] post , int len ) { for ( int i = 0 ; i < len ; i ++ ) mp . Add ( init [ i ] , i ) ; index = len - 1 ; return buildUtil ( init , post , 0 , len - 1 ) ; } 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 ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void Main ( String [ ] args ) { int [ ] init = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int [ ] post = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = init . Length ; Node root = buildTree ( init , post , n ) ; Console . Write ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁ STRNEWLINE " ) ; preOrder ( root ) ; } }
Minimum flip required to make Binary Matrix symmetric | C # Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; finding the transpose of the matrix ; Finding the number of position where element are not same . ; Driver program to test above function
using System ; class GFG { static int minimumflip ( int [ , ] mat , int n ) { int [ , ] transpose = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) transpose [ i , j ] = mat [ j , i ] ; int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( transpose [ i , j ] != mat [ i , j ] ) flip ++ ; return flip / 2 ; } public static void Main ( ) { int n = 3 ; int [ , ] mat = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; Console . WriteLine ( minimumflip ( mat , n ) ) ; } }
Minimum flip required to make Binary Matrix symmetric | C # Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; Comparing elements across diagonal ; Driver program to test above function
using System ; class GFG { static int minimumflip ( int [ , ] mat , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i , j ] != mat [ j , i ] ) flip ++ ; return flip ; } public static void Main ( ) { int n = 3 ; int [ , ] mat = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; Console . WriteLine ( minimumflip ( mat , n ) ) ; } }
Frequencies of even and odd numbers in a matrix | C # Program to Find the frequency of even and odd numbers in a matrix ; function for calculating frequency ; modulo by 2 to check even and odd ; print Frequency of numbers ; Driver code
using System ; class GFG { static int MAX = 100 ; static void freq ( int [ , ] ar , int m , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < m ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { if ( ( ar [ i , j ] % 2 ) == 0 ) ++ even ; else ++ odd ; } } Console . WriteLine ( " ▁ Frequency ▁ of ▁ odd ▁ number ▁ = " + odd ) ; Console . WriteLine ( " ▁ Frequency ▁ of ▁ even ▁ number ▁ = ▁ " + even ) ; } public static void Main ( ) { int m = 3 , n = 3 ; int [ , ] array = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; freq ( array , m , n ) ; } }
Center element of matrix equals sums of half diagonals | C # program to find maximum elements that can be made equal with k updates ; Function to Check center element is equal to the individual sum of all the half diagonals ; Find sums of half diagonals ; Driver code
using System ; class GFG { static bool HalfDiagonalSums ( int [ , ] mat , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i , i ] ; diag2_left += mat [ j , i ] ; } else if ( i > n / 2 ) { diag1_right += mat [ i , i ] ; diag2_right += mat [ j , i ] ; } } return ( diag1_left == diag2_right && diag2_right == diag2_left && diag1_right == diag2_left && diag2_right == mat [ n / 2 , n / 2 ] ) ; } static public void Main ( ) { int [ , ] a = { { 2 , 9 , 1 , 4 , - 2 } , { 6 , 7 , 2 , 11 , 4 } , { 4 , 2 , 9 , 2 , 4 } , { 1 , 9 , 2 , 4 , 4 } , { 0 , 2 , 4 , 2 , 5 } } ; Console . WriteLine ( HalfDiagonalSums ( a , 5 ) ? " Yes " : " No " ) ; } }
Program for Identity Matrix | C # program to print Identity Matrix ; Checking if row is equal to column ; Driver Code
using System ; class GFG { static int identity ( int num ) { int row , col ; for ( row = 0 ; row < num ; row ++ ) { for ( col = 0 ; col < num ; col ++ ) { if ( row == col ) Console . Write ( 1 + " ▁ " ) ; else Console . Write ( 0 + " ▁ " ) ; } Console . WriteLine ( ) ; } return 0 ; } public static void Main ( ) { int size = 5 ; identity ( size ) ; } }
Program for Identity Matrix | C # program to check if a given matrix is identity ; Driver Code
using System ; class GFG { int MAX = 100 ; static bool isIdentity ( int [ , ] mat , int N ) { for ( int row = 0 ; row < N ; row ++ ) { for ( int col = 0 ; col < N ; col ++ ) { if ( row == col && mat [ row , col ] != 1 ) return false ; else if ( row != col && mat [ row , col ] != 0 ) return false ; } } return true ; } public static void Main ( ) { int N = 4 ; int [ , ] mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isIdentity ( mat , N ) ) Console . WriteLine ( " Yes ▁ " ) ; else Console . WriteLine ( " No ▁ " ) ; } }
Ways of filling matrix such that product of all rows and all columns are equal to unity | C # program to find number of ways to fill a matrix under given constraints ; Returns a raised power t under modulo mod ; Counting number of ways of filling the matrix ; Function calculating the answer ; if sum of numbers of rows and columns is odd i . e ( n + m ) % 2 == 1 and k = - 1 , then there are 0 ways of filiing the matrix . ; If there is one row or one column then there is only one way of filling the matrix ; If the above cases are not followed then we find ways to fill the n - 1 rows and m - 1 columns which is 2 ^ ( ( m - 1 ) * ( n - 1 ) ) . ; Driver Code
using System ; class Example { static long mod = 100000007 ; static long modPower ( long a , long t , long mod ) { long now = a , ret = 1 ; while ( t > 0 ) { if ( t % 2 == 1 ) ret = now * ( ret % mod ) ; now = now * ( now % mod ) ; t >>= 1 ; } return ret ; } static long countWays ( int n , int m , int k ) { if ( n == 1 m == 1 ) return 1 ; else if ( ( n + m ) % 2 == 1 && k == - 1 ) return 0 ; return ( modPower ( modPower ( ( long ) 2 , n - 1 , mod ) , m - 1 , mod ) % mod ) ; } public static void Main ( ) { int n = 2 , m = 7 , k = 1 ; Console . WriteLine ( countWays ( n , m , k ) ) ; } }
Construct a Binary Tree from Postorder and Inorder | C # program for above approach ; Node class ; Constructor ; Tree building function ; Create Stack of type Node * ; Create HashSet of type Node * ; Initialise postIndex with n - 1 ; Initialise root with null ; Initialise node with NULL ; Run do - while loop ; Initialise node with new Node ( post [ p ] ) ; ; Check is root is equal to NULL ; If size of set is greater than 0 ; If st . Peek ( ) is present in the set s ; If the stack is not empty and st . top ( ) . data is equal to in [ i ] ; Pop elements from stack ; If node not equal to NULL ; Return root ; For print preOrder Traversal ; Driver Code ; Function Call ; Function Call for preOrder
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } } static Node buildTree ( int [ ] init , int [ ] post , int n ) { Stack < Node > st = new Stack < Node > ( ) ; HashSet < Node > s = new HashSet < Node > ( ) ; int postIndex = n - 1 ; Node root = null ; for ( int p = n - 1 , i = n - 1 ; p >= 0 ; ) { Node node = null ; do { node = new Node ( post [ p ] ) ; if ( root == null ) { root = node ; } if ( st . Count > 0 ) { if ( s . Contains ( st . Peek ( ) ) ) { s . Remove ( st . Peek ( ) ) ; st . Peek ( ) . left = node ; st . Pop ( ) ; } else { st . Peek ( ) . right = node ; } } st . Push ( node ) ; } while ( post [ p -- ] != init [ i ] && p >= 0 ) ; node = null ; while ( st . Count > 0 && i >= 0 && st . Peek ( ) . data == init [ i ] ) { node = st . Peek ( ) ; st . Pop ( ) ; i -- ; } if ( node != null ) { s . Add ( node ) ; st . Push ( node ) ; } } return root ; } static void preOrder ( Node node ) { if ( node == null ) return ; Console . Write ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void Main ( String [ ] args ) { int [ ] init = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int [ ] post = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = init . Length ; Node root = buildTree ( init , post , n ) ; Console . Write ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁ STRNEWLINE " ) ; preOrder ( root ) ; } }
Mirror of matrix across diagonal | Simple C # program to find mirror of matrix across diagonal . ; for diagonal which start from at first row of matrix ; traverse all top right diagonal ; here we use stack for reversing the element of diagonal ; push all element back to matrix in reverse order ; do the same process for all the diagonal which start from last column ; here we use stack for reversing the elements of diagonal ; push all element back to matrix in reverse order ; Utility function to print a matrix ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100 ; static void imageSwap ( int [ , ] mat , int n ) { int row = 0 ; for ( int j = 0 ; j < n ; j ++ ) { Stack < int > s = new Stack < int > ( ) ; int i = row , k = j ; while ( i < n && k >= 0 ) { s . Push ( mat [ i ++ , k -- ] ) ; } i = row ; k = j ; while ( i < n && k >= 0 ) { mat [ i ++ , k -- ] = s . Peek ( ) ; s . Pop ( ) ; } } int column = n - 1 ; for ( int j = 1 ; j < n ; j ++ ) { Stack < int > s = new Stack < int > ( ) ; int i = j , k = column ; while ( i < n && k >= 0 ) { s . Push ( mat [ i ++ , k -- ] ) ; } i = j ; k = column ; while ( i < n && k >= 0 ) { mat [ i ++ , k -- ] = s . Peek ( ) ; s . Pop ( ) ; } } } static void printMatrix ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } } 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 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }
Mirror of matrix across diagonal | Efficient C # program to find mirror of matrix across diagonal . ; traverse a matrix and swap mat [ i ] [ j ] with mat [ j ] [ i ] ; Utility function to print a matrix ; driver program to test above function
using System ; class GFG { static int MAX = 100 ; static void imageSwap ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j <= i ; j ++ ) mat [ i , j ] = mat [ i , j ] + mat [ j , i ] - ( mat [ j , i ] = mat [ i , j ] ) ; } static void printMatrix ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }
Search in a row wise and column wise sorted matrix | C # program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; Traverse through the matrix ; If the element is found ; Driver code
using System ; class GFG { static int search ( int [ , ] mat , int n , int x ) { if ( n == 0 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) if ( mat [ i , j ] == x ) { Console . Write ( " Element ▁ found ▁ at ▁ ( " + i + " , ▁ " + j + " ) STRNEWLINE " ) ; return 1 ; } } Console . Write ( " ▁ Element ▁ not ▁ found " ) ; return 0 ; } static public void Main ( ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }
Search in a row wise and column wise sorted matrix | C # Code for Search in a row wise and column wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for top right element ; if mat [ i ] [ j ] < x ; if ( i == n j == - 1 ) ; driver program to test above function
using System ; class GFG { private static void search ( int [ , ] mat , int n , int x ) { int i = 0 , j = n - 1 ; while ( i < n && j >= 0 ) { if ( mat [ i , j ] == x ) { Console . Write ( " n ▁ Found ▁ at ▁ " + i + " , ▁ " + j ) ; return ; } if ( mat [ i , j ] > x ) j -- ; else i ++ ; } Console . Write ( " n ▁ Element ▁ not ▁ found " ) ; return ; } public static void Main ( ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }
Create a matrix with alternating rectangles of O and X | C # code to demonstrate the working . ; Function to print alternating rectangles of 0 and X ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Store given number of rows and columns for later use ; A 2D array to store the output to be printed ; Iniitialize the character to be stoed in a [ ] [ ] ; Fill characters in a [ ] [ ] in spiral form . Every iteration fills one rectangle of either Xs or Os ; Fill the first row from the remaining rows ; Fill the last column from the remaining columns ; Fill the last row from the remaining rows ; Print the first column from the remaining columns ; Flip character for next iteration ; Print the filled matrix ; Driver program to test above functions
using System ; class GFG { static void fill0X ( int m , int n ) { int i , k = 0 , l = 0 ; int r = m , c = n ; char [ , ] a = new char [ m , n ] ; char x = ' X ' ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) a [ k , i ] = x ; k ++ ; for ( i = k ; i < m ; ++ i ) a [ i , n - 1 ] = x ; n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) a [ m - 1 , i ] = x ; m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) a [ i , l ] = x ; l ++ ; } x = ( x == '0' ) ? ' X ' : '0' ; } for ( i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) Console . Write ( a [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { Console . WriteLine ( " Output ▁ for " + " ▁ m ▁ = ▁ 5 , ▁ n ▁ = ▁ 6" ) ; fill0X ( 5 , 6 ) ; Console . WriteLine ( " Output ▁ for " + " ▁ m ▁ = ▁ 4 , ▁ n ▁ = ▁ 4" ) ; fill0X ( 4 , 4 ) ; Console . WriteLine ( " Output ▁ for " + " ▁ m ▁ = ▁ 3 , ▁ n ▁ = ▁ 4" ) ; fill0X ( 3 , 4 ) ; } }
Zigzag ( or diagonal ) traversal of Matrix |  ; we will use a 2D vector to store the diagonals of our array the 2D vector will have ( n + m - 1 ) rows that is equal to the number of diagnols ; Driver code ; Function call
using System ; using System . Collections . Generic ; public class GFG { public static int R = 5 , C = 4 ; public static void diagonalOrder ( int [ , ] arr , int n , int m ) { List < List < int > > ans = new List < List < int > > ( n + m - 1 ) ; for ( int i = 0 ; i < n + m - 1 ; i ++ ) { ans . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { ( ans [ i + j ] ) . Add ( arr [ i , j ] ) ; } } for ( int i = 0 ; i < ans . Count ; i ++ ) { for ( int j = ans [ i ] . Count - 1 ; j >= 0 ; j -- ) { Console . Write ( ans [ i ] [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int n = 5 , m = 4 ; int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } , { 17 , 18 , 19 , 20 } , } ; diagonalOrder ( arr , n , m ) ; } }
Minimum cost to sort a matrix of numbers from 0 to n ^ 2 | C # implementation to find the total energy required to rearrange the numbers ; function to find the total energy required to rearrange the numbers ; nested loops to access the elements of the given matrix ; store quotient ; final destination location ( i_des , j_des ) of the element mat [ i ] [ j ] is being calculated ; energy required for the movement of the element mat [ i ] [ j ] is calculated and then accumulated in the ' tot _ energy ' ; required total energy ; Driver function
using System ; class GFG { public static int calculateEnergy ( int [ , ] mat , int n ) { int i_des , j_des , q ; int tot_energy = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { q = mat [ i , j ] / n ; i_des = q ; j_des = mat [ i , j ] - ( n * q ) ; tot_energy += Math . Abs ( i_des - i ) + Math . Abs ( j_des - j ) ; } } return tot_energy ; } public static void Main ( ) { int [ , ] mat = new int [ , ] { { 4 , 7 , 0 , 3 } , { 8 , 5 , 6 , 1 } , { 9 , 11 , 10 , 2 } , { 15 , 13 , 14 , 12 } } ; int n = 4 ; Console . Write ( " Total ▁ energy ▁ required ▁ = ▁ " + calculateEnergy ( mat , n ) + " ▁ units " ) ; } }
Unique cells in a binary matrix | Efficient C # program to count unique cells in a binary matrix ; Returns true if mat [ i ] [ j ] is unique ; checking in row calculating sumrow will be moving column wise ; checking in column calculating sumcol will be moving row wise ; Driver code
using System ; public class GFG { static readonly int MAX = 100 ; static bool isUnique ( int [ , ] mat , int i , int j , int n , int m ) { int sumrow = 0 ; for ( int k = 0 ; k < m ; k ++ ) { sumrow += mat [ i , k ] ; if ( sumrow > 1 ) return false ; } int sumcol = 0 ; for ( int k = 0 ; k < n ; k ++ ) { sumcol += mat [ k , j ] ; if ( sumcol > 1 ) return false ; } return true ; } static int countUnique ( int [ , ] mat , int n , int m ) { int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i , j ] != 0 && isUnique ( mat , i , j , n , m ) ) uniquecount ++ ; return uniquecount ; } static public void Main ( ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; Console . Write ( countUnique ( mat , 3 , 4 ) ) ; } }
Unique cells in a binary matrix | Efficient C # program to count unique cells in a binary matrix ; Count number of 1 s in each row and in each column ; Using above count arrays , find cells ; Driver code
using System ; class GFG { static int MAX = 100 ; static int countUnique ( int [ , ] mat , int n , int m ) { int [ ] rowsum = new int [ n ] ; int [ ] colsum = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i , j ] != 0 ) { rowsum [ i ] ++ ; colsum [ j ] ++ ; } int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i , j ] != 0 && rowsum [ i ] == 1 && colsum [ j ] == 1 ) uniquecount ++ ; return uniquecount ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; Console . Write ( countUnique ( mat , 3 , 4 ) ) ; } }
Check if a given matrix is sparse or not | C # code to check if a matrix is sparse . ; Count number of zeros in the matrix ; Driver Function
using System ; class GFG { static bool isSparse ( int [ , ] array , int m , int n ) { int counter = 0 ; for ( int i = 0 ; i < m ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) if ( array [ i , j ] == 0 ) ++ counter ; return ( counter > ( ( m * n ) / 2 ) ) ; } public static void Main ( ) { int [ , ] array = { { 1 , 0 , 3 } , { 0 , 0 , 4 } , { 6 , 0 , 0 } } ; int m = 3 , n = 3 ; if ( isSparse ( array , m , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Row | C # program to find common elements in two diagonals . ; Returns count of row wise same elements in two diagonals of mat [ n ] [ n ] ; Driver Code
using System ; class GFG { static int countCommon ( int [ , ] mat , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( mat [ i , i ] == mat [ i , n - i - 1 ] ) res ++ ; return res ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . WriteLine ( countCommon ( mat , 3 ) ) ; } }
Check if sums of i | C # program to check if there are two adjacent set bits . ; Function to check the if sum of a row is same as corresponding column ; Driver code ; number of rows ; number of columns
using System ; class GFG { static bool areSumSame ( int [ , ] a , int n , int m ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 = 0 ; sum2 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { sum1 += a [ i , j ] ; sum2 += a [ j , i ] ; } if ( sum1 == sum2 ) return true ; } return false ; } public static void Main ( ) { int n = 4 ; int m = 4 ; int [ , ] M = { { 1 , 2 , 3 , 4 } , { 9 , 5 , 3 , 1 } , { 0 , 3 , 5 , 6 } , { 0 , 4 , 5 , 6 } } ; if ( areSumSame ( M , n , m ) == true ) Console . Write ( "1 STRNEWLINE " ) ; else Console . Write ( "0 STRNEWLINE " ) ; } }
Creating a tree with Left | C # program to create a tree with left child right sibling representation . ; Creating new Node ; Adds a sibling to a list with starting with n ; Add child Node to a Node ; Check if child is not empty . ; Traverses tree in depth first order ; Driver code
using System ; class GFG { public class NodeTemp { public int data ; public NodeTemp next , child ; public NodeTemp ( int data ) { this . data = data ; next = child = null ; } } public static NodeTemp addSibling ( NodeTemp node , int data ) { if ( node == null ) { return null ; } while ( node . next != null ) { node = node . next ; } return ( node . next = new NodeTemp ( data ) ) ; } public static NodeTemp addChild ( NodeTemp node , int data ) { if ( node == null ) { return null ; } if ( node . child != null ) { return ( addSibling ( node . child , data ) ) ; } else { return ( node . child = new NodeTemp ( data ) ) ; } } public static void traverseTree ( NodeTemp root ) { if ( root == null ) { return ; } while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; if ( root . child != null ) { traverseTree ( root . child ) ; } root = root . next ; } } public static void Main ( string [ ] args ) { NodeTemp root = new NodeTemp ( 10 ) ; NodeTemp n1 = addChild ( root , 2 ) ; NodeTemp n2 = addChild ( root , 3 ) ; NodeTemp n3 = addChild ( root , 4 ) ; NodeTemp n4 = addChild ( n3 , 6 ) ; NodeTemp n5 = addChild ( root , 5 ) ; NodeTemp n6 = addChild ( n5 , 7 ) ; NodeTemp n7 = addChild ( n5 , 8 ) ; NodeTemp n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; } }