codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of special permutations | C # program to count the number of required permutations ; Function to return the number of ways to choose r objects out of n objects ; Function to return the number of derangements of n ; Function to return the required number of permutations ; Ways to choose i indices from n indices ; Dearangements of ( n - i ) indices System . out . println ( ans ) ; ; Driver Code to test above functions
using System ; public class GFG { static int nCr ( int n , int r ) { int ans = 1 ; if ( r > n - r ) r = n - r ; for ( int i = 0 ; i < r ; i ++ ) { ans *= ( n - i ) ; ans /= ( i + 1 ) ; } return ans ; } static int countDerangements ( int n ) { int [ ] der = new int [ n + 3 ] ; der [ 0 ] = 1 ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; i ++ ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; } static int countPermutations ( int n , int k ) { int ans = 0 ; for ( int i = n - k ; i <= n ; i ++ ) { int ways = nCr ( n , i ) ; ans += ( ways * countDerangements ( n - i ) ) ; } return ans ; } public static void Main ( ) { int n = 5 , k = 3 ; Console . WriteLine ( countPermutations ( n , k ) ) ; } }
Paths with maximum number of ' a ' from ( 1 , 1 ) to ( X , Y ) vertically or horizontally | C # program to find paths with maximum number of ' a ' from ( 1 , 1 ) to ( X , Y ) vertically or horizontally ; Function to answer queries ; Iterate till query ; Decrease to get 0 - based indexing ; Print answer ; Function that pre - computes the dp array ; Check fo the first character ; Iterate in row and columns ; If not first row or not first column ; Not first row ; Not first column ; If it is not ' a ' then increase by 1 ; Driver code ; character N X N array ; queries ; number of queries ; function call to pre - compute ; function call to answer every query
using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int n = 3 ; static int [ , ] dp = new int [ n , n ] ; static void answerQueries ( pair [ ] queries , int q ) { for ( int i = 0 ; i < q ; i ++ ) { int x = queries [ i ] . first ; x -- ; int y = queries [ i ] . second ; y -- ; Console . WriteLine ( dp [ x , y ] ) ; } } static void pre_compute ( char [ , ] a ) { if ( a [ 0 , 0 ] == ' a ' ) dp [ 0 , 0 ] = 0 ; else dp [ 0 , 0 ] = 1 ; for ( int row = 0 ; row < n ; row ++ ) { for ( int col = 0 ; col < n ; col ++ ) { if ( row != 0 col != 0 ) dp [ row , col ] = int . MaxValue ; if ( row != 0 ) { dp [ row , col ] = Math . Min ( dp [ row , col ] , dp [ row - 1 , col ] ) ; } if ( col != 0 ) { dp [ row , col ] = Math . Min ( dp [ row , col ] , dp [ row , col - 1 ] ) ; } if ( a [ row , col ] != ' a ' && ( row != 0 col != 0 ) ) dp [ row , col ] += 1 ; } } } public static void Main ( String [ ] args ) { char [ , ] a = { { ' a ' , ' b ' , ' a ' } , { ' a ' , ' c ' , ' d ' } , { ' b ' , ' a ' , ' b ' } } ; pair [ ] queries = { new pair ( 1 , 3 ) , new pair ( 3 , 3 ) } ; int q = 2 ; pre_compute ( a ) ; answerQueries ( queries , q ) ; } }
Ways to place K bishops on an NÃ — N chessboard so that no two attack | C # implementation of the approach ; returns the number of squares in diagonal i ; returns the number of ways to fill a n * n chessboard with k bishops so that no two bishops attack each other . ; return 0 if the number of valid places to be filled is less than the number of bishops ; dp table to store the values ; Setting the base conditions ; calculate the required number of ways ; stores the answer ; Driver code
using System ; class GFG { static int squares ( int i ) { if ( ( i & 1 ) == 1 ) return i / 4 * 2 + 1 ; else return ( i - 1 ) / 4 * 2 + 2 ; } static long bishop_placements ( int n , int k ) { if ( k > 2 * n - 1 ) return 0 ; long [ , ] dp = new long [ n * 2 , k + 1 ] ; for ( int i = 0 ; i < n * 2 ; i ++ ) dp [ i , 0 ] = 1 ; dp [ 1 , 1 ] = 1 ; for ( int i = 2 ; i < n * 2 ; i ++ ) { for ( int j = 1 ; j <= k ; j ++ ) dp [ i , j ] = dp [ i - 2 , j ] + dp [ i - 2 , j - 1 ] * ( squares ( i ) - j + 1 ) ; } long ans = 0 ; for ( int i = 0 ; i <= k ; i ++ ) { ans += dp [ n * 2 - 1 , i ] * dp [ n * 2 - 2 , k - i ] ; } return ans ; } static public void Main ( ) { int n = 2 ; int k = 2 ; long ans = bishop_placements ( n , k ) ; Console . WriteLine ( ans ) ; } }
Number of ways to partition a string into two balanced subsequences | C # implementation of the approach ; For maximum length of input string ; Declaring the DP table ; Declaring the prefix array ; Function to calculate the number of valid assignments ; Return 1 if X is balanced . ; Increment the count if it an opening bracket ; Decrement the count if it a closing bracket ; Driver code ; Initializing the DP table ; Creating the prefix array ; Initial value for c_x and c_y is zero
using System ; public class GFG { static int MAX = 10 ; static int [ , ] F = new int [ MAX , MAX ] ; static int [ ] C = new int [ MAX ] ; static int noOfAssignments ( string S , int n , int i , int c_x ) { if ( F [ i , c_x ] != - 1 ) { return F [ i , c_x ] ; } if ( i == n ) { if ( c_x == 1 ) { F [ i , c_x ] = 0 ; } else { F [ i , c_x ] = 1 ; } return F [ i , c_x ] ; } int c_y = C [ i ] - c_x ; if ( S [ i ] == ' ( ' ) { F [ i , c_x ] = noOfAssignments ( S , n , i + 1 , c_x + 1 ) + noOfAssignments ( S , n , i + 1 , c_x ) ; return F [ i , c_x ] ; } F [ i , c_x ] = 0 ; if ( c_x == 1 ) { F [ i , c_x ] += noOfAssignments ( S , n , i + 1 , c_x - 1 ) ; } if ( c_y == 1 ) { F [ i , c_x ] += noOfAssignments ( S , n , i + 1 , c_x ) ; } return F [ i , c_x ] ; } public static void Main ( ) { string S = " ( ) " ; int n = S . Length ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { F [ i , j ] = - 1 ; } } C [ 0 ] = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( S [ i ] == ' ( ' ) { C [ i + 1 ] = C [ i ] + 1 ; } else { C [ i + 1 ] = C [ i ] - 1 ; } } Console . WriteLine ( noOfAssignments ( S , n , 0 , 0 ) ) ; } }
Minimum sum falling path in a NxN grid | C # Program to minimum required sum ; Function to return minimum path falling sum ; R = Row and C = Column We begin from second last row and keep adding maximum sum . ; best = min ( A [ R + 1 , C - 1 ] , A [ R + 1 , C ] , A [ R + 1 , C + 1 ] ) ; Driver program ; function to print required answer
using System ; class GFG { static int n = 3 ; static int minFallingPathSum ( int [ , ] A ) { for ( int R = n - 2 ; R >= 0 ; -- R ) { for ( int C = 0 ; C < n ; ++ C ) { int best = A [ R + 1 , C ] ; if ( C > 0 ) best = Math . Min ( best , A [ R + 1 , C - 1 ] ) ; if ( C + 1 < n ) best = Math . Min ( best , A [ R + 1 , C + 1 ] ) ; A [ R , C ] = A [ R , C ] + best ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < n ; ++ i ) ans = Math . Min ( ans , A [ 0 , i ] ) ; return ans ; } public static void Main ( ) { int [ , ] A = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . WriteLine ( minFallingPathSum ( A ) ) ; } }
Find the maximum sum of Plus shape pattern in a 2 | C # program to find the maximum value of a + shaped pattern in 2 - D array ; Function to return maximum Plus value ; Initializing answer with the minimum value ; Initializing all four arrays ; Initializing left and up array . ; Initializing right and down array . ; calculating value of maximum Plus ( + ) sign ; Driver code ; Function call to find maximum value
using System ; class GFG { public static int N = 100 ; public static int n = 3 , m = 4 ; public static int maxPlus ( int [ , ] arr ) { int ans = int . MinValue ; int [ , ] left = new int [ N , N ] ; int [ , ] right = new int [ N , N ] ; int [ , ] up = new int [ N , N ] ; int [ , ] down = new int [ N , N ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { left [ i , j ] = Math . Max ( 0 , ( ( j != 0 ) ? left [ i , j - 1 ] : 0 ) ) + arr [ i , j ] ; up [ i , j ] = Math . Max ( 0 , ( ( i != 0 ) ? up [ i - 1 , j ] : 0 ) ) + arr [ i , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { right [ i , j ] = Math . Max ( 0 , ( j + 1 == m ? 0 : right [ i , j + 1 ] ) ) + arr [ i , j ] ; down [ i , j ] = Math . Max ( 0 , ( i + 1 == n ? 0 : down [ i + 1 , j ] ) ) + arr [ i , j ] ; } } for ( int i = 1 ; i < n - 1 ; ++ i ) for ( int j = 1 ; j < m - 1 ; ++ j ) ans = Math . Max ( ans , up [ i - 1 , j ] + down [ i + 1 , j ] + left [ i , j - 1 ] + right [ i , j + 1 ] + arr [ i , j ] ) ; return ans ; } static void Main ( ) { int [ , ] arr = new int [ , ] { { 1 , 1 , 1 , 1 } , { - 6 , 1 , 1 , - 4 } , { 1 , 1 , 1 , 1 } } ; Console . Write ( maxPlus ( arr ) ) ; } }
Total number of different staircase that can made from N boxes | C # program to find the total number of different staircase that can made from N boxes ; Function to find the total number of different staircase that can made from N boxes ; DP table , there are two states . First describes the number of boxes and second describes the step ; Initialize all the elements of the table to zero ; Base case ; When step is equal to 2 ; When step is greater than 2 ; Count the total staircase from all the steps ; Driver Code
using System ; class GFG { static int countStaircases ( int N ) { int [ , ] memo = new int [ N + 5 , N + 5 ] ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = 0 ; j <= N ; j ++ ) { memo [ i , j ] = 0 ; } } memo [ 3 , 2 ] = memo [ 4 , 2 ] = 1 ; for ( int i = 5 ; i <= N ; i ++ ) { for ( int j = 2 ; j <= i ; j ++ ) { if ( j == 2 ) { memo [ i , j ] = memo [ i - j , j ] + 1 ; } else { memo [ i , j ] = memo [ i - j , j ] + memo [ i - j , j - 1 ] ; } } } int answer = 0 ; for ( int i = 1 ; i <= N ; i ++ ) answer = answer + memo [ N , i ] ; return answer ; } public static void Main ( ) { int N = 7 ; Console . WriteLine ( countStaircases ( N ) ) ; } }
Find maximum points which can be obtained by deleting elements from array | C # program to find maximum cost after deleting all the elements form the array ; function to return maximum cost obtained ; find maximum element of the array . ; initialize count of all elements to zero . ; calculate frequency of all elements of array . ; stores cost of deleted elements . ; selecting minimum range from L and R . ; finds upto which elements are to be deleted when element num is selected . ; get maximum when selecting element num or not . ; Driver Code ; size of array ; function call to find maximum cost
using System ; class GFG { static int maxCost ( int [ ] a , int n , int l , int r ) { int mx = 0 , k ; for ( int i = 0 ; i < n ; ++ i ) mx = Math . Max ( mx , a [ i ] ) ; int [ ] count = new int [ mx + 1 ] ; for ( int i = 0 ; i < count . Length ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int [ ] res = new int [ mx + 1 ] ; res [ 0 ] = 0 ; l = Math . Min ( l , r ) ; for ( int num = 1 ; num <= mx ; num ++ ) { k = Math . Max ( num - l - 1 , 0 ) ; res [ num ] = Math . Max ( res [ num - 1 ] , num * count [ num ] + res [ k ] ) ; } return res [ mx ] ; } public static void Main ( ) { int [ ] a = { 2 , 1 , 2 , 3 , 2 , 2 , 1 } ; int l = 1 , r = 1 ; int n = a . Length ; Console . WriteLine ( maxCost ( a , n , l , r ) ) ; } }
Count the number of ways to traverse a Matrix | C # program using recursive solution to count number of ways to reach mat [ m - 1 ] [ n - 1 ] from mat [ 0 ] [ 0 ] in a matrix mat [ ] [ ] ; Returns The number of way from top - left to mat [ m - 1 ] [ n - 1 ] ; Return 1 if it is the first row or first column ; Recursively find the no of way to reach the last cell . ; Driver Code
using System ; class GFG { public int countPaths ( int m , int n ) { if ( m == 1 n == 1 ) return 1 ; return countPaths ( m - 1 , n ) + countPaths ( m , n - 1 ) ; } public static void Main ( ) { GFG g = new GFG ( ) ; int n = 5 , m = 5 ; Console . WriteLine ( g . countPaths ( n , m ) ) ; Console . Read ( ) ; } }
Count the number of ways to traverse a Matrix | A simple recursive solution to count number of ways to reach mat [ m - 1 ] [ n - 1 ] from mat [ 0 ] [ 0 ] in a matrix mat [ ] [ ] ; Returns The number of way from top - left to mat [ m - 1 ] [ n - 1 ] ; Driver code
using System ; class GFG { static int countPaths ( int m , int n ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i == 1 j == 1 ) dp [ i , j ] = 1 ; else dp [ i , j ] = dp [ i - 1 , j ] + dp [ i , j - 1 ] ; } } return dp [ m , n ] ; } public static void Main ( ) { int n = 5 ; int m = 5 ; Console . WriteLine ( countPaths ( n , m ) ) ; } }
Number of ways a convex polygon of n + 2 sides can split into triangles by connecting vertices | C # program to find the nth catalan number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
using System ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( catalan ( n ) ) ; } }
Alternate Fibonacci Numbers | Alternate Fibonacci Series using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Driver Code
using System ; class GFG { static void alternateFib ( int n ) { if ( n < 0 ) return ; int f1 = 0 ; int f2 = 1 ; Console . Write ( f1 + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { int f3 = f2 + f1 ; if ( i % 2 == 0 ) Console . Write ( f3 + " ▁ " ) ; f1 = f2 ; f2 = f3 ; } } public static void Main ( ) { int N = 15 ; alternateFib ( N ) ; } }
Number of ways to form an array with distinct adjacent elements | C # program to count the number of ways to form arrays of N numbers such that the first and last numbers are fixed and all consecutive numbers are distinct ; Returns the total ways to form arrays such that every consecutive element is different and each element except the first and last can take values from 1 to M ; define the dp [ ] [ ] array ; if the first element is 1 ; there is only one way to place a 1 at the first index ; the value at first index needs to be 1 , thus there is no way to place a non - one integer ; if the first element was 1 then at index 1 , only non one integer can be placed thus there are M - 1 ways to place a non one integer at index 2 and 0 ways to place a 1 at the 2 nd index ; Else there is one way to place a one at index 2 and if a non one needs to be placed here , there are ( M - 2 ) options , i . e neither the element at this index should be 1 , neither should it be equal to the previous element ; Build the dp array in bottom up manner ; f ( i , one ) = f ( i - 1 , non - one ) ; f ( i , non - one ) = f ( i - 1 , one ) * ( M - 1 ) + f ( i - 1 , non - one ) * ( M - 2 ) ; last element needs to be one , so return dp [ n - 1 ] [ 0 ] ; Driver Code
using System ; class GFG { static int totalWays ( int N , int M , int X ) { int [ , ] dp = new int [ N + 1 , 2 ] ; if ( X == 1 ) { dp [ 0 , 0 ] = 1 ; } else { dp [ 0 , 1 ] = 0 ; } if ( X == 1 ) { dp [ 1 , 0 ] = 0 ; dp [ 1 , 1 ] = M - 1 ; } else { dp [ 1 , 0 ] = 1 ; dp [ 1 , 1 ] = ( M - 2 ) ; } for ( int i = 2 ; i < N ; i ++ ) { dp [ i , 0 ] = dp [ i - 1 , 1 ] ; dp [ i , 1 ] = dp [ i - 1 , 0 ] * ( M - 1 ) + dp [ i - 1 , 1 ] * ( M - 2 ) ; } return dp [ N - 1 , 0 ] ; } public static void Main ( ) { int N = 4 , M = 3 , X = 2 ; Console . WriteLine ( totalWays ( N , M , X ) ) ; } }
Memoization ( 1D , 2D and 3D ) | C # program to find the Nth term of Fibonacci series ; Fibonacci Series using Recursion ; Base case ; recursive calls ; Driver Code
using System ; class GFG { static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } static public void Main ( ) { int n = 6 ; Console . WriteLine ( fib ( n ) ) ; } }
Memoization ( 1D , 2D and 3D ) | C # program to find the Nth term of Fibonacci series ; Fibonacci Series using memoized Recursion ; base case ; if fib ( n ) has already been computed we do not do further recursive calls and hence reduce the number of repeated work ; store the computed value of fib ( n ) in an array term at index n to so that it does not needs to be precomputed again ; Driver Code
using System ; class GFG { static int fib ( int n ) { int [ ] term = new int [ 1000 ] ; if ( n <= 1 ) return n ; if ( term [ n ] != 0 ) return term [ n ] ; else { term [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ; return term [ n ] ; } } public static void Main ( ) { int n = 6 ; Console . Write ( fib ( n ) ) ; } }
Memoization ( 1D , 2D and 3D ) | A Naive recursive implementation of LCS problem ; Utility function to get max of 2 integers ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code
using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int lcs ( string X , string Y , int m , int n ) { if ( m == 0 n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } public static void Main ( ) { string X = " AGGTAB " ; string Y = " GXTXAYB " ; int m = X . Length ; int n = Y . Length ; Console . Write ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , m , n ) ) ; } }
Smallest number with given sum of digits and sum of square of digits | C # program to find the Smallest number with given sum of digits and sum of square of digits ; Top down dp to find minimum number of digits with given sum of dits a and sum of square of digits as b ; Invalid condition ; Number of digits satisfied ; Memoization ; Initialize ans as maximum as we have to find the minimum number of digits ; Check for all possible combinations of digits ; recurrence call ; If the combination of digits cannot give sum as a and sum of square of digits as b ; Returns the minimum number of digits ; Function to print the digits that gives sum as a and sum of square of digits as b ; initialize the dp array as - 1 ; base condition ; function call to get the minimum number of digits ; When there does not exists any number ; Printing the digits from the most significant digit ; Trying all combinations ; checking conditions for minimum digits ; Driver Code ; Function call to print the smallest number
using System ; public class GFG { static int [ , ] dp = new int [ 901 , 8101 ] ; static int minimumNumberOfDigits ( int a , int b ) { if ( a > b a < 0 b < 0 a > 900 b > 8100 ) { return - 1 ; } if ( a == 0 && b == 0 ) { return 0 ; } if ( dp [ a , b ] != - 1 ) { return dp [ a , b ] ; } int ans = 101 ; for ( int i = 9 ; i >= 1 ; i -- ) { int k = minimumNumberOfDigits ( a - i , b - ( i * i ) ) ; if ( k != - 1 ) { ans = Math . Min ( ans , k + 1 ) ; } } return dp [ a , b ] = ans ; } static void printSmallestNumber ( int a , int b ) { for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) dp [ i , j ] = - 1 ; dp [ 0 , 0 ] = 0 ; int k = minimumNumberOfDigits ( a , b ) ; if ( k == - 1 k > 100 ) { Console . WriteLine ( " - 1" ) ; } else { while ( a > 0 && b > 0 ) { for ( int i = 1 ; i <= 9 ; i ++ ) { if ( a >= i && b >= i * i && 1 + dp [ a - i , b - i * i ] == dp [ a , b ] ) { Console . Write ( i ) ; a -= i ; b -= i * i ; break ; } } } } } public static void Main ( ) { int a = 18 , b = 162 ; printSmallestNumber ( a , b ) ; } }
Sum of product of consecutive Binomial Coefficients | C # Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient up to nth term ; memset ( C , 0 , sizeof ( C ) ) ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; Driver Code
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static int sumOfproduct ( int n ) { return binomialCoeff ( 2 * n , n - 1 ) ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( sumOfproduct ( n ) ) ; } }
Check if array sum can be made K by three operations on it | C # Program to find if Array can have a sum of K by applying three types of possible ; Check if it is possible to achieve a sum with three operation allowed . ; If sum is negative . ; If going out of bound . ; If sum is achieved . ; If the current state is not evaluated yet . ; Replacing element with negative value of the element . ; Substracting index number from the element . ; Adding index number to the element . ; Wrapper Function ; Driver Code
using System ; class GFG { static int MAX = 100 ; static int check ( int i , int sum , int n , int k , int [ ] a , int [ , ] dp ) { if ( sum <= 0 ) { return 0 ; } if ( i >= n ) { if ( sum == k ) { return 1 ; } return 0 ; } if ( dp [ i , sum ] != - 1 ) { return dp [ i , sum ] ; } dp [ i , sum ] = check ( i + 1 , sum - 2 * a [ i ] , n , k , a , dp ) | check ( i + 1 , sum , n , k , a , dp ) ; dp [ i , sum ] = check ( i + 1 , sum - ( i + 1 ) , n , k , a , dp ) | dp [ i , sum ] ; dp [ i , sum ] = check ( i + 1 , sum + i + 1 , n , k , a , dp ) | dp [ i , sum ] ; return dp [ i , sum ] ; } static int wrapper ( int n , int k , int [ ] a ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = - 1 ; } } return check ( 0 , sum , n , k , a , dp ) ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 5 ; if ( wrapper ( n , k , a ) == 1 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Print Fibonacci sequence using 2 variables | Simple C # Program to print Fibonacci sequence ; Driver code
using System ; class GFG { static void fib ( int n ) { int a = 0 , b = 1 , c ; if ( n >= 0 ) Console . Write ( a + " ▁ " ) ; if ( n >= 1 ) Console . Write ( b + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { c = a + b ; Console . Write ( c + " ▁ " ) ; a = b ; b = c ; } } public static void Main ( ) { fib ( 9 ) ; } }
Maximum sum increasing subsequence from a prefix and a given element after prefix is must | C # program to find maximum sum increasing subsequence till i - th index and including k - th index . ; Initializing the first row of the dp [ ] [ ] . ; Creating the dp [ ] [ ] matrix . ; To calculate for i = 4 and k = 6. ; Driver code
using System ; class GFG { static int pre_compute ( int [ ] a , int n , int index , int k ) { int [ , ] dp = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > a [ 0 ] ) dp [ 0 , i ] = a [ i ] + a [ 0 ] ; else dp [ 0 , i ] = a [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( a [ j ] > a [ i ] && j > i ) { if ( dp [ i - 1 , i ] + a [ j ] > dp [ i - 1 , j ] ) dp [ i , j ] = dp [ i - 1 , i ] + a [ j ] ; else dp [ i , j ] = dp [ i - 1 , j ] ; } else dp [ i , j ] = dp [ i - 1 , j ] ; } } return dp [ index , k ] ; } static public void Main ( ) { int [ ] a = { 1 , 101 , 2 , 3 , 100 , 4 , 5 } ; int n = a . Length ; int index = 4 , k = 6 ; Console . WriteLine ( pre_compute ( a , n , index , k ) ) ; } }
Moser | C # code to generate first ' n ' terms of the Moser - de Bruijn Sequence ; Function to generate nth term of Moser - de Bruijn Sequence ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code
using System ; class GFG { static int gen ( int n ) { int [ ] S = new int [ n + 1 ] ; S [ 0 ] = 0 ; if ( n != 0 ) S [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) S [ i ] = 4 * S [ i / 2 ] ; else S [ i ] = 4 * S [ i / 2 ] + 1 ; } return S [ n ] ; } static void moserDeBruijn ( int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( gen ( i ) + " ▁ " ) ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( " First ▁ " + n + " ▁ terms ▁ of ▁ " + " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ " ) ; moserDeBruijn ( n ) ; } }