codeparrot/xlcost-text-to-code huggingface dataset:
Count number of unique ways to paint a N x 3 grid | C # program for the above approach ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Count of ways to pain a row with same colored ends ; Count of ways to pain a row with different colored ends ; Traverse up to ( N - 1 ) th row ; For same colored ends ; For different colored ends ; Print the total number of ways ; Driver code ; Function call
using System ; class GFG { static void waysToPaint ( int n ) { long same = 6 ; long diff = 6 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long sameTmp = 3 * same + 2 * diff ; long diffTmp = 2 * same + 2 * diff ; same = sameTmp ; diff = diffTmp ; } Console . WriteLine ( same + diff ) ; } static public void Main ( ) { int N = 2 ; waysToPaint ( N ) ; } }
Maximize sum by selecting X different | C # program for the above approach ; Function to find maximum sum of at most N with different index array elements such that at most X are from [ ] A , Y are from [ ] B and Z are from C [ ] ; Base Cases ; Selecting i - th element from [ ] A ; Selecting i - th element from [ ] B ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given [ ] A ; Given [ ] B ; Given C [ ] ; Given Size ; Function Call
using System ; class GFG { static int FindMaxS ( int X , int Y , int Z , int n , int [ ] A , int [ ] B , int [ ] C ) { if ( X < 0 Y < 0 Z < 0 ) return int . MinValue ; if ( n < 0 ) return 0 ; int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . Max ( ch , Math . Max ( ca , Math . Max ( co , no ) ) ) ; return maximum ; } public static void Main ( String [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int [ ] A = { 10 , 0 , 5 } ; int [ ] B = { 5 , 10 , 0 } ; int [ ] C = { 0 , 5 , 10 } ; int n = B . Length ; Console . WriteLine ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }
Rearrange array by interchanging positions of even and odd elements in the given array | C # program for the above approach ; Function to replace each even element by odd and vice - versa in a given array ; Stores length of array ; Traverse array ; If current element is even then swap it with odd ; Perform Swap ; Change the sign ; If current element is odd then swap it with even ; Perform Swap ; Change the sign ; Marked element positive ; Print readonly array ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void replace ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 == 0 && arr [ j ] % 2 != 0 ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } else if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 != 0 && arr [ j ] % 2 == 0 ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } } } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = Math . Abs ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; replace ( arr ) ; } }
Count half nodes in a Binary tree ( Iterative and Recursive ) | C # program to count half nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of half Nodes in a binary tree ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver code ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree shown in above example
using System ; class GfG { public class Node { public int data ; public Node left , right ; } static int gethalfCount ( Node root ) { if ( root == null ) return 0 ; int res = 0 ; if ( ( root . left == null && root . right != null ) || ( root . left != null && root . right == null ) ) res ++ ; res += ( gethalfCount ( root . left ) + gethalfCount ( root . right ) ) ; return res ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void Main ( ) { Node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; Console . WriteLine ( gethalfCount ( root ) ) ; } }
Minimize cost to reduce array to a single element by replacing K consecutive elements by their sum | C # program to implement the above approach ; Function to find the minimum cost to reduce given array to a single element by replacing consecutive K array elements ; If ( N - 1 ) is not multiple of ( K - 1 ) ; Store prefix sum of the array ; Iterate over the range [ 1 , N ] ; Update prefixSum [ i ] ; dp [ i , j ] : Store minimum cost to merge array elements interval [ i , j ] ; L : Stores length of interval [ i , j ] ; Iterate over each interval [ i , j ] of length L in in [ 0 , N ] ; Stores index of last element of the interval [ i , j ] ; If L is greater than K ; Update dp [ i , j ] ; If ( L - 1 ) is multiple of ( K - 1 ) ; Update dp [ i , j ] ; Return dp [ 0 , N - 1 ] ; Driver Code ; Function Call
using System ; class GFG { static int minimumCostToMergeK ( int [ ] arr , int K , int N ) { if ( ( N - 1 ) % ( K - 1 ) != 0 ) { return - 1 ; } int [ ] prefixSum = new int [ N + 1 ] ; for ( int i = 1 ; i < ( N + 1 ) ; i ++ ) { prefixSum [ i ] = ( prefixSum [ i - 1 ] + arr [ i - 1 ] ) ; } int [ , ] dp = new int [ N , N ] ; for ( int L = K ; L < ( N + 1 ) ; L ++ ) { for ( int i = 0 ; i < ( N - L + 1 ) ; i ++ ) { int j = i + L - 1 ; if ( L > K ) { int temp = int . MaxValue ; for ( int x = i ; x < j ; x += K - 1 ) { temp = Math . Min ( temp , dp [ i , x ] + dp [ x + 1 , j ] ) ; } dp [ i , j ] = temp ; } if ( ( L - 1 ) % ( K - 1 ) == 0 ) { dp [ i , j ] += ( prefixSum [ j + 1 ] - prefixSum [ i ] ) ; } } } return dp [ 0 , N - 1 ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 1 , 2 , 6 } ; int K = 3 ; int N = arr . Length ; Console . Write ( minimumCostToMergeK ( arr , K , N ) ) ; } }
Check if end of a sorted Array can be reached by repeated jumps of one more , one less or same number of indices as previous jump | C # program for the above approach ; Utility function to check if it is possible to move from index 1 to N - 1 ; Successfully reached end index ; memo [ i ] [ j ] is already calculated ; Check if there is any index having value of A [ i ] + j - 1 , A [ i ] + j or A [ i ] + j + 1 ; If A [ k ] > A [ i ] + j + 1 , can 't make a move further ; It 's possible to move A[k] ; Check is it possible to move from index k having previously taken A [ k ] - A [ i ] steps ; If yes then break the loop ; Store value of flag in memo ; Return memo [ i ] [ j ] ; Function to check if it is possible to move from index 1 to N - 1 ; Stores the memoized state ; Initialize all values as - 1 ; Initially , starting index = 1 ; Function call ; Driver Code ; Function Call
using System ; class GFG { static int N = 8 ; static int check ( int [ , ] memo , int i , int j , int [ ] A ) { if ( i == N - 1 ) return 1 ; if ( memo [ i , j ] != - 1 ) return memo [ i , j ] ; int flag = 0 , k ; for ( k = i + 1 ; k < N ; k ++ ) { if ( A [ k ] - A [ i ] > j + 1 ) break ; if ( A [ k ] - A [ i ] >= j - 1 && A [ k ] - A [ i ] <= j + 1 ) flag = check ( memo , k , A [ k ] - A [ i ] , A ) ; if ( flag != 0 ) break ; } memo [ i , j ] = flag ; return memo [ i , j ] ; } static void checkEndReach ( int [ ] A , int K ) { int [ , ] memo = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { memo [ i , j ] = - 1 ; } } int startIndex = 1 ; if ( check ( memo , startIndex , K , A ) != 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] A = { 0 , 1 , 3 , 5 , 6 , 8 , 12 , 17 } ; int K = 1 ; checkEndReach ( A , K ) ; } }
Maximum subsequence sum such that no K elements are consecutive | C # program for the above approach ; Function to find the maximum sum of a subsequence consisting of no K consecutive array elements ; Stores states of dp ; Initialise dp state ; Stores the prefix sum ; Update the prefix sum ; Base case for i < K ; For indices less than k take all the elements ; For i >= K case ; Skip each element from i to ( i - K + 1 ) to ensure that no K elements are consecutive ; Update the current dp state ; dp [ N ] stores the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call
using System ; class GFG { static int Max_Sum ( int [ ] arr , int K , int N ) { int [ ] dp = new int [ N + 1 ] ; Array . Fill ( dp , 0 ) ; int [ ] prefix = new int [ N + 1 ] ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } dp [ 0 ] = 0 ; for ( int i = 1 ; i <= K - 1 ; i ++ ) { dp [ i ] = prefix [ i ] ; } for ( int i = K ; i <= N ; ++ i ) { for ( int j = i ; j >= ( i - K + 1 ) ; j -- ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ j - 1 ] + prefix [ i ] - prefix [ j ] ) ; } } return dp [ N ] ; } static public void Main ( ) { int [ ] arr = { 4 , 12 , 22 , 18 , 34 , 12 , 25 } ; int N = arr . Length ; int K = 5 ; Console . WriteLine ( Max_Sum ( arr , K , N ) ) ; } }
Count possible splits of sum N into K integers such that the minimum is at least P | C # program for the above approach ; Function that finds the value of the Binomial Coefficient C ( n , k ) ; Stores the value of Binomial Coefficient in bottom up manner ; Base Case ; Find the value using previously stored values ; Return the value of C ( N , K ) ; Function that count the number of ways to divide N into K integers >= P such that their sum is N ; Update the value of N ; Find the binomial coefficient recursively ; Driver Code ; Given K , N , and P
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } static int waysToSplitN ( int k , int n , int P ) { int new_N = n - k * P ; return binomialCoeff ( new_N + k - 1 , new_N ) ; } public static void Main ( ) { int K = 3 , N = 8 , P = 2 ; Console . Write ( waysToSplitN ( K , N , P ) ) ; } }
Minimum pair merge operations required to make Array non | C # program to implement the above approach ; Function to find the minimum operations to make the array Non - increasing ; Size of the array ; Dp table initialization ; dp [ i ] : Stores minimum number of operations required to make subarray { A [ i ] , ... , A [ N ] } non - increasing ; Increment the value of j ; Add current value to sum ; Update the dp tables ; Return the answer ; Driver code
using System ; class GFG { static int solve ( int [ ] a ) { int n = a . Length ; int [ ] dp = new int [ n + 1 ] ; int [ ] val = new int [ n + 1 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int sum = a [ i ] ; int j = i ; while ( j + 1 < n && sum < val [ j + 1 ] ) { j ++ ; sum += a [ j ] ; } dp [ i ] = ( j - i ) + dp [ j + 1 ] ; val [ i ] = sum ; } return dp [ 0 ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 9 , 1 } ; Console . Write ( solve ( arr ) ) ; } }
Count full nodes in a Binary tree ( Iterative and Recursive ) | C # program to count full nodes in a Binary Tree using Iterative approach ; Class to represent Tree node ; Class to count full nodes of Tree ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root ; Initialize count of full nodes ; Enqueue left child ; Enqueue right child ; Driver code ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } public class BinaryTree { Node root ; int getfullCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new Queue < Node > ( ) ; queue . Enqueue ( root ) ; int count = 0 ; while ( queue . Count != 0 ) { Node temp = queue . Dequeue ( ) ; if ( temp . left != null && temp . right != null ) count ++ ; if ( temp . left != null ) { queue . Enqueue ( temp . left ) ; } if ( temp . right != null ) { queue . Enqueue ( temp . right ) ; } } return count ; } public static void Main ( String [ ] args ) { BinaryTree tree_level = new BinaryTree ( ) ; tree_level . root = new Node ( 2 ) ; tree_level . root . left = new Node ( 7 ) ; tree_level . root . right = new Node ( 5 ) ; tree_level . root . left . right = new Node ( 6 ) ; tree_level . root . left . right . left = new Node ( 1 ) ; tree_level . root . left . right . right = new Node ( 11 ) ; tree_level . root . right . right = new Node ( 9 ) ; tree_level . root . right . right . left = new Node ( 4 ) ; Console . WriteLine ( tree_level . getfullCount ( ) ) ; } }
Count of Distinct Substrings occurring consecutively in a given String | C # program to implement the above approach ; Function to count the distinct substrings placed consecutively in the given string ; Length of the string ; If length of the string does not exceed 1 ; Initialize a DP - table ; Declaring ArrayList to store strings ; Iterate from end of the string ; Iterate backward until dp table is all computed ; If character at i - th index is same as character at j - th index ; Update dp [ i ] [ j ] based on previously computed value ; Otherwise ; Condition for consecutively placed similar substring ; Return the count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int distinctSimilarSubstrings ( string str ) { int n = str . Length ; if ( n <= 1 ) return 0 ; long [ , ] dp = new long [ n + 1 , n + 1 ] ; List < string > list = new List < string > ( ) ; for ( int j = n - 1 ; j >= 0 ; j -- ) { for ( int i = j - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == str [ j ] ) { dp [ i , j ] = dp [ i + 1 , j + 1 ] + 1 ; } else { dp [ i , j ] = 0 ; } if ( dp [ i , j ] >= j - i ) { list . Add ( str . Substring ( i , j - i ) ) ; } } } return list . Count ; } static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( distinctSimilarSubstrings ( str ) ) ; } }
Finding shortest path between any two nodes using Floyd Warshall Algorithm | C # program to find the shortest path between any two nodes using Floyd Warshall Algorithm . ; Infinite value for array ; Initializing the distance and Next array ; No edge between node i and j ; Function construct the shortest path between u and v ; If there 's no path between  node u and v, simply return  an empty array ; Storing the path in a vector ; Standard Floyd Warshall Algorithm with little modification Now if we find that dis [ i , j ] > dis [ i , k ] + dis [ k , j ] then we modify next [ i , j ] = next [ i , k ] ; We cannot travel through edge that doesn 't exist ; Print the shortest path ; Driver code ; Function to initialise the distance and Next array ; Calling Floyd Warshall Algorithm , this will update the shortest distance as well as Next array ; Path from node 1 to 3 ; Path from node 0 to 2 ; Path from node 3 to 2
using System ; using System . Collections . Generic ; class GFG { static readonly int MAXN = 100 ; static int INF = ( int ) 1e7 ; static int [ , ] dis = new int [ MAXN , MAXN ] ; static int [ , ] Next = new int [ MAXN , MAXN ] ; static void initialise ( int V , int [ , ] graph ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { dis [ i , j ] = graph [ i , j ] ; if ( graph [ i , j ] == INF ) Next [ i , j ] = - 1 ; else Next [ i , j ] = j ; } } } static List < int > constructPath ( int u , int v ) { if ( Next [ u , v ] == - 1 ) return null ; List < int > path = new List < int > ( ) ; path . Add ( u ) ; while ( u != v ) { u = Next [ u , v ] ; path . Add ( u ) ; } return path ; } static void floydWarshall ( int V ) { for ( int k = 0 ; k < V ; k ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( dis [ i , k ] == INF dis [ k , j ] == INF ) continue ; if ( dis [ i , j ] > dis [ i , k ] + dis [ k , j ] ) { dis [ i , j ] = dis [ i , k ] + dis [ k , j ] ; Next [ i , j ] = Next [ i , k ] ; } } } } } static void printPath ( List < int > path ) { int n = path . Count ; for ( int i = 0 ; i < n - 1 ; i ++ ) Console . Write ( path [ i ] + " ▁ - > ▁ " ) ; Console . Write ( path [ n - 1 ] + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int V = 4 ; int [ , ] graph = { { 0 , 3 , INF , 7 } , { 8 , 0 , 2 , INF } , { 5 , INF , 0 , 1 } , { 2 , INF , INF , 0 } } ; initialise ( V , graph ) ; floydWarshall ( V ) ; List < int > path ; Console . Write ( " Shortest ▁ path ▁ from ▁ 1 ▁ to ▁ 3 : ▁ " ) ; path = constructPath ( 1 , 3 ) ; printPath ( path ) ; Console . Write ( " Shortest ▁ path ▁ from ▁ 0 ▁ to ▁ 2 : ▁ " ) ; path = constructPath ( 0 , 2 ) ; printPath ( path ) ; Console . Write ( " Shortest ▁ path ▁ from ▁ 3 ▁ to ▁ 2 : ▁ " ) ; path = constructPath ( 3 , 2 ) ; printPath ( path ) ; } }
Count sequences of length K having each term divisible by its preceding term | C # program to implement the above approach ; Stores the factors of i - th element in v [ i ] ; Function to find all the factors of N ; Iterate upto Math . Sqrt ( N ) ; Function to return the count of sequences of length K having aint terms divisible by its preceding term ; Calculate factors of i ; Initialize dp [ 0 , i ] = 0 : No subsequence of length 0 ending with i - th element exists ; Initialize dp [ 0 , i ] = 1 : Only 1 subsequence of length 1 ending with i - th element exists ; Iterate [ 2 , K ] to obtain sequences of each length ; Calculate sum of aint dp [ i - 1 , vp [ j , k ] ] ; vp [ j , k ] stores aint factors of j ; Store the sum in A [ i , j ] ; Sum of aint dp [ K , j ] obtain all K length sequences ending with j ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] vp = new List < int > [ 2009 ] ; static void finding_factors ( int n ) { int i ; for ( i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i == n ) { vp [ n ] . Add ( i ) ; } else { vp [ n ] . Add ( i ) ; vp [ n ] . Add ( n / i ) ; } } } } static int countSeq ( int N , int K ) { int i , j , k ; int [ , ] dp = new int [ 109 , 109 ] ; for ( i = 1 ; i <= N ; i ++ ) { finding_factors ( i ) ; dp [ 0 , i ] = 0 ; dp [ 1 , i ] = 1 ; } for ( i = 2 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { int sum = 0 ; for ( k = 0 ; k < vp [ j ] . Count ; k ++ ) { sum = ( sum + dp [ i - 1 , vp [ j ] [ k ] ] ) ; } dp [ i , j ] = sum ; } } int ans = 0 ; for ( j = 1 ; j <= N ; j ++ ) { ans = ( ans + dp [ K , j ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int N , K ; N = 3 ; K = 2 ; for ( int i = 0 ; i < vp . Length ; i ++ ) vp [ i ] = new List < int > ( ) ; Console . Write ( countSeq ( N , K ) + " STRNEWLINE " ) ; } }
Largest possible square submatrix with maximum AND value | C # program to find the length of longest possible square submatrix with maximum AND value from the given matrix ; Function to calculate and return the length of square submatrix with maximum AND value ; Extract dimensions ; Auxiliary array ; c : Stores the maximum value in the matrix p : Stores the number of elements in the submatrix having maximum AND value ; Iterate over the matrix to fill the auxiliary matrix ; Find the max element in the matrix side by side ; Fill first row and column with 1 's ; For every cell , check if the elements at the left , top and top left cells from the current cell are equal or not ; Store the minimum possible submatrix size these elements are part of ; Store 1 otherwise ; Checking maximum value ; If the maximum AND value occurs more than once ; Update the maximum size of submatrix ; Final output ; Driver code
using System ; class GFG { static int MAX_value ( int [ , ] arr ) { int row = arr . GetLength ( 0 ) ; int col = arr . GetLength ( 1 ) ; int [ , ] dp = new int [ row , col ] ; int i = 0 , j = 0 ; int c = arr [ 0 , 0 ] , p = 0 ; int d = row ; for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( c < arr [ i , j ] ) { c = arr [ i , j ] ; } if ( i == 0 j == 0 ) { dp [ i , j ] = 1 ; } else { if ( arr [ i - 1 , j - 1 ] == arr [ i , j ] && arr [ i - 1 , j ] == arr [ i , j ] && arr [ i , j - 1 ] == arr [ i , j ] ) { dp [ i , j ] = Math . Min ( dp [ i - 1 , j - 1 ] , Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ) + 1 ; } else { dp [ i , j ] = 1 ; } } } } for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( arr [ i , j ] == c ) { if ( p < dp [ i , j ] ) { p = dp [ i , j ] ; } } } } return p * p ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 9 , 9 , 3 , 3 , 4 , 4 } , { 9 , 9 , 7 , 7 , 7 , 4 } , { 1 , 2 , 7 , 7 , 7 , 4 } , { 4 , 4 , 7 , 7 , 7 , 4 } , { 5 , 5 , 1 , 1 , 2 , 7 } , { 2 , 7 , 1 , 1 , 4 , 4 } } ; Console . Write ( MAX_value ( arr ) + " STRNEWLINE " ) ; } }
Smallest power of 2 consisting of N digits | C # program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
using System ; class GFG { static int smallestNum ( int n ) { for ( int i = 2 ; ; i *= 2 ) { int length = ( int ) ( Math . Log10 ( i ) ) + 1 ; if ( length == n ) return ( int ) ( Math . Log ( i ) / Math . Log ( 2 ) ) ; } } public static void Main ( ) { int n = 4 ; Console . Write ( smallestNum ( n ) ) ; } }
Count full nodes in a Binary tree ( Iterative and Recursive ) | C # program to count full nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of full Nodes in a binary tree ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
using System ; class GfG { public class Node { public int data ; public Node left , right ; } static int getfullCount ( Node root ) { if ( root == null ) return 0 ; int res = 0 ; if ( root . left != null && root . right != null ) res ++ ; res += ( getfullCount ( root . left ) + getfullCount ( root . right ) ) ; return res ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void Main ( ) { Node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; Console . WriteLine ( getfullCount ( root ) ) ; } }
Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | C # implementation to count decodings ; A Dynamic programming based function to count decodings in digit sequence ; For base condition "01123" should return 0 ; Using last two calculated values , calculate for ith index ; Change bool to int ; Return the required answer ; Driver Code ; Function call
using System ; class GFG { static int countDecodingDP ( String digits , int n ) { if ( digits [ 0 ] == '0' ) { return 0 ; } int count0 = 1 , count1 = 1 , count2 ; for ( int i = 2 ; i <= n ; i ++ ) { int dig1 = 0 , dig2 , dig3 = 0 ; if ( digits [ i - 1 ] != '0' ) { dig1 = 1 ; } if ( digits [ i - 2 ] == '1' ) { dig2 = 1 ; } else dig2 = 0 ; if ( digits [ i - 2 ] == '2' && digits [ i - 1 ] < '7' ) { dig3 = 1 ; } count2 = dig1 * count1 + dig2 + dig3 * count0 ; count0 = count1 ; count1 = count2 ; } return count1 ; } public static void Main ( String [ ] args ) { String digits = "1234" ; int n = digits . Length ; Console . Write ( countDecodingDP ( digits , n ) ) ; } }