codeparrot/xlcost-text-to-code huggingface dataset:
Count All Palindromic Subsequence in a given String | C # code to Count Palindromic Subsequence Subsequence in a given String ; Function return the total palindromic subsequence ; create a 2D array to store the count of palindromic subsequence ; palindromic subsequence of length 1 ; check subsequence of length L is palindrome or not ; return total palindromic subsequence ; Driver Code
using System ; class GFG { static int countPS ( string str ) { int N = str . Length ; int [ , ] cps = new int [ N + 1 , N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) cps [ i , i ] = 1 ; for ( int L = 2 ; L <= N ; L ++ ) { for ( int i = 0 ; i <= N - L ; i ++ ) { int k = L + i - 1 ; if ( k < N ) { if ( str [ i ] == str [ k ] ) cps [ i , k ] = cps [ i , k - 1 ] + cps [ i + 1 , k ] + 1 ; else cps [ i , k ] = cps [ i , k - 1 ] + cps [ i + 1 , k ] - cps [ i + 1 , k - 1 ] ; } } } return cps [ 0 , N - 1 ] ; } public static void Main ( ) { string str = " abcb " ; Console . Write ( " Total ▁ palindromic ▁ " + " subsequence ▁ are ▁ : ▁ " + countPS ( str ) ) ; } }
Count All Palindromic Subsequence in a given String | C # program to counts Palindromic Subsequence in a given String using recursion ; Function return the total palindromic subsequence ; Driver code
using System ; class GFG { static int n ; static int [ , ] dp = new int [ 1000 , 1000 ] ; static string str = " abcb " ; static int countPS ( int i , int j ) { if ( i > j ) return 0 ; if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; if ( i == j ) return dp [ i , j ] = 1 ; else if ( str [ i ] == str [ j ] ) return dp [ i , j ] = countPS ( i + 1 , j ) + countPS ( i , j - 1 ) + 1 ; else return dp [ i , j ] = countPS ( i + 1 , j ) + countPS ( i , j - 1 ) - countPS ( i + 1 , j - 1 ) ; } static void Main ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) for ( int j = 0 ; j < 1000 ; j ++ ) dp [ i , j ] = - 1 ; n = str . Length ; Console . Write ( " Total ▁ palindromic ▁ subsequence " + " are ▁ : ▁ " + countPS ( 0 , n - 1 ) ) ; } }
Count all increasing subsequences | C # program to count increasing subsequences in an array of digits . ; Function To Count all the sub - sequences possible in which digit is greater than all previous digits arr [ ] is array of n digits ; count [ ] array is used to store all sub - sequences possible using that digit count [ ] array covers all the digit from 0 to 9 ; scan each digit in arr [ ] ; count all possible sub - sequences by the digits less than arr [ i ] digit ; store sum of all sub - sequences plus 1 in count [ ] array ; now sum up the all sequences possible in count [ ] array ; Driver program
using System ; class GFG { static int countSub ( int [ ] arr , int n ) { int [ ] count = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - 1 ; j >= 0 ; j -- ) count [ arr [ i ] ] += count [ j ] ; count [ arr [ i ] ] ++ ; } int result = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) result += count [ i ] ; return result ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 4 , 5 , 4 } ; int n = arr . Length ; Console . WriteLine ( countSub ( arr , n ) ) ; } }
Find minimum sum such that one of every three consecutive elements is taken | A Dynamic Programming based C # program to find minimum possible sum of elements of array such that an element out of every three consecutive is picked . ; A utility function to find minimum of 3 elements ; Returns minimum possible sum of elements such that an element out of every three consecutive elements is picked . ; Create a DP table to store results of subproblems . sum [ i ] is going to store minimum possible sum when arr [ i ] is part of the solution . ; When there are less than or equal to 3 elements ; Iterate through all other elements ; Driver code
using System ; class GFG { static int minimum ( int a , int b , int c ) { return Math . Min ( Math . Min ( a , b ) , c ) ; } static int findMinSum ( int [ ] arr , int n ) { int [ ] sum = new int [ n ] ; sum [ 0 ] = arr [ 0 ] ; sum [ 1 ] = arr [ 1 ] ; sum [ 2 ] = arr [ 2 ] ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = arr [ i ] + minimum ( sum [ i - 3 ] , sum [ i - 2 ] , sum [ i - 1 ] ) ; return minimum ( sum [ n - 1 ] , sum [ n - 2 ] , sum [ n - 3 ] ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 20 , 2 , 10 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Min ▁ Sum ▁ is ▁ " + findMinSum ( arr , n ) ) ; } }
Count Distinct Subsequences | C # Program for above approach ; Returns count of distinct subsequences of str . ; Iterate from 0 to s . length ( ) ; Iterate from 0 to s . length ( ) ; Check if i equal to 0 ; Replace levelCount withe allCount + 1 ; If map is less than 0 ; Return answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int countSub ( String s ) { Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! map . ContainsKey ( s [ i ] ) ) { map . Add ( s [ i ] , - 1 ) ; } } int allCount = 0 ; int levelCount = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { char c = s [ i ] ; if ( i == 0 ) { allCount = 1 ; if ( ! map . ContainsKey ( c ) ) { map . Add ( c , 1 ) ; } else { map = 1 ; } levelCount = 1 ; continue ; } levelCount = allCount + 1 ; if ( map . ContainsKey ( c ) ) { if ( map < 0 ) { allCount = ( allCount + levelCount ) ; } else { allCount = ( allCount + levelCount - map ) ; } } if ( ! map . ContainsKey ( c ) ) { map . Add ( c , levelCount ) ; } else { map = levelCount ; } } return allCount ; } static void Main ( ) { List < string > list = new List < string > ( ) ; list . Add ( " abab " ) ; list . Add ( " gfg " ) ; foreach ( string s in list ) { int cnt = countSub ( s ) ; int withEmptyString = cnt + 1 ; Console . WriteLine ( " With ▁ empty ▁ string ▁ count ▁ for ▁ " + s + " ▁ is ▁ " + withEmptyString ) ; Console . WriteLine ( " Without ▁ empty ▁ string ▁ count ▁ for ▁ " + s + " ▁ is ▁ " + cnt ) ; } } }
Minimum cost to fill given weight in a bag | C # Code for Minimum cost to fill given weight in a bag ; cost [ ] initial cost array including unavailable packet W capacity of bag ; val [ ] and wt [ ] arrays val [ ] array to store cost of ' i ' kg packet of orange wt [ ] array weight of packet of orange ; traverse the original cost [ ] array and skip unavailable packets and make val [ ] and wt [ ] array . size variable tells the available number of distinct weighted packets ; fill 0 th row with infinity ; fill 0 'th column with 0 ; now check for each weight one by one and fill the matrix according to the condition ; wt [ i - 1 ] > j means capacity of bag is less then weight of item ; here we check we get minimum cost either by including it or excluding it ; exactly weight W can not be made by given weights ; Driver program to test above function
using System ; using System . Collections . Generic ; class GFG { public static int MinimumCost ( int [ ] cost , int n , int W ) { List < int > val = new List < int > ( ) ; List < int > wt = new List < int > ( ) ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( cost [ i ] != - 1 ) { val . Add ( cost [ i ] ) ; wt . Add ( i + 1 ) ; size ++ ; } } n = size ; int [ , ] min_cost = new int [ n + 1 , W + 1 ] ; for ( int i = 0 ; i <= W ; i ++ ) min_cost [ 0 , i ] = int . MaxValue ; for ( int i = 1 ; i <= n ; i ++ ) min_cost [ i , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= W ; j ++ ) { if ( wt [ i - 1 ] > j ) min_cost [ i , j ] = min_cost [ i - 1 , j ] ; else min_cost [ i , j ] = Math . Min ( min_cost [ i - 1 , j ] , min_cost [ i , j - wt [ i - 1 ] ] + val [ i - 1 ] ) ; } } return ( min_cost [ n , W ] == int . MaxValue ) ? - 1 : min_cost [ n , W ] ; } public static void Main ( ) { int [ ] cost = { 1 , 2 , 3 , 4 , 5 } ; int W = 5 ; int n = cost . Length ; Console . WriteLine ( MinimumCost ( cost , n , W ) ) ; } }
Find number of times a string occurs as a subsequence in given string | A Naive recursive C # program to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; Recursive function to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; If both first and second string is empty , or if second string is empty , return 1 ; If only first string is empty and second string is not empty , return 0 ; If last characters are same Recur for remaining strings by 1. considering last characters of both strings 2. ignoring last character of first string ; If last characters are different , ignore last char of first string and recur for remaining string ; Driver code
using System ; class GFG { static int count ( string a , string b , int m , int n ) { if ( ( m == 0 && n == 0 ) n == 0 ) return 1 ; if ( m == 0 ) return 0 ; if ( a [ m - 1 ] == b [ n - 1 ] ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ; else return count ( a , b , m - 1 , n ) ; } public static void Main ( ) { string a = " GeeksforGeeks " ; string b = " Gks " ; Console . Write ( count ( a , b , a . Length , b . Length ) ) ; } }
Minimum Cost To Make Two Strings Identical | C # code to find minimum cost to make two strings identical ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; Returns cost of making X [ ] and Y [ ] identical . costX is cost of removing a character from X [ ] and costY is cost of removing a character from Y [ ] ; Find LCS of X [ ] and Y [ ] ; Cost of making two strings identical is SUM of following two 1 ) Cost of removing extra characters from first string 2 ) Cost of removing extra characters from second string ; Driver code
using System ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ m , n ] ; } static int findMinCost ( String X , String Y , int costX , int costY ) { int m = X . Length ; int n = Y . Length ; int len_LCS ; len_LCS = lcs ( X , Y , m , n ) ; return costX * ( m - len_LCS ) + costY * ( n - len_LCS ) ; } public static void Main ( ) { String X = " ef " ; String Y = " gh " ; Console . Write ( " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings ▁ " + " ▁ identical ▁ is ▁ = ▁ " + findMinCost ( X , Y , 10 , 20 ) ) ; } }
Printing Longest Common Subsequence | Set 2 ( Printing All ) | Dynamic Programming implementation of LCS problem ; Maximum String length ; Returns set containing all LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; construct a set to store possible LCS ; If we reaches end of either String , return a empty set ; If the last characters of X and Y are same ; recurse for X [ 0. . m - 2 ] and Y [ 0. . n - 2 ] in the matrix ; append current character to all possible LCS of subString X [ 0. . m - 2 ] and Y [ 0. . n - 2 ] . ; If the last characters of X and Y are not same ; If LCS can be constructed from top side of the matrix , recurse for X [ 0. . m - 2 ] and Y [ 0. . n - 1 ] ; If LCS can be constructed from left side of the matrix , recurse for X [ 0. . m - 1 ] and Y [ 0. . n - 2 ] ; merge two sets if L [ m - 1 , n ] == L [ m , n - 1 ] Note s will be empty if L [ m - 1 , n ] != L [ m , n - 1 ] ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Build L [ m + 1 , n + 1 ] in bottom up fashion ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int N = 100 ; static int [ , ] L = new int [ N , N ] ; static HashSet < String > findLCS ( String X , String Y , int m , int n ) { HashSet < String > s = new HashSet < String > ( ) ; if ( m == 0 n == 0 ) { s . Add ( " " ) ; return s ; } if ( X [ m - 1 ] == Y [ n - 1 ] ) { HashSet < String > tmp = findLCS ( X , Y , m - 1 , n - 1 ) ; foreach ( String str in tmp ) s . Add ( str + X [ m - 1 ] ) ; } else { if ( L [ m - 1 , n ] >= L [ m , n - 1 ] ) s = findLCS ( X , Y , m - 1 , n ) ; if ( L [ m , n - 1 ] >= L [ m - 1 , n ] ) { HashSet < String > tmp = findLCS ( X , Y , m , n - 1 ) ; foreach ( String str in tmp ) s . Add ( str ) ; } } return s ; } static int LCS ( String X , String Y , int m , int n ) { for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ m , n ] ; } public static void Main ( String [ ] args ) { String X = " AGTGATG " ; String Y = " GTTAG " ; int m = X . Length ; int n = Y . Length ; Console . WriteLine ( " LCS ▁ length ▁ is ▁ " + LCS ( X , Y , m , n ) ) ; HashSet < String > s = findLCS ( X , Y , m , n ) ; foreach ( String str in s ) Console . WriteLine ( str ) ; } }
Number of non | A naive C # solution to count solutions of a + b + c = n ; Returns count of solutions of a + b + c = n ; Initialize result ; Consider all triplets and increment result whenever sum of a triplet is n . ; Driver code
using System ; class GFG { static int countIntegralSolutions ( int n ) { int result = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n - i ; j ++ ) for ( int k = 0 ; k <= ( n - i - j ) ; k ++ ) if ( i + j + k == n ) result ++ ; return result ; } public static void Main ( ) { int n = 3 ; Console . Write ( countIntegralSolutions ( n ) ) ; } }
Number of non | C # solution to count solutions of a + b + c = n ; Returns count of solutions of a + b + c = n ; Driver code
using System ; class GFG { static int countIntegralSolutions ( int n ) { return ( ( n + 1 ) * ( n + 2 ) ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( countIntegralSolutions ( n ) ) ; } }
Maximum absolute difference between sum of two contiguous sub | C # program to find two non - overlapping contiguous sub - arrays such that the absolute difference ; Find maximum subarray sum for subarray [ 0. . i ] using standard Kadane ' s ▁ algorithm . ▁ This ▁ version ▁ of ▁ Kadane ' s Algorithm will work if all numbers are negative . ; Find maximum subarray sum for subarray [ i . . n ] using Kadane ' s ▁ algorithm . ▁ ▁ This ▁ version ▁ of ▁ Kadane ' s Algorithm will work if all numbers are negative ; The function finds two non - overlapping contiguous sub - arrays such that the absolute difference between the sum of two sub - array is maximum . ; create and build an array that stores maximum sums of subarrays that lie in arr [ 0. . . i ] ; create and build an array that stores maximum sums of subarrays that lie in arr [ i + 1. . . n - 1 ] ; Invert array ( change sign ) to find minumum sum subarrays . ; create and build an array that stores minimum sums of subarrays that lie in arr [ 0. . . i ] ; create and build an array that stores minimum sums of subarrays that lie in arr [ i + 1. . . n - 1 ] ; For each index i , take maximum of 1. abs ( max sum subarray that lies in arr [ 0. . . i ] - min sum subarray that lies in arr [ i + 1. . . n - 1 ] ) 2. abs ( min sum subarray that lies in arr [ 0. . . i ] - max sum subarray that lies in arr [ i + 1. . . n - 1 ] ) ; driver code
using System ; class GFG { static int maxLeftSubArraySum ( int [ ] a , int size , int [ ] sum ) { int max_so_far = a [ 0 ] ; int curr_max = a [ 0 ] ; sum [ 0 ] = max_so_far ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . Max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; sum [ i ] = max_so_far ; } return max_so_far ; } static int maxRightSubArraySum ( int [ ] a , int n , int [ ] sum ) { int max_so_far = a [ n ] ; int curr_max = a [ n ] ; sum [ n ] = max_so_far ; for ( int i = n - 1 ; i >= 0 ; i -- ) { curr_max = Math . Max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; sum [ i ] = max_so_far ; } return max_so_far ; } static int findMaxAbsDiff ( int [ ] arr , int n ) { int [ ] leftMax = new int [ n ] ; maxLeftSubArraySum ( arr , n , leftMax ) ; int [ ] rightMax = new int [ n ] ; maxRightSubArraySum ( arr , n - 1 , rightMax ) ; int [ ] invertArr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) invertArr [ i ] = - arr [ i ] ; int [ ] leftMin = new int [ n ] ; maxLeftSubArraySum ( invertArr , n , leftMin ) ; for ( int i = 0 ; i < n ; i ++ ) leftMin [ i ] = - leftMin [ i ] ; int [ ] rightMin = new int [ n ] ; maxRightSubArraySum ( invertArr , n - 1 , rightMin ) ; for ( int i = 0 ; i < n ; i ++ ) rightMin [ i ] = - rightMin [ i ] ; int result = - 2147483648 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int absValue = Math . Max ( Math . Abs ( leftMax [ i ] - rightMin [ i + 1 ] ) , Math . Abs ( leftMin [ i ] - rightMax [ i + 1 ] ) ) ; if ( absValue > result ) result = absValue ; } return result ; } public static void Main ( ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . Length ; Console . Write ( findMaxAbsDiff ( a , n ) ) ; } }
Ways to arrange Balls such that adjacent balls are of different types | C # program to count number of ways to arrange three types of balls such that no two balls of same color are adjacent to each other ; Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q Balls and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; Returns count of required arrangements ; Three cases arise : 1. Last required balls is type P 2. Last required balls is type Q 3. Last required balls is type R ; Driver code
using System ; class GFG { static int countWays ( int p , int q , int r , int last ) { if ( p < 0 q < 0 r < 0 ) return 0 ; if ( p == 1 && q == 0 && r == 0 && last == 0 ) return 1 ; if ( p == 0 && q == 1 && r == 0 && last == 1 ) return 1 ; if ( p == 0 && q == 0 && r == 1 && last == 2 ) return 1 ; if ( last == 0 ) return countWays ( p - 1 , q , r , 1 ) + countWays ( p - 1 , q , r , 2 ) ; if ( last == 1 ) return countWays ( p , q - 1 , r , 0 ) + countWays ( p , q - 1 , r , 2 ) ; if ( last == 2 ) return countWays ( p , q , r - 1 , 0 ) + countWays ( p , q , r - 1 , 1 ) ; return 0 ; } static int countUtil ( int p , int q , int r ) { return countWays ( p , q , r , 0 ) + countWays ( p , q , r , 1 ) + countWays ( p , q , r , 2 ) ; } public static void Main ( ) { int p = 1 , q = 1 , r = 1 ; Console . Write ( countUtil ( p , q , r ) ) ; } }
Partition a set into two subsets such that the difference of subset sums is minimum | C # code to partition a set into two subsets such that the difference of subset sums is minimum ; Function to find the minimum sum ; If we have reached last element . Sum of one subset is sumCalculated , sum of other subset is sumTotal - sumCalculated . Return absolute difference of two sums . ; For every item arr [ i ] , we have two choices ( 1 ) We do not include it first set ( 2 ) We include it in first set We return minimum of two choices ; Returns minimum possible difference between sums of two subsets ; Compute total sum of elements ; Compute result using recursive function ; Driver program to test above function
using System ; class GFG { public static int findMinRec ( int [ ] arr , int i , int sumCalculated , int sumTotal ) { if ( i == 0 ) return Math . Abs ( ( sumTotal - sumCalculated ) - sumCalculated ) ; return Math . Min ( findMinRec ( arr , i - 1 , sumCalculated + arr [ i - 1 ] , sumTotal ) , findMinRec ( arr , i - 1 , sumCalculated , sumTotal ) ) ; } public static int findMin ( int [ ] arr , int n ) { int sumTotal = 0 ; for ( int i = 0 ; i < n ; i ++ ) sumTotal += arr [ i ] ; return findMinRec ( arr , n , 0 , sumTotal ) ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 4 , 2 , 2 , 1 } ; int n = arr . Length ; Console . Write ( " The ▁ minimum ▁ difference " + " ▁ between ▁ two ▁ sets ▁ is ▁ " + findMin ( arr , n ) ) ; } }
Partition a set into two subsets such that the difference of subset sums is minimum |  ; dp [ i ] gives whether is it possible to get i as sum of elements dd is helper variable we use dd to ignoring duplicates ; Initialising dp and dd ; sum = 0 is possible ; updating dd [ k ] as true if k can be formed using elements from 1 to i + 1 ; updating dd ; dd [ j ] = false ; reset dd ; checking the number from sum / 2 to 1 which is possible to get as sum ; since i is possible to form then another number is sum - i so mindifference is sum - i - i
using System ; public class GFG { static int minDifference ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int y = sum / 2 + 1 ; bool [ ] dp = new bool [ y ] ; bool [ ] dd = new bool [ y ] ; for ( int i = 0 ; i < y ; i ++ ) { dp [ i ] = dd [ i ] = false ; } dd [ 0 ] = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j + arr [ i ] < y ; j ++ ) { if ( dp [ j ] ) dd [ j + arr [ i ] ] = true ; } for ( int j = 0 ; j < y ; j ++ ) { if ( dd [ j ] ) dp [ j ] = true ; } } for ( int i = y - 1 ; i >= 0 ; i -- ) { if ( dp [ i ] ) return ( sum - 2 * i ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 11 , 5 } ; int n = arr . Length ; Console . Write ( " The ▁ Minimum ▁ difference ▁ of ▁ 2 ▁ sets ▁ is ▁ " + minDifference ( arr , n ) + ' STRNEWLINE ' ) ; } }
Count number of ways to partition a set into k subsets | C # program to count number of partitions of a set with n elements into k subsets ; Returns count of different partitions of n elements in k subsets ; Base cases ; S ( n + 1 , k ) = k * S ( n , k ) + S ( n , k - 1 ) ; Driver program
using System ; class GFG { public static int countP ( int n , int k ) { if ( n == 0 k == 0 k > n ) return 0 ; if ( k == 1 k == n ) return 1 ; return ( k * countP ( n - 1 , k ) + countP ( n - 1 , k - 1 ) ) ; } public static void Main ( ) { Console . WriteLine ( countP ( 3 , 2 ) ) ; } }
Count number of ways to partition a set into k subsets | A Dynamic Programming based C # program to count number of partitions of a set with n elements into k subsets ; Returns count of different partitions of n elements in k subsets ; Table to store results of subproblems ; Base cases ; Fill rest of the entries in dp [ ] [ ] in bottom up manner ; Driver code
using System ; class GFG { static int countP ( int n , int k ) { int [ , ] dp = new int [ n + 1 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = 0 ; for ( int i = 0 ; i <= k ; i ++ ) dp [ 0 , k ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= k ; j ++ ) if ( j == 1 i == j ) dp [ i , j ] = 1 ; else dp [ i , j ] = j * dp [ i - 1 , j ] + dp [ i - 1 , j - 1 ] ; return dp [ n , k ] ; } public static void Main ( ) { Console . Write ( countP ( 5 , 2 ) ) ; } }
Count number of ways to cover a distance | A naive recursive C # program to count number of ways to cover a distance with 1 , 2 and 3 steps ; Function returns count of ways to cover ' dist ' ; Base cases ; Recur for all previous 3 and add the results ; Driver Code
using System ; class GFG { static int printCountRec ( int dist ) { if ( dist < 0 ) return 0 ; if ( dist == 0 ) return 1 ; return printCountRec ( dist - 1 ) + printCountRec ( dist - 2 ) + printCountRec ( dist - 3 ) ; } public static void Main ( ) { int dist = 4 ; Console . WriteLine ( printCountRec ( dist ) ) ; } }