codeparrot/xlcost-text-to-code huggingface dataset:
Count binary strings with k times appearing adjacent two set bits | C # program to count number of binary strings with k times appearing consecutive 1 's. ; dp [ i ] [ j ] [ 0 ] stores count of binary strings of length i with j consecutive 1 ' s ▁ and ▁ ending ▁ at ▁ 0 . ▁ ▁ dp [ i ] [ j ] [1 ] ▁ stores ▁ count ▁ of ▁ binary ▁ ▁ strings ▁ of ▁ length ▁ i ▁ with ▁ j ▁ consecutive ▁ ▁ 1' s and ending at 1. ; If n = 1 and k = 0. ; number of adjacent 1 's can not exceed i-1 ; Driver code
using System ; class GFG { static int countStrings ( int n , int k ) { int [ , , ] dp = new int [ n + 1 , k + 1 , 2 ] ; dp [ 1 , 0 , 0 ] = 1 ; dp [ 1 , 0 , 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i && j < k + 1 ; j ++ ) { dp [ i , j , 0 ] = dp [ i - 1 , j , 0 ] + dp [ i - 1 , j , 1 ] ; dp [ i , j , 1 ] = dp [ i - 1 , j , 0 ] ; if ( j - 1 >= 0 ) { dp [ i , j , 1 ] += dp [ i - 1 , j - 1 , 1 ] ; } } } return dp [ n , k , 0 ] + dp [ n , k , 1 ] ; } public static void Main ( String [ ] args ) { int n = 5 , k = 2 ; Console . WriteLine ( countStrings ( n , k ) ) ; } }
Friends Pairing Problem | C # program for solution of friends pairing problem Using Recursion ; Returns count of ways n people can remain single or paired up . ; Driver code
using System ; class GFG { static int [ ] dp = new int [ 1000 ] ; static int countFriendsPairings ( int n ) { if ( dp [ n ] != - 1 ) return dp [ n ] ; if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ; else return dp [ n ] = n ; } static void Main ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) dp [ i ] = - 1 ; int n = 4 ; Console . Write ( countFriendsPairings ( n ) ) ; } }
Minimum Sum Path In 3 | C # program for Min path sum of 3D - array ; A utility function that returns minimum of 3 integers ; function to calculate MIN path sum of 3D array ; Initialize first row of tSum array ; Initialize first column of tSum array ; Initialize first width of tSum array ; Initialize first row - First column of tSum array ; Initialize first row - First width of tSum array ; Initialize first width - First column of tSum array ; Construct rest of the tSum array ; Driver Code
using System ; class GFG { static int l = 3 ; static int m = 3 ; static int n = 3 ; static int min ( int x , int y , int z ) { return ( x < y ) ? ( ( x < z ) ? x : z ) : ( ( y < z ) ? y : z ) ; } static int minPathSum ( int [ , , ] arr ) { int i , j , k ; int [ , , ] tSum = new int [ l , m , n ] ; tSum [ 0 , 0 , 0 ] = arr [ 0 , 0 , 0 ] ; for ( i = 1 ; i < l ; i ++ ) tSum [ i , 0 , 0 ] = tSum [ i - 1 , 0 , 0 ] + arr [ i , 0 , 0 ] ; for ( j = 1 ; j < m ; j ++ ) tSum [ 0 , j , 0 ] = tSum [ 0 , j - 1 , 0 ] + arr [ 0 , j , 0 ] ; for ( k = 1 ; k < n ; k ++ ) tSum [ 0 , 0 , k ] = tSum [ 0 , 0 , k - 1 ] + arr [ 0 , 0 , k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ i , j , 0 ] = min ( tSum [ i - 1 , j , 0 ] , tSum [ i , j - 1 , 0 ] , int . MaxValue ) + arr [ i , j , 0 ] ; for ( i = 1 ; i < l ; i ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i , 0 , k ] = min ( tSum [ i - 1 , 0 , k ] , tSum [ i , 0 , k - 1 ] , int . MaxValue ) + arr [ i , 0 , k ] ; for ( k = 1 ; k < n ; k ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ 0 , j , k ] = min ( tSum [ 0 , j , k - 1 ] , tSum [ 0 , j - 1 , k ] , int . MaxValue ) + arr [ 0 , j , k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i , j , k ] = min ( tSum [ i - 1 , j , k ] , tSum [ i , j - 1 , k ] , tSum [ i , j , k - 1 ] ) + arr [ i , j , k ] ; return tSum [ l - 1 , m - 1 , n - 1 ] ; } static public void Main ( ) { int [ , , ] arr = { { { 1 , 2 , 4 } , { 3 , 4 , 5 } , { 5 , 2 , 1 } } , { { 4 , 8 , 3 } , { 5 , 2 , 1 } , { 3 , 4 , 2 } } , { { 2 , 4 , 1 } , { 3 , 1 , 4 } , { 6 , 3 , 8 } } } ; Console . WriteLine ( minPathSum ( arr ) ) ; } }
Maximum path sum in a triangle . | C # Program for Dynamic Programming implementation of Max sum problem in a triangle ; Function for finding maximum sum ; loop for bottom - up calculation ; for each element , check both elements just below the number and below right to the number add the maximum of them to it ; return the top element which stores the maximum sum ; Driver program to test above functions
using System ; class GFG { static int maxPathSum ( int [ , ] tri , int m , int n ) { for ( int i = m - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( tri [ i + 1 , j ] > tri [ i + 1 , j + 1 ] ) tri [ i , j ] += tri [ i + 1 , j ] ; else tri [ i , j ] += tri [ i + 1 , j + 1 ] ; } } return tri [ 0 , 0 ] ; } public static void Main ( ) { int [ , ] tri = { { 1 , 0 , 0 } , { 4 , 8 , 0 } , { 1 , 5 , 3 } } ; Console . Write ( maxPathSum ( tri , 2 , 2 ) ) ; } }
Find Maximum dot product of two arrays with insertion of 0 's | C # program to find maximum dot product of two array ; Function to compute Maximum Dot Product and return it ; Create 2D Matrix that stores dot product dp [ i + 1 ] [ j + 1 ] stores product considering B [ 0. . i ] and A [ 0. . . j ] . Note that since all m > n , we fill values in upper diagonal of dp [ ] [ ] ; Traverse through all elements of B [ ] ; Consider all values of A [ ] with indexes greater than or equal to i and compute dp [ i ] [ j ] ; Two cases arise 1 ) Include A [ j ] 2 ) Exclude A [ j ] ( insert 0 in B [ ] ) ; return Maximum Dot Product ; Driver code
using System ; public class GFG { static int MaxDotProduct ( int [ ] A , int [ ] B , int m , int n ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= m ; j ++ ) dp [ i , j ] = Math . Max ( ( dp [ i - 1 , j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i , j - 1 ] ) ; return dp [ n , m ] ; } public static void Main ( ) { int [ ] A = { 2 , 3 , 1 , 7 , 8 } ; int [ ] B = { 3 , 6 , 7 } ; int m = A . Length ; int n = B . Length ; Console . Write ( MaxDotProduct ( A , B , m , n ) ) ; } }
LCS ( Longest Common Subsequence ) of three strings | C # program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Driver code
using System ; class GFG { static string X = " AGGT12" ; static string Y = "12TXAYB " ; static string Z = "12XBA " ; static int [ , , ] dp = new int [ 100 , 100 , 100 ] ; static int lcsOf3 ( int i , int j , int k ) { if ( i == - 1 j == - 1 k == - 1 ) return 0 ; if ( dp [ i , j , k ] != - 1 ) return dp [ i , j , k ] ; if ( X [ i ] == Y [ j ] && Y [ j ] == Z [ k ] ) return dp [ i , j , k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) ; else return dp [ i , j , k ] = Math . Max ( Math . Max ( lcsOf3 ( i - 1 , j , k ) , lcsOf3 ( i , j - 1 , k ) ) , lcsOf3 ( i , j , k - 1 ) ) ; } static void Main ( ) { 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 ; int m = X . Length ; int n = Y . Length ; int o = Z . Length ; Console . Write ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( m - 1 , n - 1 , o - 1 ) ) ; } }
Minimum number of elements which are not part of Increasing or decreasing subsequence in array | C # program to return minimum number of elements which are not part of increasing or decreasing subsequences . ; Return minimum number of elements which is not part of any of the sequence . ; If already calculated , return value . ; If whole array is traversed . ; calculating by considering element as part of decreasing sequence . ; calculating by considering element as part of increasing sequence . ; If cannot be calculated for decreasing sequence . ; After considering once by decreasing sequence , now try for increasing sequence . ; If element cannot be part of any of the sequence . ; After considering element as part of increasing and decreasing sequence trying as not part of any of the sequence . ; Wrapper Function ; Adding two number at the end of array , so that increasing and decreasing sequence can be made . MAX - 2 index is assigned INT_MAX for decreasing sequence because next number of sequence must be less than it . Similarly , for Increasing sequence INT_MIN is assigned to MAX - 1 index . ; Driver Code
using System ; class GFG { static int MAX = 102 ; static int countMin ( int [ ] arr , int [ , , ] dp , int n , int dec , int inc , int i ) { if ( dp [ dec , inc , i ] != - 1 ) return dp [ dec , inc , i ] ; if ( i == n ) return 0 ; if ( arr [ i ] < arr [ dec ] ) dp [ dec , inc , i ] = countMin ( arr , dp , n , i , inc , i + 1 ) ; if ( arr [ i ] > arr [ inc ] ) { if ( dp [ dec , inc , i ] == - 1 ) dp [ dec , inc , i ] = countMin ( arr , dp , n , dec , i , i + 1 ) ; else dp [ dec , inc , i ] = Math . Min ( countMin ( arr , dp , n , dec , i , i + 1 ) , dp [ dec , inc , i ] ) ; } if ( dp [ dec , inc , i ] == - 1 ) dp [ dec , inc , i ] = 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) ; else dp [ dec , inc , i ] = Math . Min ( 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) , dp [ dec , inc , i ] ) ; return dp [ dec , inc , i ] ; } static int wrapper ( int [ ] arr , int n ) { arr [ MAX - 2 ] = int . MaxValue ; arr [ MAX - 1 ] = int . MinValue ; int [ , , ] dp = new int [ MAX , MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k = 0 ; k < MAX ; k ++ ) dp [ i , j , k ] = - 1 ; return countMin ( arr , dp , n , MAX - 2 , MAX - 1 , 0 ) ; } static void Main ( ) { int n = 12 ; int [ ] arr = new int [ MAX ] ; arr [ 0 ] = 7 ; arr [ 1 ] = 8 ; arr [ 2 ] = 1 ; arr [ 3 ] = 2 ; arr [ 4 ] = 4 ; arr [ 5 ] = 6 ; arr [ 6 ] = 3 ; arr [ 7 ] = 5 ; arr [ 8 ] = 2 ; arr [ 9 ] = 1 ; arr [ 10 ] = 8 ; arr [ 11 ] = 7 ; Console . Write ( wrapper ( arr , n ) ) ; } }
Find all distinct subset ( or subsequence ) sums of an array | C # program to print distinct subset sums of a given array . ; sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; This function mainly calls recursive function distSumRec ( ) to generate distinct sum subsets . And finally prints the generated subsets . ; Print the result ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void distSumRec ( int [ ] arr , int n , int sum , int currindex , HashSet < int > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . Add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; foreach ( int i in s ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistSum ( arr , n ) ; } }
Find all distinct subset ( or subsequence ) sums of an array | C # program to print distinct subset sums of a given array . ; Uses Dynamic Programming to find distinct subset sums ; dp [ i ] [ j ] would be true if arr [ 0. . i - 1 ] has a subset with sum equal to j . ; There is always a subset with 0 sum ; Fill dp [ ] [ ] in bottom up manner ; Sums that were achievable without current array element ; Print last row elements ; Driver code
using System ; class GFG { static void printDistSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; bool [ , ] dp = new bool [ n + 1 , sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i , arr [ i - 1 ] ] = true ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 , j ] == true ) { dp [ i , j ] = true ; dp [ i , j + arr [ i - 1 ] ] = true ; } } } for ( int j = 0 ; j <= sum ; j ++ ) if ( dp [ n , j ] == true ) Console . Write ( j + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistSum ( arr , n ) ; } }
Count number of ways to reach destination in a Maze | C # program to count number of paths in a maze with obstacles . ; Returns count of possible paths in a maze [ R ] [ C ] from ( 0 , 0 ) to ( R - 1 , C - 1 ) ; If the initial cell is blocked , there is no way of moving anywhere ; Initializing the leftmost column ; If we encounter a blocked cell in leftmost row , there is no way of visiting any cell directly below it . ; Similarly initialize the topmost row ; If we encounter a blocked cell in bottommost row , there is no way of visiting any cell directly below it . ; The only difference is that if a cell is - 1 , simply ignore it else recursively compute count value maze [ i ] [ j ] ; If blockage is found , ignore this cell ; If we can reach maze [ i ] [ j ] from maze [ i - 1 ] [ j ] then increment count . ; If we can reach maze [ i ] [ j ] from maze [ i ] [ j - 1 ] then increment count . ; If the final cell is blocked , output 0 , otherwise the answer ; Driver code
using System ; class GFG { static int R = 4 ; static int C = 4 ; static int countPaths ( int [ , ] maze ) { if ( maze [ 0 , 0 ] == - 1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i , 0 ] == 0 ) maze [ i , 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 , i ] == 0 ) maze [ 0 , i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i , j ] == - 1 ) continue ; if ( maze [ i - 1 , j ] > 0 ) maze [ i , j ] = ( maze [ i , j ] + maze [ i - 1 , j ] ) ; if ( maze [ i , j - 1 ] > 0 ) maze [ i , j ] = ( maze [ i , j ] + maze [ i , j - 1 ] ) ; } } return ( maze [ R - 1 , C - 1 ] > 0 ) ? maze [ R - 1 , C - 1 ] : 0 ; } public static void Main ( ) { int [ , ] maze = { { 0 , 0 , 0 , 0 } , { 0 , - 1 , 0 , 0 } , { - 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; Console . Write ( countPaths ( maze ) ) ; } }
Minimum sum subsequence such that at least one of every four consecutive elements is picked | C # program to find minimum sum subsequence of an array such that one of every four consecutive elements is picked . ; Returns sum of minimum sum subsequence such that one of every four consecutive elements is picked from arr [ ] . ; dp [ i ] is going to store minimum sum subsequence of arr [ 0. . i ] such that arr [ i ] is part of the solution . Note that this may not be the best solution for subarray arr [ 0. . i ] ; If there is single value , we get the minimum sum equal to arr [ 0 ] ; If there are two values , we get the minimum sum equal to the minimum of two values ; If there are three values , return minimum of the three elements of array ; If there are four values , return minimum of the four elements of array ; Return the minimum of last 4 index ; Driver code
using System ; class GFG { static int minSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return Math . Min ( arr [ 0 ] , arr [ 1 ] ) ; if ( n == 3 ) return Math . Min ( arr [ 0 ] , Math . Min ( arr [ 1 ] , arr [ 2 ] ) ) ; if ( n == 4 ) return Math . Min ( Math . Min ( arr [ 0 ] , arr [ 1 ] ) , Math . Min ( arr [ 2 ] , arr [ 3 ] ) ) ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = arr [ 1 ] ; dp [ 2 ] = arr [ 2 ] ; dp [ 3 ] = arr [ 3 ] ; for ( int i = 4 ; i < n ; i ++ ) dp [ i ] = arr [ i ] + Math . Min ( Math . Min ( dp [ i - 1 ] , dp [ i - 2 ] ) , Math . Min ( dp [ i - 3 ] , dp [ i - 4 ] ) ) ; return Math . Min ( Math . Min ( dp [ n - 1 ] , dp [ n - 2 ] ) , Math . Min ( dp [ n - 4 ] , dp [ n - 3 ] ) ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 1 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }
Maximum decimal value path in a binary matrix | C # program to find maximum decimal value path in binary matrix ; Returns maximum decimal value in binary matrix . Here p indicate power of 2 ; Out of matrix boundary ; If current matrix value is 1 then return result + power ( 2 , p ) else result ; Driver program
using System ; class GFG { static int N = 4 ; static int maxDecimalValue ( int [ , ] mat , int i , int j , int p ) { if ( i >= N j >= N ) { return 0 ; } int result = Math . Max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i , j ] == 1 ) { return ( int ) ( Math . Pow ( 2 , p ) + result ) ; } else { return result ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } , { 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 } , } ; Console . Write ( maxDecimalValue ( mat , 0 , 0 , 0 ) ) ; } }
Maximum decimal value path in a binary matrix | C # program to find Maximum decimal value Path in Binary matrix ; Returns maximum decimal value in binary matrix . Here p indicate power of 2 ; dp [ 0 , 0 ] = 1 ; 1 * ( 2 ^ 0 ) ; Compute binary stream of first row of matrix and store result in dp [ 0 , i ] ; indicate 1 * ( 2 ^ i ) + result of previous ; } indicate 0 * ( 2 ^ i ) + result of previous ; Compute binary stream of first column of matrix and store result in dp [ i , 0 ] ; indicate 1 * ( 2 ^ i ) + result of previous ; } indicate 0 * ( 2 ^ i ) + result of previous ; Traversal rest Binary matrix and Compute maximum decimal value ; Here ( i + j ) indicate the current power of 2 in path that is 2 ^ ( i + j ) ; Return maximum decimal value in binary matrix ; Driver program
using System ; public class GFG { readonly static int N = 4 ; static int MaximumDecimalValue ( int [ , ] mat , int n ) { int [ , ] dp = new int [ n , n ] ; if ( mat [ 0 , 0 ] == 1 ) { } for ( int i = 1 ; i < n ; i ++ ) { if ( mat [ 0 , i ] == 1 ) { dp [ 0 , i ] = ( int ) ( dp [ 0 , i - 1 ] + Math . Pow ( 2 , i ) ) ; else { dp [ 0 , i ] = dp [ 0 , i - 1 ] ; } } for ( int i = 1 ; i < n ; i ++ ) { if ( mat [ i , 0 ] == 1 ) { dp [ i , 0 ] = ( int ) ( dp [ i - 1 , 0 ] + Math . Pow ( 2 , i ) ) ; else { dp [ i , 0 ] = dp [ i - 1 , 0 ] ; } } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { if ( mat [ i , j ] == 1 ) { dp [ i , j ] = ( int ) ( Math . Max ( dp [ i , j - 1 ] , dp [ i - 1 , j ] ) + Math . Pow ( 2 , i + j ) ) ; } else { dp [ i , j ] = Math . Max ( dp [ i , j - 1 ] , dp [ i - 1 , j ] ) ; } } } return dp [ n - 1 , n - 1 ] ; } public static void Main ( ) { int [ , ] mat = { { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } , { 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 } , } ; Console . Write ( MaximumDecimalValue ( mat , 4 ) ) ; } }
Count All Palindrome Sub | C # program to find palindromic substrings of a string ; Returns total number of palindrome substring of length greater then equal to 2 ; create empty 2 - D matrix that counts all palindrome substring . dp [ i ] [ j ] stores counts of palindromic substrings in st [ i . . j ] ; P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill DP table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; Add current palindrome substring ( + 1 ) and rest palindrome substring ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] ) remove common palindrome substrings ( - dp [ i + 1 ] [ j - 1 ] ) ; return total palindromic substrings ; Driver Code
using System ; class GFG { public static int CountPS ( char [ ] str , int n ) { int [ ] [ ] dp = RectangularArrays . ReturnRectangularIntArray ( n , n ) ; bool [ ] [ ] P = RectangularArrays . ReturnRectangularBoolArray ( n , n ) ; for ( int i = 0 ; i < n ; i ++ ) { P [ i ] [ i ] = true ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { P [ i ] [ i + 1 ] = true ; dp [ i ] [ i + 1 ] = 1 ; } } for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str [ i ] == str [ j ] && P [ i + 1 ] [ j - 1 ] ) { P [ i ] [ j ] = true ; } if ( P [ i ] [ j ] == true ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] + 1 - dp [ i + 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] ; } } } return dp [ 0 ] [ n - 1 ] ; } public static class RectangularArrays { public static int [ ] [ ] ReturnRectangularIntArray ( int size1 , int size2 ) { int [ ] [ ] newArray = new int [ size1 ] [ ] ; for ( int array1 = 0 ; array1 < size1 ; array1 ++ ) { newArray [ array1 ] = new int [ size2 ] ; } return newArray ; } public static bool [ ] [ ] ReturnRectangularBoolArray ( int size1 , int size2 ) { bool [ ] [ ] newArray = new bool [ size1 ] [ ] ; for ( int array1 = 0 ; array1 < size1 ; array1 ++ ) { newArray [ array1 ] = new bool [ size2 ] ; } return newArray ; } } public static void Main ( string [ ] args ) { string str = " abaab " ; Console . WriteLine ( CountPS ( str . ToCharArray ( ) , str . Length ) ) ; } }
Maximum subsequence sum such that no three are consecutive | C # program to find the maximum sum such that no three are consecutive ; Returns maximum subsequence sum such that no three elements are consecutive ; Stores result for subarray arr [ 0. . i ] , i . e . , maximum possible sum in subarray arr [ 0. . i ] such that no three elements are consecutive . ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases 1 ) Exclude arr [ i ] , i . e . , sum [ i ] = sum [ i - 1 ] 2 ) Exclude arr [ i - 1 ] , i . e . , sum [ i ] = sum [ i - 2 ] + arr [ i ] 3 ) Exclude arr [ i - 2 ] , i . e . , sum [ i - 3 ] + arr [ i ] + arr [ i - 1 ] ; Driver code
using System ; class GFG { static int maxSumWO3Consec ( int [ ] arr , int n ) { int [ ] sum = new int [ n ] ; if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ; if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ; if ( n > 2 ) sum [ 2 ] = Math . Max ( sum [ 1 ] , Math . Max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = Math . Max ( Math . Max ( sum [ i - 1 ] , sum [ i - 2 ] + arr [ i ] ) , arr [ i ] + arr [ i - 1 ] + sum [ i - 3 ] ) ; return sum [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 100 , 1000 , 100 , 1000 , 1 } ; int n = arr . Length ; Console . Write ( maxSumWO3Consec ( arr , n ) ) ; } }
Maximum sum of pairs with specific difference | C # program to find maximum pair sum whose difference is less than K ; method to return maximum sum we can get by finding less than K difference pair ; Sort input array in ascending order . ; dp [ i ] denotes the maximum disjoint pair sum we can achieve using first i elements ; if no element then dp value will be 0 ; first give previous value to dp [ i ] i . e . no pairing with ( i - 1 ) th element ; if current and previous element can form a pair ; update dp [ i ] by choosing maximum between pairing and not pairing ; last index will have the result ; Driver code to test above methods
using System ; class GFG { static int maxSumPairWithDifferenceLessThanK ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int [ ] dp = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = Math . Max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] + arr [ i - 1 ] ) ; else dp [ i ] = Math . Max ( dp [ i ] , arr [ i ] + arr [ i - 1 ] ) ; } } return dp [ N - 1 ] ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 10 , 15 , 17 , 12 , 9 } ; int N = arr . Length ; int K = 4 ; Console . WriteLine ( maxSumPairWithDifferenceLessThanK ( arr , N , K ) ) ; } }
Lucas Numbers | Recursive C # program to find n 'th Lucas number ; recursive function ; Base cases ; recurrence relation ; Driver program
using System ; class GFG { public static int lucas ( int n ) { if ( n == 0 ) return 2 ; if ( n == 1 ) return 1 ; return lucas ( n - 1 ) + lucas ( n - 2 ) ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( lucas ( n ) ) ; } }
Recursively break a number in 3 parts to get maximum sum | A simple recursive C # program to find maximum sum by recursively breaking a number in 3 parts . ; Function to find the maximum sum ; base conditions ; recursively break the number and return what maximum you can get ; Driver program to test the above function
using System ; class GFG { static int breakSum ( int n ) { if ( n == 0 n == 1 ) return n ; return Math . Max ( ( breakSum ( n / 2 ) + breakSum ( n / 3 ) + breakSum ( n / 4 ) ) , n ) ; } public static void Main ( ) { int n = 12 ; Console . WriteLine ( breakSum ( n ) ) ; } }