codeparrot/xlcost-text-to-code huggingface dataset:
Ways to form a group from three groups with given constraints | C # program to find the number of ways to form the group of peopls ; Function to pre - compute the Combination using DP ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; return C [ n , k ] ; ; Function to find the number of ways ; Function to pre - compute ; Sum the Zci ; Iterate for second position ; Iterate for first position ; Multiply the common Combination value ; Driver Code
using System ; class GFG { static int [ , ] C = new int [ 1000 , 1000 ] ; static void binomialCoeff ( int n ) { int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } } static int numberOfWays ( int x , int y , int z ) { binomialCoeff ( Math . Max ( x , Math . Max ( y , z ) ) ) ; int sum = 0 ; for ( int i = 1 ; i <= z ; i ++ ) { sum = ( sum + C [ z , i ] ) ; } int sum1 = 0 ; for ( int i = 1 ; i <= y ; i ++ ) { for ( int j = i + 1 ; j <= x ; j ++ ) { sum1 = ( sum1 + ( C [ y , i ] * C [ x , j ] ) ) ; } } sum1 = ( sum * sum1 ) ; return sum1 ; } public static void Main ( String [ ] args ) { int x = 3 ; int y = 2 ; int z = 1 ; Console . WriteLine ( numberOfWays ( x , y , z ) ) ; } }
Minimum cost to make a string free of a subsequence | C # implementation of the approach ; Function to return the minimum cost ; Traverse the string ; Min Cost to remove ' c ' ; Min Cost to remove subsequence " co " ; Min Cost to remove subsequence " cod " ; Min Cost to remove subsequence " code " ; Return the minimum cost ; Driver program
using System ; class GFG { public static int findCost ( string str , int [ ] arr , int n ) { long costofC = 0 , costofO = 0 , costofD = 0 , costofE = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' c ' ) { costofC += arr [ i ] ; } else if ( str [ i ] == ' o ' ) { costofO = Math . Min ( costofC , costofO + arr [ i ] ) ; } else if ( str [ i ] == ' d ' ) { costofD = Math . Min ( costofO , costofD + arr [ i ] ) ; } else if ( str [ i ] == ' e ' ) { costofE = Math . Min ( costofD , costofE + arr [ i ] ) ; } } return ( int ) costofE ; } public static void Main ( string [ ] args ) { string str = " geekcodergeeks " ; int [ ] arr = new int [ ] { 1 , 2 , 1 , 3 , 4 , 2 , 6 , 4 , 6 , 2 , 3 , 3 , 3 , 2 } ; int n = arr . Length ; Console . Write ( findCost ( str , arr , n ) ) ; } }
Longest subsequence such that adjacent elements have at least one common digit | C # program to find maximum length subsequence such that adjacent elements have at least one common digit ; Returns length of maximum length subsequence ; To store the length of the maximum length subsequence ; To store current element arr [ i ] ; To store the length of the sub - sequence ending at index i and having common digit d ; To store digits present in current element ; To store length of maximum length subsequence ending at index i ; For first element maximum length is 1 for each digit ; Find digits of each element , then find length of subsequence for each digit and then find local maximum ; Find digits in current element ; For each digit present find length of subsequence and find local maximum ; Update value of dp [ i , d ] for each digit present in current element to local maximum found . ; Update maximum length with local maximum ; Driver code
using System ; class GFG { static int findSubsequence ( int [ ] arr , int n ) { int len = 1 ; int tmp ; int i , j , d ; int [ , ] dp = new int [ n , 10 ] ; int [ ] cnt = new int [ 10 ] ; int locMax ; tmp = arr [ 0 ] ; while ( tmp > 0 ) { dp [ 0 , tmp % 10 ] = 1 ; tmp /= 10 ; } for ( i = 1 ; i < n ; i ++ ) { tmp = arr [ i ] ; locMax = 1 ; for ( int x = 0 ; x < 10 ; x ++ ) cnt [ x ] = 0 ; while ( tmp > 0 ) { cnt [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i , d ] = 1 ; for ( j = 0 ; j < i ; j ++ ) { dp [ i , d ] = Math . Max ( dp [ i , d ] , dp [ j , d ] + 1 ) ; locMax = Math . Max ( dp [ i , d ] , locMax ) ; } } } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i , d ] = locMax ; } } len = Math . Max ( len , locMax ) ; } return len ; } public static void Main ( ) { int [ ] arr = { 1 , 12 , 44 , 29 , 33 , 96 , 89 } ; int n = arr . Length ; Console . WriteLine ( findSubsequence ( arr , n ) ) ; } }
Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime | C # implementation of the above approach ; Prime numbers upto 100 ; Function to return the count of required numbers from 0 to num ; Base Case ; check if the difference is equal to any prime number ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current position is odd add it to currOdd , otherwise to currEven ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int M = 18 ; static int a , b ; static int [ , , , ] dp = new int [ M , 90 , 90 , 2 ] ; static int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 } ; static int count ( int pos , int even , int odd , int tight , List < int > num ) { if ( pos == num . Count ) { if ( ( num . Count & 1 ) != 0 ) { int t = odd ; odd = even ; even = t ; } int d = even - odd ; for ( int i = 0 ; i < 24 ; i ++ ) if ( d == prime [ i ] ) return 1 ; return 0 ; } if ( dp [ pos , even , odd , tight ] != - 1 ) return dp [ pos , even , odd , tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num [ pos ] ) ; for ( int d = 0 ; d <= limit ; d ++ ) { int currF = tight , currEven = even ; int currOdd = odd ; if ( d < num [ pos ] ) currF = 1 ; if ( ( pos & 1 ) != 0 ) currOdd += d ; else currEven += d ; ans += count ( pos + 1 , currEven , currOdd , currF , num ) ; } return dp [ pos , even , odd , tight ] = ans ; } static int solve ( int x ) { List < int > num = new List < int > ( ) ; while ( x != 0 ) { num . Add ( x % 10 ) ; x /= 10 ; } num . Reverse ( ) ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) for ( int k = 0 ; k < dp . GetLength ( 2 ) ; k ++ ) for ( int k1 = 0 ; k1 < dp . GetLength ( 3 ) ; k1 ++ ) dp [ i , j , k , k1 ] = - 1 ; return count ( 0 , 0 , 0 , 0 , num ) ; } public static void Main ( String [ ] args ) { int L = 1 , R = 50 ; Console . WriteLine ( solve ( R ) - solve ( L - 1 ) ) ; L = 50 ; R = 100 ; Console . WriteLine ( solve ( R ) - solve ( L - 1 ) ) ; } }
Find the number of distinct pairs of vertices which have a distance of exactly k in a tree | C # implementation of the approach ; To store vertices and value of k ; To store number vertices at a level i ; To store the readonly answer ; Function to add an edge between two nodes ; Function to find the number of distinct pairs of the vertices which have a distance of exactly k in a tree ; At level zero vertex itself is counted ; Count the pair of vertices at distance k ; For all levels count vertices ; Driver code ; Add edges ; Function call ; Required answer
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 5005 ; static int n , k ; static List < int > [ ] gr = new List < int > [ N ] ; static int [ , ] d = new int [ N , 505 ] ; static int ans = 0 ; static void Add_edge ( int x , int y ) { gr [ x ] . Add ( y ) ; gr [ y ] . Add ( x ) ; } static void dfs ( int v , int par ) { d [ v , 0 ] = 1 ; foreach ( int i in gr [ v ] ) { if ( i != par ) { dfs ( i , v ) ; for ( int j = 1 ; j <= k ; j ++ ) ans += d [ i , j - 1 ] * d [ v , k - j ] ; for ( int j = 1 ; j <= k ; j ++ ) d [ v , j ] += d [ i , j - 1 ] ; } } } public static void Main ( String [ ] args ) { n = 5 ; k = 2 ; for ( int i = 0 ; i < N ; i ++ ) gr [ i ] = new List < int > ( ) ; Add_edge ( 1 , 2 ) ; Add_edge ( 2 , 3 ) ; Add_edge ( 3 , 4 ) ; Add_edge ( 2 , 5 ) ; dfs ( 1 , 0 ) ; Console . Write ( ans ) ; } }
Sum of XOR of all subarrays | C # program to find the sum of XOR of all subarray of the array ; Function to calculate the sum of XOR of all subarrays ; variable to store the final sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update sum ; updating the multiplier ; returning the sum ; Driver Code
using System ; class GFG { static int findXorSum ( int [ ] arr , int n ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { int c_odd = 0 ; bool odd = false ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) odd = ( ! odd ) ; if ( odd ) c_odd ++ ; } for ( int j = 0 ; j < n ; j ++ ) { sum += ( mul * c_odd ) ; if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) c_odd = ( n - j - c_odd ) ; } mul *= 2 ; } return sum ; } static void Main ( ) { int [ ] arr = { 3 , 8 , 13 } ; int n = arr . Length ; Console . WriteLine ( findXorSum ( arr , n ) ) ; } }
Minimum replacements to make adjacent characters unequal in a ternary string | Set | C # program to count the minimal replacements such that adjacent characters are unequal ; function to return integer value of i - th character in the string ; Function to count the number of minimal replacements ; If the string has reached the end ; If the state has been visited previously ; Get the current value of character ; If it is equal then change it ; All possible changes ; Change done ; else If same no change ; Driver Code ; Length of string ; Create a DP array ; First character ; Function to find minimal replacements
using System ; class GFG { static int charVal ( string s , int i ) { if ( s [ i ] == '0' ) { return 0 ; } else if ( s [ i ] == '1' ) { return 1 ; } else { return 2 ; } } static int countMinimalReplacements ( string s , int i , int prev , int [ , ] dp , int n ) { if ( i == n ) { return 0 ; } if ( dp [ i , prev ] != - 1 ) { return dp [ i , prev ] ; } int val = charVal ( s , i ) ; int ans = int . MaxValue ; if ( val == prev ) { val = 0 ; for ( int cur = 0 ; cur <= 2 ; cur ++ ) { if ( cur == prev ) { continue ; } val = 1 + countMinimalReplacements ( s , i + 1 , cur , dp , n ) ; ans = Math . Min ( ans , val ) ; } } { ans = countMinimalReplacements ( s , i + 1 , val , dp , n ) ; } return dp [ i , val ] = ans ; } public static void Main ( ) { string s = "201220211" ; int n = s . Length ; int [ , ] dp = new int [ n , 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { dp [ i , j ] = - 1 ; } } int val = charVal ( s , 0 ) ; Console . WriteLine ( countMinimalReplacements ( s , 1 , val , dp , n ) ) ; } }
A variation of Rat in a Maze : multiple steps or jumps allowed | C # implementation of the approach ; Function to check whether the path exists ; Declaring and initializing CRF ( can reach from ) matrix ; Using the DP to fill CRF matrix in correct order ; If it is possible to get to a valid location from cell maze [ k , j ] ; If it is possible to get to a valid location from cell maze [ j , k ] ; If CRF [ 0 , 0 ] is false it means we cannot reach the end of the maze at all ; Filling the solution matrix using CRF ; Get to a valid location from the current cell ; Utility function to print the contents of a 2 - D array ; Driver code ; If path exists ; Print the path
using System ; class GFG { static int MAX = 50 ; static Boolean hasPath ( int [ , ] maze , int [ , ] sol , int N ) { int i , j , k ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) sol [ i , j ] = 0 ; Boolean [ , ] CRF = new Boolean [ N , N ] ; CRF [ N - 1 , N - 1 ] = true ; for ( k = N - 1 ; k >= 0 ; k -- ) { for ( j = k ; j >= 0 ; j -- ) { if ( ! ( k == N - 1 && j == N - 1 ) ) { for ( int a = 0 ; a <= maze [ k , j ] ; a ++ ) { if ( ( j + a < N && CRF [ k , j + a ] == true ) || ( k + a < N && CRF [ k + a , j ] == true ) ) { CRF [ k , j ] = true ; break ; } } for ( int a = 0 ; a <= maze [ j , k ] ; a ++ ) { if ( ( k + a < N && CRF [ j , k + a ] == true ) || ( j + a < N && CRF [ j + a , k ] == true ) ) { CRF [ j , k ] = true ; break ; } } } } } if ( CRF [ 0 , 0 ] == false ) return false ; i = 0 ; j = 0 ; while ( ! ( i == N - 1 && j == N - 1 ) ) { sol [ i , j ] = 1 ; if ( maze [ i , j ] > 0 ) for ( int a = 1 ; a <= maze [ i , j ] ; a ++ ) { if ( ( j + a < N && CRF [ i , j + a ] == true ) ) { j = j + a ; break ; } else if ( ( i + a < N && CRF [ i + a , j ] == true ) ) { i = i + a ; break ; } } } sol [ N - 1 , N - 1 ] = 1 ; return true ; } static void printMatrix ( int [ , ] sol , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( sol [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] maze = { { 2 , 2 , 1 , 1 , 0 } , { 0 , 0 , 3 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 0 , 3 , 0 , 0 } } ; int N = maze . GetLength ( 0 ) ; int [ , ] sol = new int [ N , MAX ] ; if ( hasPath ( maze , sol , N ) ) printMatrix ( sol , N ) ; else Console . WriteLine ( " No ▁ path ▁ exists " ) ; } }
Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | C # program to check if we come back to zero or not in a clock ; Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; Check if jth bit in the counter is set If set then print jth element from set ; sum += a [ j ] ; if set then consider as ' + ' ; sum -= a [ j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver code
using System ; class GfG { static bool checkCombinations ( int [ ] a , int n ) { int pow_set_size = ( int ) Math . Pow ( 2 , n ) ; int counter , j ; for ( counter = 0 ; counter < pow_set_size ; counter ++ ) { int sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) else } if ( sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } public static void Main ( ) { int [ ] a = { 60 , 60 , 120 } ; int n = a . Length ; if ( checkCombinations ( a , n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Find probability of selecting element from kth column after N iterations | C # implementation of the above approach ; Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code
using System ; class GFG { static int n = 4 ; static int m = 4 ; static float calcProbability ( int [ , ] M , int k ) { float [ , ] dp = new float [ m , n ] ; float [ ] sum = new float [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { dp [ 0 , j ] = M [ 0 , j ] ; sum [ 0 ] = sum [ 0 ] + dp [ 0 , j ] ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] += dp [ i - 1 , j ] / sum [ i - 1 ] + M [ i , j ] ; sum [ i ] += dp [ i , j ] ; } } return dp [ n - 1 , k - 1 ] / sum [ n - 1 ] ; } public static void Main ( ) { int [ , ] M = { { 1 , 1 , 0 , 3 } , { 2 , 3 , 2 , 3 } , { 9 , 3 , 0 , 2 } , { 2 , 3 , 2 , 2 } } ; int k = 3 ; Console . Write ( calcProbability ( M , k ) ) ; } }
Possible cuts of a number such that maximum parts are divisible by 3 | C # program to find the maximum number of numbers divisible by 3 in a large number . ; Function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; Stores last index of a remainder ; last visited place of remainder zero is at 0. ; To store result from 0 to i ; get the remainder ; Get maximum res [ i ] value ; Driver Code
using System ; class GFG { static int MaximumNumbers ( String s ) { int n = s . Length ; int [ ] remIndex = { - 1 , - 1 , - 1 } ; remIndex [ 0 ] = 0 ; int [ ] res = new int [ n + 1 ] ; int r = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { r = ( r + s [ i - 1 ] - '0' ) % 3 ; res [ i ] = res [ i - 1 ] ; if ( remIndex [ r ] != - 1 ) res [ i ] = Math . Max ( res [ i ] , res [ remIndex [ r ] ] + 1 ) ; remIndex [ r ] = i + 1 ; } return res [ n ] ; } public static void Main ( String [ ] args ) { String s = "12345" ; Console . WriteLine ( MaximumNumbers ( s ) ) ; } }
Minimum steps required to convert X to Y where a binary matrix represents the possible conversions | C # implementation of the above approach ; dist [ ] [ ] will be the output matrix that will finally have the shortest distances between every pair of numbers ; Initially same as mat ; Add all numbers one by one to the set of intermediate numbers . Before start of an iteration , we have shortest distances between all pairs of numbers such that the shortest distances consider only the numbers in set { 0 , 1 , 2 , . . k - 1 } as intermediate numbers . After the end of an iteration , vertex no . k is added to the set of intermediate numbers and the set becomes { 0 , 1 , 2 , . . k } ; Pick all numbers as source one by one ; Pick all numbers as destination for the above picked source ; If number k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; If no path ; Driver Code
using System ; class GFG { static int INF = 99999 ; static int findMinimumSteps ( int [ , ] mat , int x , int y , int n ) { int i , j , k ; int [ , ] dist = new int [ n , n ] ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i , j ] == 0 ) dist [ i , j ] = INF ; else dist [ i , j ] = 1 ; if ( i == j ) dist [ i , j ] = 1 ; } } for ( k = 0 ; k < n ; k ++ ) { for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( dist [ i , k ] + dist [ k , j ] < dist [ i , j ] ) dist [ i , j ] = dist [ i , k ] + dist [ k , j ] ; } } } if ( dist [ x , y ] < INF ) return dist [ x , y ] ; else return - 1 ; } public static void Main ( ) { int [ , ] mat = { { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } } ; int x = 2 , y = 3 ; int size = mat . GetLength ( 0 ) ; Console . WriteLine ( findMinimumSteps ( mat , x , y , size ) ) ; } }
Count number of paths whose weight is exactly X and has at | C # program to count the number of paths ; Function to find the number of paths ; If the summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; else Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths
using System ; class GFG { static int max = 4 ; static int c = 2 ; static int countPaths ( int sum , int get , int m , int n , int [ , ] dp ) { if ( sum < 0 ) return 0 ; if ( sum == 0 ) return get ; if ( dp [ sum , get ] != - 1 ) return dp [ sum , get ] ; int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i == m ) res += countPaths ( sum - i , 1 , m , n , dp ) ; res += countPaths ( sum - i , get , m , n , dp ) ; } dp [ sum , get ] = res ; return dp [ sum , get ] ; } public static void Main ( ) { int n = 3 , m = 2 , x = 3 ; int [ , ] dp = new int [ max + 1 , 2 ] ; for ( int i = 0 ; i <= max ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( countPaths ( x , 0 , m , n , dp ) ) ; } }
Sum of kth powers of first n natural numbers | C # program to find sum pf k - th powers of first n natural numbers . ; A global array to store factorials ; Function to calculate the factorials of all the numbers upto k ; Function to return the binomial coefficient ; nCr = ( n ! * ( n - r ) ! ) / r ! ; Function to return the sum of kth powers of n natural numbers ; When j is unity ; Calculating sum ( n ^ 1 ) of unity powers of n ; storing sum ( n ^ 1 ) for sum ( n ^ 2 ) ; If k = 1 then temp is the result ; For finding sum ( n ^ k ) removing 1 and n * kCk from ( n + 1 ) ^ k ; Removing all kC2 * sum ( n ^ ( k - 2 ) ) + ... + kCk - 1 * ( sum ( n ^ ( k - ( k - 1 ) ) ; Storing the result for next sum of next powers of k ; Driver code
using System ; class GFG { static int MAX_K = 15 ; static int [ ] fac = new int [ MAX_K ] ; static void factorial ( int k ) { fac [ 0 ] = 1 ; for ( int i = 1 ; i <= k + 1 ; i ++ ) { fac [ i ] = ( i * fac [ i - 1 ] ) ; } } static int bin ( int a , int b ) { int ans = ( ( fac [ a ] ) / ( fac [ a - b ] * fac [ b ] ) ) ; return ans ; } static int sumofn ( int n , int k ) { int p = 0 ; int num1 , temp ; int [ ] arr = new int [ 1000 ] ; for ( int j = 1 ; j <= k ; j ++ ) { if ( j == 1 ) { num1 = ( n * ( n + 1 ) ) / 2 ; arr [ p ++ ] = num1 ; temp = num1 ; } else { temp = ( ( int ) Math . Pow ( n + 1 , j + 1 ) - 1 - n ) ; for ( int s = 1 ; s < j ; s ++ ) { temp = temp - ( arr [ j - s - 1 ] * bin ( j + 1 , s + 1 ) ) ; } temp = temp / ( j + 1 ) ; arr [ p ++ ] = temp ; } } temp = arr [ p - 1 ] ; return temp ; } public static void Main ( ) { int n = 5 , k = 2 ; factorial ( k ) ; Console . WriteLine ( sumofn ( n , k ) ) ; } }
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors | C # implementation of the approach ; Recursive function to find the required number of ways ; When all positions are filled ; If adjacent pairs are exactly K ; If already calculated ; Next position filled with same color ; Next position filled with different color So there can be m - 1 different colors ; Driver Code
using System ; class solution { static int max = 4 ; static int countWays ( int index , int cnt , int [ , ] dp , int n , int m , int k ) { if ( index == n ) { if ( cnt == k ) return 1 ; else return 0 ; } if ( dp [ index , cnt ] != - 1 ) return dp [ index , cnt ] ; int ans = 0 ; ans += countWays ( index + 1 , cnt , dp , n , m , k ) ; ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) ; return dp [ index , cnt ] = ans ; } public static void Main ( ) { int n = 3 , m = 3 , k = 2 ; int [ , ] dp = new int [ n + 1 , max ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < max ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( m * countWays ( 1 , 0 , dp , n , m , k ) ) ; } }