codeparrot/xlcost-text-to-code huggingface dataset:
Connect nodes at same level | C # program to connect nodes at same level ; initialize queue to hold nodes at same level ; adding nodes to tehe queue ; initializing prev to null ; i > 0 because when i is 0 prev points the last node of previous level , so we skip it ; pointing last node of the nth level to null ; Driver code ; Constructed binary tree is 10 / \ 8 2 / 3 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
using System ; using System . Collections . Generic ; class Node { public int data ; public Node left , right , nextRight ; public Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { Node root ; void connect ( Node p ) { Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; Node temp = null ; while ( q . Count > 0 ) { int n = q . Count ; for ( int i = 0 ; i < n ; i ++ ) { Node prev = temp ; temp = q . Dequeue ( ) ; if ( i > 0 ) prev . nextRight = temp ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; } temp . nextRight = null ; } } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . connect ( tree . root ) ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " the ▁ tree " + " ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; Console . ReadKey ( ) ; } }
Memoization ( 1D , 2D and 3D ) | A recursive implementation of LCS problem of three strings ; Utility function to get max of 2 integers ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; base case ; if equal , then check for next combination ; recursive call ; return the maximum of the three other possible states in recursion ; Driver code
using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int lcs ( char [ ] X , char [ ] Y , char [ ] Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] && Y [ n - 1 ] == Z [ o - 1 ] ) { return 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; } else { return Math . Max ( lcs ( X , Y , Z , m , n - 1 , o ) , Math . Max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; } } static void Main ( ) { char [ ] X = " geeks " . ToCharArray ( ) ; char [ ] Y = " geeksfor " . ToCharArray ( ) ; char [ ] Z = " geeksforge " . ToCharArray ( ) ; int m = X . Length ; int n = Y . Length ; int o = Z . Length ; Console . WriteLine ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , Z , m , n , o ) ) ; } }
Memoization ( 1D , 2D and 3D ) | A memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Utility function to get max of 2 integers ; Driver Code
using System ; public class GFG { public static int [ , , ] arr = new int [ 100 , 100 , 100 ] ; static int lcs ( String X , String Y , String Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( arr [ m - 1 , n - 1 , o - 1 ] != - 1 ) return arr [ m - 1 , n - 1 , o - 1 ] ; if ( X [ m - 1 ] == Y [ n - 1 ] && Y [ n - 1 ] == Z [ o - 1 ] ) { arr [ m - 1 , n - 1 , o - 1 ] = 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; return arr [ m - 1 , n - 1 , o - 1 ] ; } else { arr [ m - 1 , n - 1 , o - 1 ] = max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; return arr [ m - 1 , n - 1 , o - 1 ] ; } } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static public void Main ( ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int k = 0 ; k < 100 ; k ++ ) { arr [ i , j , k ] = - 1 ; } } } String X = " geeks " ; String Y = " geeksfor " ; String Z = " geeksforgeeks " ; int m = X . Length ; int n = Y . Length ; int o = Z . Length ; Console . WriteLine ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , Z , m , n , o ) ) ; } }
Find the probability of reaching all points after N moves from point N | C # program to calculate the probability of reaching all points after N moves from point N ; Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; when the person moves from ith index in right direction when i moves has been done ; when the person moves from ith index in left direction when i moves has been done ; Calling function to print the array with probabilities ; Function that prints the array ; Driver Code
using System ; class GFG { static void printProbabilities ( int n , double left ) { double right = 1 - left ; double [ , ] arr = new double [ n + 1 , 2 * n + 1 ] ; arr [ 1 , n + 1 ] = right ; arr [ 1 , n - 1 ] = left ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= 2 * n ; j ++ ) { arr [ i , j ] += ( arr [ i - 1 , j - 1 ] * right ) ; } for ( int j = 2 * n - 1 ; j >= 0 ; j -- ) { arr [ i , j ] += ( arr [ i - 1 , j + 1 ] * left ) ; } } printArray ( arr , n ) ; } static void printArray ( double [ , ] arr , int n ) { for ( int i = 0 ; i < GetRow ( arr , 0 ) . GetLength ( 0 ) ; i ++ ) { Console . Write ( " { 0 : F4 } ▁ " , arr [ n , i ] ) ; } } public static double [ ] GetRow ( double [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new double [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int n = 2 ; double left = 0.5 ; printProbabilities ( n , left ) ; } }
Check if it is possible to reach a number by making jumps of two given length | C # implementation of the approach ; Function to perform BFS traversal to find minimum number of step needed to reach x from K ; Calculate GCD of d1 and d2 ; If position is not reachable return - 1 ; Queue for BFS ; Hash Table for marking visited positions ; we need 0 steps to reach K ; Mark starting position as visited ; stp is the number of steps to reach position s ; if position not visited add to queue and mark visited ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int minStepsNeeded ( int k , int d1 , int d2 , int x ) { int gcd = __gcd ( d1 , d2 ) ; if ( ( k - x ) % gcd != 0 ) return - 1 ; Queue < pair > q = new Queue < pair > ( ) ; HashSet < int > visited = new HashSet < int > ( ) ; q . Enqueue ( new pair ( k , 0 ) ) ; visited . Add ( k ) ; while ( q . Count != 0 ) { int s = q . Peek ( ) . first ; int stp = q . Peek ( ) . second ; if ( s == x ) return stp ; q . Dequeue ( ) ; if ( ! visited . Contains ( s + d1 ) ) { q . Enqueue ( new pair ( s + d1 , stp + 1 ) ) ; visited . Add ( s + d1 ) ; } if ( ! visited . Contains ( s + d2 ) ) { q . Enqueue ( new pair ( s + d2 , stp + 1 ) ) ; visited . Add ( s + d2 ) ; } if ( ! visited . Contains ( s - d1 ) ) { q . Enqueue ( new pair ( s - d1 , stp + 1 ) ) ; visited . Add ( s - d1 ) ; } if ( ! visited . Contains ( s - d2 ) ) { q . Enqueue ( new pair ( s - d2 , stp + 1 ) ) ; visited . Add ( s - d2 ) ; } } return int . MinValue ; } public static void Main ( String [ ] args ) { int k = 10 , d1 = 4 , d2 = 6 , x = 8 ; Console . WriteLine ( minStepsNeeded ( k , d1 , d2 , x ) ) ; } }
Tiling with Dominoes | C # program to find no . of ways to fill a 3 xn board with 2 x1 dominoes . ; Driver code
using System ; class GFG { static int countWays ( int n ) { int [ ] A = new int [ n + 1 ] ; int [ ] B = new int [ n + 1 ] ; A [ 0 ] = 1 ; A [ 1 ] = 0 ; B [ 0 ] = 0 ; B [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ; B [ i ] = A [ i - 1 ] + B [ i - 2 ] ; } return A [ n ] ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( countWays ( n ) ) ; } }
Newman | C # program to find nth element of Newman - Conway Sequence ; Recursion to find n - th element ; Driver code
using System ; class GFG { static int sequence ( int n ) { if ( n == 1 n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; } public static void Main ( ) { int n = 10 ; Console . Write ( sequence ( n ) ) ; } }
Connect nodes at same level | C # program to connect nodes at same level using extended pre - order traversal ; A binary tree node ; Sets the nextRight of root and calls connectRecur ( ) for other nodes ; Set the nextRight for root ; Set the next right for rest of the nodes ( other than root ) ; Set next right of all descendents of p . Assumption : p is a compete binary tree ; Base case ; Set the nextRight pointer for p 's left child ; Set the nextRight pointer for p 's right child  p->nextRight will be NULL if p is the right most child  at its level ; Set nextRight for other nodes in pre order fashion ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / 3 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
using System ; public class Node { public int data ; public Node left , right , nextRight ; public Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { public Node root ; public virtual void connect ( Node p ) { p . nextRight = null ; connectRecur ( p ) ; } public virtual void connectRecur ( Node p ) { if ( p == null ) { return ; } if ( p . left != null ) { p . left . nextRight = p . right ; } if ( p . right != null ) { p . right . nextRight = ( p . nextRight != null ) ? p . nextRight . left : null ; } connectRecur ( p . left ) ; connectRecur ( p . right ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . connect ( tree . root ) ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " the ▁ tree " + " ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; } }
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of people reduce to ( x - 2 ) . ; Driver code
using System ; class GFG { static int numberOfWays ( int x ) { if ( x == 0 x == 1 ) return 1 ; else return numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ; } public static void Main ( ) { int x = 3 ; Console . WriteLine ( numberOfWays ( x ) ) ; } }
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code
using System ; class GFG { static int numberOfWays ( int x ) { int [ ] dp = new int [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } public static void Main ( ) { int x = 3 ; Console . WriteLine ( numberOfWays ( x ) ) ; } }
Longest Repeated Subsequence | Refer https : www . geeksforgeeks . org / longest - repeating - subsequence / for complete code . This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match
static int findLongestRepeatingSubSeq ( String str ) { int n = str . Length ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = Math . Max ( dp [ i , j - 1 ] , dp [ i - 1 , j ] ) ; } } return dp [ n , n ] ; }
Number of ways to arrange N items under given constraints | C # program to find number of ways to arrange items under given constraint ; method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; Driver code
using System ; class GFG { static int waysToArrange ( int N , int K , int [ ] k ) { int [ , ] C = new int [ N + 1 , N + 1 ] ; int i , j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = ( C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ) ; } } int [ ] dp = new int [ K + 1 ] ; int count = 0 ; dp [ 0 ] = 1 ; for ( i = 0 ; i < K ; i ++ ) { dp [ i + 1 ] = ( dp [ i ] * C [ count + k [ i ] - 1 , k [ i ] - 1 ] ) ; count += k [ i ] ; } return dp [ K ] ; } static void Main ( ) { int N = 4 ; int [ ] k = new int [ ] { 2 , 2 } ; int K = k . Length ; Console . Write ( waysToArrange ( N , K , k ) ) ; } }
Minimum cells required to reach destination with jumps equal to cell values | C # implementation to count minimum cells required to be covered to reach destination ; function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != INT_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; Driver code
using System ; class GFG { static int minCells ( int [ , ] mat , int m , int n ) { int [ , ] dp = new int [ m , n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = int . MaxValue ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( dp [ i , j ] != int . MaxValue && ( j + mat [ i , j ] ) < n && ( dp [ i , j ] + 1 ) < dp [ i , j + mat [ i , j ] ] ) dp [ i , j + mat [ i , j ] ] = dp [ i , j ] + 1 ; if ( dp [ i , j ] != int . MaxValue && ( i + mat [ i , j ] ) < m && ( dp [ i , j ] + 1 ) < dp [ i + mat [ i , j ] , j ] ) dp [ i + mat [ i , j ] , j ] = dp [ i , j ] + 1 ; } } if ( dp [ m - 1 , n - 1 ] != int . MaxValue ) return dp [ m - 1 , n - 1 ] ; return - 1 ; } public static void Main ( ) { int [ , ] mat = { { 2 , 3 , 2 , 1 , 4 } , { 3 , 2 , 5 , 8 , 2 } , { 1 , 1 , 2 , 2 , 1 } } ; int m = 3 , n = 5 ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ " + " cells ▁ = ▁ " + minCells ( mat , m , n ) ) ; } }
Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays | C # to find largest bitonic sequence ; Utility Binary search ; Reverse array ; Function to find LIS in reverse form ; Add boundary case , when array n is zero Depend on smart pointers ; It will always point to empty location ; New smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential candidate of future subsequence It will replace ceil value in tailIndices ; Put LIS into vector ; Function for finding longest bitonic seq ; Find LIS of array 1 in reverse form ; Reverse res to get LIS of first array ; Reverse array2 and find its LIS ; Print result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > res = new List < int > ( ) ; static int GetCeilIndex ( int [ ] arr , int [ ] T , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } static int [ ] reverse ( int [ ] a ) { int i , n = a . Length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static void LIS ( int [ ] arr , int n ) { int [ ] tailIndices = new int [ n ] ; int [ ] prevIndices = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { tailIndices [ i ] = 0 ; prevIndices [ i ] = - 1 ; } int len = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { int pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } for ( int i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) res . Add ( arr [ i ] ) ; } static void longestBitonic ( int [ ] arr1 , int n1 , int [ ] arr2 , int n2 ) { LIS ( arr1 , n1 ) ; res . Reverse ( ) ; arr2 = reverse ( arr2 ) ; LIS ( arr2 , n2 ) ; for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 4 , 3 , 2 } ; int [ ] arr2 = { 8 , 6 , 4 , 7 , 8 , 9 } ; int n1 = arr1 . Length ; int n2 = arr2 . Length ; longestBitonic ( arr1 , n1 , arr2 , n2 ) ; } }
Maximize the binary matrix by filpping submatrix once | C # program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; Driver code
using System ; public class GFG { static readonly int R = 3 ; static readonly int C = 3 ; static int cal ( int [ , ] ones , int x , int y , int k ) { return ones [ x + k - 1 , y + k - 1 ] - ones [ x - 1 , y + k - 1 ] - ones [ x + k - 1 , y - 1 ] + ones [ x - 1 , y - 1 ] ; } static int sol ( int [ , ] mat ) { int ans = 0 ; int val = 0 ; int [ , ] ones = new int [ R + 1 , C + 1 ] ; for ( int i = 1 ; i <= R ; i ++ ) for ( int j = 1 ; j <= C ; j ++ ) { if ( mat [ i - 1 , j - 1 ] == 1 ) val = 1 ; ones [ i , j ] = ones [ i - 1 , j ] + ones [ i , j - 1 ] - ones [ i - 1 , j - 1 ] + ( val ) ; } for ( int k = 1 ; k <= Math . Min ( R , C ) ; k ++ ) for ( int i = 1 ; i + k - 1 <= R ; i ++ ) for ( int j = 1 ; j + k - 1 <= C ; j ++ ) ans = Math . Max ( ans , ( ones [ R , C ] + k * k - 2 * cal ( ones , i , j , k ) ) ) ; return ans ; } static public void Main ( ) { int [ , ] mat = { { 0 , 0 , 1 } , { 0 , 0 , 1 } , { 1 , 0 , 1 } } ; Console . WriteLine ( sol ( mat ) ) ; } }
Level with maximum number of nodes | C # implementation to find the level having maximum number of Nodes ; A binary tree Node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; function to find the level having maximum number of Nodes ; Current level ; Maximum Nodes at same level ; Level having maximum Nodes ; Count Nodes in a level ; If it is maximum till now Update level_no to current level ; Pop complete current level ; Increment for next level ; Driver program to test above ; binary tree formation 2 ; / \ ; 1 3 ; / \ \ ; 4 6 8 ; / ; 5 ;
using System ; using System . Collections . Generic ; public class GfG { public class Node { public int data ; public Node left ; public Node right ; } public static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static int maxNodeLevel ( Node root ) { if ( root == null ) { return - 1 ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( root ) ; int level = 0 ; int max = int . MinValue ; int level_no = 0 ; while ( true ) { int NodeCount = q . Count ; if ( NodeCount == 0 ) { break ; } if ( NodeCount > max ) { max = NodeCount ; level_no = level ; } while ( NodeCount > 0 ) { Node Node = q . First . Value ; q . RemoveFirst ( ) ; if ( Node . left != null ) { q . AddLast ( Node . left ) ; } if ( Node . right != null ) { q . AddLast ( Node . right ) ; } NodeCount -- ; } level ++ ; } return level_no ; } public static void Main ( string [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . right . right = newNode ( 8 ) ; root . left . right . left = newNode ( 5 ) ; Console . WriteLine ( " Level ▁ having ▁ maximum ▁ number ▁ of ▁ Nodes ▁ : ▁ " + maxNodeLevel ( root ) ) ; } }
Minimum steps to minimize n as per given condition | A tabulation based solution in C # ; Driver Code
using System ; class GFG { static int getMinSteps ( int n ) { int [ ] table = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) table [ i ] = n - i ; for ( int i = n ; i >= 1 ; i -- ) { if ( ! ( i % 2 > 0 ) ) table [ i / 2 ] = Math . Min ( table [ i ] + 1 , table [ i / 2 ] ) ; if ( ! ( i % 3 > 0 ) ) table [ i / 3 ] = Math . Min ( table [ i ] + 1 , table [ i / 3 ] ) ; } return table [ 1 ] ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( getMinSteps ( n ) ) ; } }
How to solve a Dynamic Programming Problem ? | Returns the number of arrangements to form ' n ' ; base case
static int solve ( int n ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; return solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ; }