codeparrot/xlcost-text-to-code huggingface dataset:
Construct the largest number whose sum of cost of digits is K | C # program for the above approach ; Function to find the maximum number among the two numbers S and T ; If "0" exists in the string S ; If "0" exists in the string T ; Else return the maximum number formed ; Recursive function to find maximum number formed such that the sum of cost of digits of formed number is K ; Base Case ; Return the stored state ; Including the digit ( idx + 1 ) ; Excluding the digit ( idx + 1 ) ; Store the result and return ; Function to find the maximum number formed such that the sum of the cost digits in the formed number is K ; Stores all Dp - states ; Recursive Call ; Return the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string getMaximum ( string S , string T ) { if ( S . IndexOf ( "0" ) > - 1 ) return T ; if ( T . IndexOf ( "0" ) > - 1 ) return S ; return ( S . Length > T . Length ? S : T ) ; } static string recursion ( int [ ] arr , int idx , int N , int K , List < List < string > > dp ) { if ( K == 0 ) { return " " ; } if ( K < 0 idx == N ) { return "0" ; } if ( dp [ idx ] [ K ] != " - 1" ) return dp [ idx ] [ K ] ; string include = ( idx + 1 ) . ToString ( ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ; string exclude = recursion ( arr , idx + 1 , N , K , dp ) ; return dp [ idx ] [ K ] = getMaximum ( include , exclude ) ; } static string largestNumber ( int [ ] arr , int N , int K ) { List < List < string > > dp = new List < List < string > > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) { dp . Add ( new List < string > ( ) ) ; for ( int j = 0 ; j < K + 1 ; j ++ ) { dp [ i ] . Add ( " - 1" ) ; } } string ans = recursion ( arr , 0 , N , K , dp ) ; return ( ans == " " ? "0" : ans ) ; } static void Main ( ) { int [ ] arr = { 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 } ; int K = 14 ; int N = arr . Length ; Console . Write ( largestNumber ( arr , N , K ) ) ; } }
Count of N | C # program for the above approach ; Function to calculate count of ' N ' digit numbers such that bitwise AND of adjacent digits is 0. ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; For remaining positions , any digit from [ 0 - 9 ] can be placed after checking the conditions . ; Check if bitwise AND of current digit and previous digit is 0. ; Return answer ; Driver code ; Initializing dp array with - 1. ; Given Input ; Function call
using System ; class GFG { static int [ , ] dp = new int [ 100 , 10 ] ; static int countOfNumbers ( int digit , int prev , int n ) { if ( digit == n + 1 ) { return 1 ; } int val = dp [ digit , prev ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } else { for ( int i = 0 ; i <= 9 ; ++ i ) { if ( ( i & prev ) == 0 ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } } return val ; } public static void Main ( string [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { dp [ i , j ] = - 1 ; } } int N = 3 ; Console . WriteLine ( countOfNumbers ( 1 , 0 , N ) ) ; } }
Maximum sum among all ( a x b ) submatrices for given Q queries | Function to preprcess input mat [ N ] [ M ] . This function mainly fills dp [ N ] [ M ] such that dp [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to dp [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using dp [ ] [ ] which is built by the preprocess function ; Result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add dp [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Function to find the maximum sum among all ( a x b ) sub - matrices of the matrix ; Function call ; Run a loop for finding answer for all queries ; Driver Code ; Given input ; Function call ; Print answer for all queries
using System ; public class GFG { static void preProcess ( int [ , ] mat , int [ , ] dp , int n , int m ) { for ( int i = 0 ; i < m ; i ++ ) { dp [ 0 , i ] = mat [ 0 , i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] + mat [ i , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j < m ; j ++ ) { dp [ i , j ] += dp [ i , j - 1 ] ; } } } static int sumQuery ( int [ , ] dp , int tli , int tlj , int rbi , int rbj ) { int res = dp [ rbi , rbj ] ; if ( tli > 0 ) res = res - dp [ tli - 1 , rbj ] ; if ( tlj > 0 ) res = res - dp [ rbi , tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + dp [ tli - 1 , tlj - 1 ] ; return res ; } static int [ ] maxSubMatrixSumQueries ( int [ , ] mat , int n , int m , int [ , ] queries , int q ) { int [ , ] dp = new int [ n , m ] ; preProcess ( mat , dp , n , m ) ; int [ ] maxSum = new int [ queries . GetLength ( 0 ) ] ; for ( int qi = 0 ; qi < q ; qi ++ ) { for ( int i = 0 ; i < n - queries [ qi , 0 ] + 1 ; i ++ ) { for ( int j = 0 ; j < m - queries [ qi , 1 ] + 1 ; j ++ ) { maxSum [ qi ] = Math . Max ( maxSum [ qi ] , sumQuery ( dp , i , j , i + queries [ qi , 0 ] - 1 , j + queries [ qi , 1 ] - 1 ) ) ; } } } return maxSum ; } static public void Main ( ) { int n = 3 , m = 4 ; int [ , ] mat = { { 1 , 2 , 3 , 9 } , { 4 , 5 , 6 , 2 } , { 8 , 3 , 2 , 6 } } ; int Q = 3 ; int [ , ] Queries = { { 1 , 1 } , { 2 , 2 } , { 3 , 3 } } ; int [ ] maxSum = maxSubMatrixSumQueries ( mat , n , m , Queries , Q ) ; for ( int i = 0 ; i < Q ; i ++ ) { Console . Write ( maxSum [ i ] + " ▁ " ) ; } } }
Number of ways such that only K bars are visible from the left | C # implementation for the above approach ; dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Initialize dp array ; Function call
using System ; public class GFG { static int [ , ] dp = new int [ 1005 , 1005 ] ; static int KvisibleFromLeft ( int N , int K ) { if ( dp [ N , K ] != - 1 ) return dp [ N , K ] ; if ( N == K ) return dp [ N , K ] = 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return dp [ N , K ] = ans ; } return dp [ N , K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } public static void Main ( String [ ] args ) { int N = 5 , K = 2 ; for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( KvisibleFromLeft ( N , K ) ) ; } }
Number of distinct words of size N with at most K contiguous vowels | C # program for the above approach ; Power function to calculate long powers with mod ; Function for finding number of ways to create string with length N and atmost K contiguous vowels ; Array dp to store number of ways ; dp [ i ] [ 0 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 1 ] . . dp [ i - 1 ] [ k ] ) * 21 ; Now setting sum to be dp [ i ] [ 0 ] ; If j > i , no ways are possible to create a string with length i and vowel j ; If j = i all the character should be vowel ; dp [ i ] [ j ] relation with dp [ i - 1 ] [ j - 1 ] ; Adding dp [ i ] [ j ] in the sum ; Driver Code ; Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int kvowelwords ( int N , int K ) { int i , j ; int MOD = 1000000007 ; int [ , ] dp = new int [ N + 1 , K + 1 ] ; int sum = 1 ; for ( i = 1 ; i <= N ; i ++ ) { dp [ i , 0 ] = sum * 21 ; dp [ i , 0 ] %= MOD ; sum = dp [ i , 0 ] ; for ( j = 1 ; j <= K ; j ++ ) { if ( j > i ) dp [ i , j ] = 0 ; else if ( j == i ) { dp [ i , j ] = power ( 5 , i , MOD ) ; } else { dp [ i , j ] = dp [ i - 1 , j - 1 ] * 5 ; } dp [ i , j ] %= MOD ; sum += dp [ i , j ] ; sum %= MOD ; } } return sum ; } public static void Main ( ) { int N = 3 ; int K = 3 ; Console . Write ( kvowelwords ( N , K ) ) ; } }
Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically | C # program for the above approach ; Function to find maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertically ; Stores the intermediate transition states ; Place the first rectangle horizontally ; Place the first rectangle vertically ; Place horizontally ; Stores the difference in height of current and previous rectangle ; Take maximum out of two options ; Place Vertically ; Stores the difference in height of current and previous rectangle ; Take maximum out two options ; Print maximum of horizontal or vertical alignment of the last rectangle ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maxBoundary ( int N , List < Tuple < int , int > > V ) { int [ , ] dp = new int [ N , 2 ] ; dp [ 0 , 0 ] = V [ 0 ] . Item1 ; dp [ 0 , 1 ] = V [ 0 ] . Item2 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i , 0 ] = V [ i ] . Item1 ; int height1 = Math . Abs ( V [ i - 1 ] . Item2 - V [ i ] . Item2 ) ; int height2 = Math . Abs ( V [ i - 1 ] . Item1 - V [ i ] . Item2 ) ; dp [ i , 0 ] += Math . Max ( height1 + dp [ i - 1 , 0 ] , height2 + dp [ i - 1 , 1 ] ) ; dp [ i , 1 ] = V [ i ] . Item2 ; int vertical1 = Math . Abs ( V [ i ] . Item1 - V [ i - 1 ] . Item2 ) ; int vertical2 = Math . Abs ( V [ i ] . Item1 - V [ i - 1 ] . Item1 ) ; dp [ i , 1 ] += Math . Max ( vertical1 + dp [ i - 1 , 0 ] , vertical2 + dp [ i - 1 , 1 ] ) ; } Console . WriteLine ( Math . Max ( dp [ N - 1 , 0 ] , dp [ N - 1 , 1 ] ) ) ; } static void Main ( ) { int N = 5 ; List < Tuple < int , int > > V = new List < Tuple < int , int > > ( ) ; V . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; V . Add ( new Tuple < int , int > ( 3 , 8 ) ) ; V . Add ( new Tuple < int , int > ( 1 , 10 ) ) ; V . Add ( new Tuple < int , int > ( 7 , 14 ) ) ; V . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; maxBoundary ( N , V ) ; } }
Program to find the Nth natural number with exactly two bits set | Set 2 | C # program for the above approach ; Function to find the Nth number with exactly two bits set ; Initialize variables ; Initialize the range in which the value of ' a ' is present ; Perform Binary Search ; Find the mid value ; Update the range using the mid value t ; Find b value using a and N ; Print the value 2 ^ a + 2 ^ b ; Driver Code ; Function Call
using System ; class GFG { static void findNthNum ( int N ) { int a = 0 , b , left ; int right , mid ; int t ; left = 1 ; right = N ; while ( left <= right ) { mid = left + ( right - left ) / 2 ; t = ( mid * ( mid + 1 ) ) / 2 ; if ( t < N ) { left = mid + 1 ; } else if ( t == N ) { a = mid ; break ; } else { a = mid ; right = mid - 1 ; } } t = a - 1 ; b = N - ( t * ( t + 1 ) ) / 2 - 1 ; Console . Write ( ( 1 << a ) + ( 1 << b ) ) ; } public static void Main ( ) { int N = 15 ; findNthNum ( N ) ; } }
Ways to sum to N using Natural Numbers up to K with repetitions allowed | C # program for the above approach ; Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Driver Code ; Given inputs
using System ; class GFG { static int NumberOfways ( int N , int K ) { int [ ] dp = new int [ ( N + 1 ) ] ; dp [ 0 ] = 1 ; for ( int row = 1 ; row < K + 1 ; row ++ ) { for ( int col = 1 ; col < N + 1 ; col ++ ) { if ( col >= row ) dp [ col ] = dp [ col ] + dp [ col - row ] ; } } return ( dp [ N ] ) ; } public static void Main ( ) { int N = 8 ; int K = 2 ; Console . WriteLine ( NumberOfways ( N , K ) ) ; } }
Queries to check if array elements from indices [ L , R ] forms an Arithmetic Progression or not | C # code for above approach ; Function to check if the given range of queries form an AP or not in the given array arr [ ] ; Stores length of the longest subarray forming AP for every array element ; Iterate over the range [ 0 , N ] ; Stores the index of the last element of forming AP ; Iterate until the element at index ( j , j + 1 ) forms AP ; Increment j by 1 ; Traverse the current subarray over the range [ i , j - 1 ] ; Update the length of the longest subarray at index k ; Update the value of i ; Traverse the given queries ; Print the result ; Otherwise ; Driver Code
using System ; public class GFG { static void findAPSequence ( int [ ] arr , int N , int [ , ] Q , int M ) { int [ ] dp = new int [ N + 5 ] ; for ( int i = 0 ; i + 1 < N ; ) { int j = i + 1 ; while ( j + 1 < N && arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) j ++ ; for ( int k = i ; k < j ; k ++ ) { dp [ k ] = j - k ; } i = j ; } for ( int i = 0 ; i < M ; i ++ ) { if ( dp [ Q [ i , 0 ] ] >= Q [ i , 1 ] - Q [ i , 0 ] ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } } static public void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 } ; int [ , ] Q = { { 0 , 3 } , { 3 , 4 } , { 2 , 4 } } ; int N = arr . Length ; int M = Q . GetLength ( 0 ) ; findAPSequence ( arr , N , Q , M ) ; } }
Minimize difference between sum of two K | C # program for the above approach ; Stores the values at recursive states ; Function to find the minimum difference between sum of two K - length subsets ; Base Case ; If k1 and k2 are 0 , then return the absolute difference between sum1 and sum2 ; Otherwise , return INT_MAX ; If the result is already computed , return the result ; Store the 3 options ; Including the element in first subset ; Including the element in second subset ; Not including the current element in both the subsets ; Store minimum of 3 values obtained ; Return the value for the current states ; Driver Code
using System ; class GFG { static int [ , , ] dp = new int [ 100 , 100 , 100 ] ; static int minSumDifference ( int [ ] arr , int n , int k1 , int k2 , int sum1 , int sum2 ) { if ( n < 0 ) { if ( k1 == 0 && k2 == 0 ) { return Math . Abs ( sum1 - sum2 ) ; } else { return Int32 . MaxValue ; } } if ( dp [ n , sum1 , sum2 ] != - 1 ) { return dp [ n , sum1 , sum2 ] ; } int op1 = Int32 . MaxValue ; int op2 = Int32 . MaxValue ; int op3 = Int32 . MaxValue ; if ( k1 > 0 ) { op1 = minSumDifference ( arr , n - 1 , k1 - 1 , k2 , sum1 + arr [ n ] , sum2 ) ; } if ( k2 > 0 ) { op2 = minSumDifference ( arr , n - 1 , k1 , k2 - 1 , sum1 , sum2 + arr [ n ] ) ; } op3 = minSumDifference ( arr , n - 1 , k1 , k2 , sum1 , sum2 ) ; dp [ n , sum1 , sum2 ] = Math . Min ( op1 , Math . Min ( op2 , op3 ) ) ; return dp [ n , sum1 , sum2 ] ; } static public void Main ( ) { int [ ] arr = { 12 , 3 , 5 , 6 , 7 , 17 } ; int K = 2 ; int N = arr . Length ; for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int k = 0 ; k < 100 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } Console . WriteLine ( minSumDifference ( arr , N - 1 , K , K , 0 , 0 ) ) ; } }
Geek | C # program for the above approach ; Function to calculate the N - th Geek - onacci Number ; Stores the geekonacci series ; Store the first three terms of the series ; Iterate over the range [ 3 , N ] ; Update the value of arr [ i ] as the sum of previous 3 terms in the series ; Return the last element of arr [ ] as the N - th term ; Driver Code
using System ; class GFG { static int find ( int A , int B , int C , int N ) { int [ ] arr = new int [ N ] ; arr [ 0 ] = A ; arr [ 1 ] = B ; arr [ 2 ] = C ; for ( int i = 3 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] + arr [ i - 3 ] ; } return arr [ N - 1 ] ; } public static void Main ( string [ ] args ) { int A = 1 , B = 3 , C = 2 , N = 4 ; Console . Write ( find ( A , B , C , N ) ) ; } }
Minimum days required to cure N persons | C # Program for the above approach ; Function to find minimum count of days required to give a cure such that the high risk person and risk person does not get a dose on same day . ; Stores count of persons whose age is less than or equal to 10 and greater than or equal to 60. ; Stores the count of persons whose age is in the range [ 11 , 59 ] ; Traverse the array arr [ ] ; If age less than or equal to 10 or greater than or equal to 60 ; Update risk ; Update normal_risk ; Calculate days to cure risk and normal_risk persons ; Print the days ; Driver code ; Given array ; Size of the array ; Given P
using System ; class GFG { static void daysToCure ( int [ ] arr , int N , int P ) { int risk = 0 ; int normal_risk = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 60 arr [ i ] <= 10 ) { risk ++ ; } else { normal_risk ++ ; } } int days = ( risk / P ) + ( normal_risk / P ) ; if ( risk % P > 0 ) { days ++ ; } if ( normal_risk % P > 0 ) { days ++ ; } Console . Write ( days ) ; } static void Main ( ) { int [ ] arr = { 9 , 80 , 27 , 72 , 79 } ; int N = arr . Length ; int P = 2 ; daysToCure ( arr , N , P ) ; } }
Count numbers from a given range whose product of digits is K | C # program to implement the above approach ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; If count of digits in a number greater than count of digits in X ; If product of digits of a number equal to K ; If overlapping subproblems already occurred ; Stores count of numbers whose product of digits is K ; Check if the numbers exceeds K or not ; Iterate over all possible value of i - th digits ; If number contains leading 0 ; Update res ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose prod of digits is K ; Stores numbers in the form of String ; Stores overlapping subproblems ; Initialize [ , ] dp [ ] to - 1 ; Stores count of numbers in the range [ 0 , R ] whose product of digits is k ; Update str ; Initialize [ , ] dp [ ] to - 1 ; Stores count of numbers in the range [ 0 , L - 1 ] whose product of digits is k ; Driver Code
using System ; class GFG { static readonly int M = 100 ; static int cntNum ( String X , int i , int prod , int K , int st , int tight , int [ , , , ] dp ) { if ( i >= X . Length prod > K ) { return prod == K ? 1 : 0 ; } if ( dp [ prod , i , tight , st ] != - 1 ) { return dp [ prod , i , tight , st ] ; } int res = 0 ; int end = tight > 0 ? X [ i ] - '0' : 9 ; for ( int j = 0 ; j <= end ; j ++ ) { if ( j == 0 && st == 0 ) { res += cntNum ( X , i + 1 , prod , K , 0 , ( tight & ( ( j == end ) ? 1 : 0 ) ) , dp ) ; } else { res += cntNum ( X , i + 1 , prod * j , K , 1 , ( tight & ( ( j == end ) ? 1 : 0 ) ) , dp ) ; } } return dp [ prod , i , tight , st ] = res ; } static int UtilCntNumRange ( int L , int R , int K ) { String str = String . Join ( " " , R ) ; int [ , , , ] dp = new int [ M , M , 2 , 2 ] ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; } } int cntR = cntNum ( str , 0 , 1 , K , 0 , 1 , dp ) ; str = String . Join ( " " , L - 1 ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; } } int cntL = cntNum ( str , 0 , 1 , K , 0 , 1 , dp ) ; return ( cntR - cntL ) ; } public static void Main ( String [ ] args ) { int L = 20 , R = 10000 , K = 14 ; Console . Write ( UtilCntNumRange ( L , R , K ) ) ; } }
Count all possible N | C # program to find absolute difference between the sum of all odd frequenct and even frequent elements in an array ; Function to find the number of vowel permutations possible ; To avoid the large output value ; Initialize 2D dp array ; Initialize dp [ 1 ] [ i ] as 1 since string of length 1 will consist of only one vowel in the string ; Directed graph using the adjacency matrix ; Iterate over the range [ 1 , N ] ; Traverse the directed graph ; Traversing the list ; Update dp [ i + 1 ] [ u ] ; Stores total count of permutations ; Return count of permutations ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countVowelPermutation ( int n ) { int MOD = ( int ) ( 1e9 + 7 ) ; long [ , ] dp = new long [ n + 1 , 5 ] ; for ( int i = 0 ; i < 5 ; i ++ ) { dp [ 1 , i ] = 1 ; } List < List < int > > relation = new List < List < int > > ( ) ; relation . Add ( new List < int > { 1 } ) ; relation . Add ( new List < int > { 0 , 2 } ) ; relation . Add ( new List < int > { 0 , 1 , 3 , 4 } ) ; relation . Add ( new List < int > { 2 , 4 } ) ; relation . Add ( new List < int > { 0 } ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int u = 0 ; u < 5 ; u ++ ) { dp [ i + 1 , u ] = 0 ; foreach ( int v in relation [ u ] ) { dp [ i + 1 , u ] += dp [ i , v ] % MOD ; } } } long ans = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans = ( ans + dp [ n , i ] ) % MOD ; } return ( int ) ans ; } static void Main ( ) { int N = 2 ; Console . WriteLine ( countVowelPermutation ( N ) ) ; } }
Queries to calculate Bitwise OR of each subtree of a given node in an N | C # program to generate n - bit Gray codes ; Maximum Number of nodes ; Adjacency list ; Stores Bitwise OR of each node ; Function to Add edges to the Tree ; Traverse the edges ; Add edges ; Function to perform DFS Traversal on the given tree ; Initialize answer with bitwise OR of current node ; Iterate over each child of the current node ; Skip parent node ; Call DFS for each child ; Taking bitwise OR of the answer of the child to find node 's OR value ; Function to call DFS from the '=  root for precomputing answers ; Function to calculate and print the Bitwise OR for Q queries ; Perform preprocessing ; Iterate over each given query ; Utility function to find and print bitwise OR for Q queries ; Function to Add edges to graph ; Function call ; Driver function ; Number of nodes ; Function call
using System ; using System . Collections . Generic ; class GFG { static int N = ( int ) 1e5 + 5 ; static List < List < int > > adj ; static int [ ] answer ; static void AddEdgesToGraph ( int [ , ] Edges , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } } static void DFS ( int node , int parent , int [ ] Val ) { answer [ node ] = Val [ node ] ; foreach ( int child in adj [ node ] ) { if ( child == parent ) continue ; DFS ( child , node , Val ) ; answer [ node ] = ( answer [ node ] answer [ child ] ) ; } } static void preprocess ( int [ ] Val ) { DFS ( 1 , - 1 , Val ) ; } static void findSubtreeOR ( int [ ] Queries , int Q , int [ ] Val ) { preprocess ( Val ) ; for ( int i = 0 ; i < Q ; i ++ ) { Console . Write ( answer [ Queries [ i ] ] + " ▁ " ) ; } } static void findSubtreeORUtil ( int N , int [ , ] Edges , int [ ] Val , int [ ] Queries , int Q ) { AddEdgesToGraph ( Edges , N ) ; findSubtreeOR ( Queries , Q , Val ) ; } public static void Main ( String [ ] args ) { adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) adj . Add ( new List < int > ( ) ) ; answer = new int [ N ] ; N = 5 ; int [ , ] Edges = { { 1 , 2 } , { 1 , 3 } , { 3 , 4 } , { 3 , 5 } } ; int [ ] Val = { 0 , 2 , 3 , 4 , 8 , 16 } ; int [ ] Queries = { 2 , 3 , 1 } ; int Q = Queries . Length ; findSubtreeORUtil ( N , Edges , Val , Queries , Q ) ; } }