codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to represent a number as sum of k fibonacci numbers | C # implementation of above approach ; to store fibonacci numbers 42 second number in fibonacci series largest possible integer ; Function to generate fibonacci series ; Recursive function to return the number of ways ; base condition ; for recursive function call ; Driver code
using System ; class GFG { static int [ ] fib = new int [ 43 ] ; public static void fibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( int i = 2 ; i < 43 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } public static int rec ( int x , int y , int last ) { if ( y == 0 ) { if ( x == 0 ) return 1 ; return 0 ; } int sum = 0 ; for ( int i = last ; i >= 0 && fib [ i ] * y >= x ; i -- ) { if ( fib [ i ] > x ) continue ; sum += rec ( x - fib [ i ] , y - 1 , i ) ; } return sum ; } static void Main ( ) { for ( int i = 0 ; i < 43 ; i ++ ) fib [ i ] = 0 ; fibonacci ( ) ; int n = 13 , k = 3 ; Console . Write ( " Possible ▁ ways ▁ are : ▁ " + rec ( n , k , 42 ) ) ; } }
Minimum cost to reach the top of the floor by climbing stairs | C # program to find the minimum cost required to reach the n - th floor ; function to find the minimum cost to reach N - th floor ; declare an array ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code
using System ; class GFG { static int minimumCost ( int [ ] cost , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) return cost [ 0 ] ; dp [ 0 ] = cost [ 0 ] ; dp [ 1 ] = cost [ 1 ] ; for ( int i = 2 ; i < n ; i ++ ) { dp [ i ] = Math . Min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] ; } return Math . Min ( dp [ n - 2 ] , dp [ n - 1 ] ) ; } public static void Main ( ) { int [ ] a = { 16 , 19 , 10 , 12 , 18 } ; int n = a . Length ; Console . WriteLine ( minimumCost ( a , n ) ) ; } }
Minimum cost to reach the top of the floor by climbing stairs | C # program to find the minimum cost required to reach the n - th floor space - optimized solution ; function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code
using System ; class GFG { static int minimumCost ( int [ ] cost , int n ) { int dp1 = 0 , dp2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int dp0 = cost [ i ] + Math . Min ( dp1 , dp2 ) ; dp2 = dp1 ; dp1 = dp0 ; } return Math . Min ( dp1 , dp2 ) ; } public static void Main ( ) { int [ ] a = { 2 , 5 , 3 , 1 , 7 , 3 , 4 } ; int n = a . Length ; Console . Write ( minimumCost ( a , n ) ) ; } }
Sudo Placement [ 1.5 ] | Wolfish | C # program for SP - Wolfish ; Function to find the maxCost of path from ( n - 1 , n - 1 ) to ( 0 , 0 ) ; base condition ; reaches the point ; if the state has been visited previously ; i + j ; check if it is a power of 2 , then only move diagonally ; if not a power of 2 then move side - wise ; Function to return the maximum cost ; calling dp function to get the answer ; Driver Code ; Function calling to get the answer
using System ; class GFG { static int size = 1000 ; static int maxCost ( int [ , ] a , int m , int n , int [ , ] dp ) { if ( n < 0 m < 0 ) return ( int ) - 1e9 ; else if ( m == 0 && n == 0 ) return 0 ; else if ( dp [ m , n ] != - 1 ) return dp [ m , n ] ; else { int num = m + n ; if ( ( num & ( num - 1 ) ) == 0 ) return dp [ m , n ] = a [ m , n ] + maxCost ( a , m - 1 , n - 1 , dp ) ; else return dp [ m , n ] = ( a [ m , n ] + Math . Max ( maxCost ( a , m - 1 , n , dp ) , maxCost ( a , m , n - 1 , dp ) ) ) ; } } static int answer ( int [ , ] a , int n ) { int [ , ] dp = new int [ size , size ] ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size ; j ++ ) { dp [ i , j ] = - 1 ; } } return maxCost ( a , n - 1 , n - 1 , dp ) ; } public static void Main ( String [ ] args ) { int [ , ] a = { { 1 , 2 , 3 , 1 } , { 4 , 5 , 6 , 1 } , { 7 , 8 , 9 , 1 } , { 1 , 1 , 1 , 1 } } ; int n = 4 ; Console . WriteLine ( answer ( a , n ) ) ; } }
Edit distance and LCS ( Longest Common Subsequence ) | C # program to find Edit Distance ( when only two operations areallowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver Code
using System ; class GFG { static int editDistanceWith2Ops ( String X , String Y ) { int m = X . Length , n = Y . Length ; 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 ] ) ; } } } int lcs = L [ m , n ] ; return ( m - lcs ) + ( n - lcs ) ; } public static void Main ( ) { String X = " abc " , Y = " acd " ; Console . Write ( editDistanceWith2Ops ( X , Y ) ) ; } }
Longest Common Subsequence | DP using Memoization | A Naive recursive C # implementation of LCS of two strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code ; Find the length of String
using System ; class GFG { 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 Math . 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 : ▁ " + lcs ( X , Y , m , n ) ) ; } }
Number of different cyclic paths of length N in a tetrahedron | C # program count total number of paths to reach B from B ; Function to count the number of steps in a tetrahedron ; initially coming to B is B -> B ; cannot reach A , D or C ; iterate for all steps ; recurrence relation ; memoize previous values ; returns steps ; Driver Code
using System ; class GFG { static int countPaths ( int n ) { int zB = 1 ; int zADC = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int nzB = zADC * 3 ; int nzADC = ( zADC * 2 + zB ) ; zB = nzB ; zADC = nzADC ; } return zB ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( countPaths ( n ) ) ; } }
Maximum sub | C # implementation to find the maximum area sub - matrix having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; Traverse the given array ; Accumulating sum ; When subarray starts form index '0' ; Make an entry for ' sum ' if it is not present in ' um ' ; Check if ' sum - 1' is present in ' um ' or not ; Update ' start ' , ' finish ' and maxLength ; Required maximum length ; Function to find the maximum area sub - matrix having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Variables to store final and intermediate results ; Set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' , consider '0' as ' - 1' ; Function to set the ' start ' and ' finish ' variables having index values of temp [ ] which contains the longest subarray of temp [ ] having count of 1 ' s ▁ ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Compare with maximum area so far and accordingly update the final variables ; Print final values ; Driver code
using System ; using System . Collections . Generic ; using System . Collections ; class GFG { static int lenOfLongSubarr ( int [ ] arr , int n , ref int start , ref int finish ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 1 ) { start = 0 ; finish = i ; maxLen = i + 1 ; } else if ( ! um . ContainsKey ( sum ) ) um [ sum ] = i ; if ( um . ContainsKey ( sum - 1 ) ) { if ( maxLen < ( i - um [ sum - 1 ] ) ) start = um [ sum - 1 ] + 1 ; finish = i ; maxLen = i - um [ sum - 1 ] ; } } return maxLen ; } static void largestSubmatrix ( int [ , ] mat , int n ) { int finalLeft = 0 , finalRight = 0 , finalTop = 0 , finalBottom = 0 ; int maxArea = 0 , len , start = 0 , finish = 0 ; int [ ] temp = new int [ n ] ; for ( int left = 0 ; left < n ; left ++ ) { Array . Fill ( temp , 0 ) ; for ( int right = left ; right < n ; right ++ ) { for ( int i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i , right ] == 0 ? - 1 : 1 ; len = lenOfLongSubarr ( temp , n , ref start , ref finish ) ; if ( ( len != 0 ) && ( maxArea < ( finish - start + 1 ) * ( right - left + 1 ) ) ) { finalLeft = left ; finalRight = right ; finalTop = start ; finalBottom = finish ; maxArea = ( finish - start + 1 ) * ( right - left + 1 ) ; } } } Console . Write ( " ( Top , ▁ Left ) : ▁ ( " + finalTop + " , ▁ " + finalLeft + " ) STRNEWLINE " ) ; Console . Write ( " ( Bottom , ▁ Right ) : ▁ ( " + finalBottom + " , ▁ " + finalRight + " ) STRNEWLINE " ) ; Console . Write ( " Maximum ▁ area : ▁ " + maxArea ) ; } public static void Main ( string [ ] args ) { int [ , ] mat = new int [ , ] { { 1 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } } ; int n = 4 ; largestSubmatrix ( mat , n ) ; } }
Minimum steps to reach target by a Knight | Set 2 | C # code for minimum steps for a knight to reach target position ; initializing the matrix . ; if knight is on the target position return 0. ; } else if already calculated then return that value . Taking Absolute difference . ; there will be two distinct positions from the knight towards a target . if the target is in same row or column as of knight than there can be four positions towards the target but in that two would be the same and the other two would be the same . ; ( x1 , y1 ) and ( x2 , y2 ) are two positions . these can be different according to situation . From position of knight , the chess board can be divided into four blocks i . e . . N - E , E - S , S - W , W - N . ; ans will be , 1 + minimum of steps required from ( x1 , y1 ) and ( x2 , y2 ) . ; exchanging the coordinates x with y of both knight and target will result in same ans . ; Driver Code ; size of chess board n * n ; ( x , y ) coordinate of the knight . ( tx , ty ) coordinate of the target position . ; ( Exception ) these are the four corner points for which the minimum steps is 4. ; dp [ a , b ] , here a , b is the difference of x & tx and y & ty respectively .
using System ; public class GFG { static int [ , ] dp = new int [ 8 , 8 ] ; static int getsteps ( int x , int y , int tx , int ty ) { if ( x == tx && y == ty ) { return dp [ 0 , 0 ] ; if ( dp [ Math . Abs ( x - tx ) , Math . Abs ( y - ty ) ] != 0 ) { return dp [ Math . Abs ( x - tx ) , Math . Abs ( y - ty ) ] ; } else { int x1 , y1 , x2 , y2 ; if ( x <= tx ) { if ( y <= ty ) { x1 = x + 2 ; y1 = y + 1 ; x2 = x + 1 ; y2 = y + 2 ; } else { x1 = x + 2 ; y1 = y - 1 ; x2 = x + 1 ; y2 = y - 2 ; } } else if ( y <= ty ) { x1 = x - 2 ; y1 = y + 1 ; x2 = x - 1 ; y2 = y + 2 ; } else { x1 = x - 2 ; y1 = y - 1 ; x2 = x - 1 ; y2 = y - 2 ; } dp [ Math . Abs ( x - tx ) , Math . Abs ( y - ty ) ] = Math . Min ( getsteps ( x1 , y1 , tx , ty ) , getsteps ( x2 , y2 , tx , ty ) ) + 1 ; dp [ Math . Abs ( y - ty ) , Math . Abs ( x - tx ) ] = dp [ Math . Abs ( x - tx ) , Math . Abs ( y - ty ) ] ; return dp [ Math . Abs ( x - tx ) , Math . Abs ( y - ty ) ] ; } } static public void Main ( ) { int i , n , x , y , tx , ty , ans ; n = 100 ; x = 4 ; y = 5 ; tx = 1 ; ty = 1 ; if ( ( x == 1 && y == 1 && tx == 2 && ty == 2 ) || ( x == 2 && y == 2 && tx == 1 && ty == 1 ) ) { ans = 4 ; } else if ( ( x == 1 && y == n && tx == 2 && ty == n - 1 ) || ( x == 2 && y == n - 1 && tx == 1 && ty == n ) ) { ans = 4 ; } else if ( ( x == n && y == 1 && tx == n - 1 && ty == 2 ) || ( x == n - 1 && y == 2 && tx == n && ty == 1 ) ) { ans = 4 ; } else if ( ( x == n && y == n && tx == n - 1 && ty == n - 1 ) || ( x == n - 1 && y == n - 1 && tx == n && ty == n ) ) { ans = 4 ; } else { dp [ 1 , 0 ] = 3 ; dp [ 0 , 1 ] = 3 ; dp [ 1 , 1 ] = 2 ; dp [ 2 , 0 ] = 2 ; dp [ 0 , 2 ] = 2 ; dp [ 2 , 1 ] = 1 ; dp [ 1 , 2 ] = 1 ; ans = getsteps ( x , y , tx , ty ) ; } Console . WriteLine ( ans ) ; } }
Sequence Alignment problem | C # program to implement sequence alignment problem . ; function to find out the minimum penalty ; table for storing optimal substructure answers ; initialising the table ; calculating the minimum penalty ; Reconstructing the solution int l = n + m ; maximum possible length ; Final answers for the respective strings ; Since we have assumed the answer to be n + m long , we need to remove the extra gaps in the starting id represents the index from which the arrays xans , yans are useful ; Printing the final answer ; Driver code ; input strings ; initialising penalties of different types ; calling the function to calculate the result
using System ; class GFG { public static void getMinimumPenalty ( string x , string y , int pxy , int pgap ) { int [ , ] dp = new int [ n + m + 1 , n + m + 1 ] ; for ( int q = 0 ; q < n + m + 1 ; q ++ ) for ( int w = 0 ; w < n + m + 1 ; w ++ ) dp [ q , w ] = 0 ; for ( i = 0 ; i <= ( n + m ) ; i ++ ) { dp [ i , 0 ] = i * pgap ; dp [ 0 , i ] = i * pgap ; } for ( i = 1 ; i <= m ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { if ( x [ i - 1 ] == y [ j - 1 ] ) { dp [ i , j ] = dp [ i - 1 , j - 1 ] ; } else { dp [ i , j ] = Math . Min ( Math . Min ( dp [ i - 1 , j - 1 ] + pxy , dp [ i - 1 , j ] + pgap ) , dp [ i , j - 1 ] + pgap ) ; } } } i = m ; j = n ; int xpos = l ; int ypos = l ; int [ ] xans = new int [ l + 1 ] ; int [ ] yans = new int [ l + 1 ] ; while ( ! ( i == 0 j == 0 ) ) { if ( x [ i - 1 ] == y [ j - 1 ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; i -- ; j -- ; } else if ( dp [ i - 1 , j - 1 ] + pxy == dp [ i , j ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; i -- ; j -- ; } else if ( dp [ i - 1 , j ] + pgap == dp [ i , j ] ) { xans [ xpos -- ] = ( int ) x [ i - 1 ] ; yans [ ypos -- ] = ( int ) ' _ ' ; i -- ; } else if ( dp [ i , j - 1 ] + pgap == dp [ i , j ] ) { xans [ xpos -- ] = ( int ) ' _ ' ; yans [ ypos -- ] = ( int ) y [ j - 1 ] ; j -- ; } } while ( xpos > 0 ) { if ( i > 0 ) xans [ xpos -- ] = ( int ) x [ -- i ] ; else xans [ xpos -- ] = ( int ) ' _ ' ; } while ( ypos > 0 ) { if ( j > 0 ) yans [ ypos -- ] = ( int ) y [ -- j ] ; else yans [ ypos -- ] = ( int ) ' _ ' ; } int id = 1 ; for ( i = l ; i >= 1 ; i -- ) { if ( ( char ) yans [ i ] == ' _ ' && ( char ) xans [ i ] == ' _ ' ) { id = i + 1 ; break ; } } Console . Write ( " Minimum ▁ Penalty ▁ in ▁ aligning ▁ the ▁ genes ▁ = ▁ " + dp [ m , n ] + " STRNEWLINE " ) ; Console . Write ( " The ▁ aligned ▁ genes ▁ are ▁ : STRNEWLINE " ) ; for ( i = id ; i <= l ; i ++ ) { Console . Write ( ( char ) xans [ i ] ) ; } Console . Write ( " STRNEWLINE " ) ; for ( i = id ; i <= l ; i ++ ) { Console . Write ( ( char ) yans [ i ] ) ; } return ; } static void Main ( ) { string gene1 = " AGGGCT " ; string gene2 = " AGGCA " ; int misMatchPenalty = 3 ; int gapPenalty = 2 ; getMinimumPenalty ( gene1 , gene2 , misMatchPenalty , gapPenalty ) ; } }
Number of subsets with sum divisible by m | C # program which returns the Number of sub sequences ( or subsets ) which are divisible by m . ; Use Dynamic Programming to find sum of subsequences . ; Find sum pf array elements ; dp [ i ] [ j ] would be > 0 if arr [ 0. . i - 1 ] has a subsequence with sum equal to j . ; There is always sum equals zero ; Fill up the dp table ; Initialize the counter ; Check if the sum exists ; check sum is divisible by m ; Driver Code
using System ; class GFG { static int sumSubSequence ( int [ ] arr , int len , int m ) { int sum = 0 ; foreach ( int x in arr ) { sum += x ; } int [ , ] dp = new int [ len + 1 , sum + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) dp [ i , 0 ] ++ ; for ( int i = 1 ; i <= len ; i ++ ) { dp [ i , arr [ i - 1 ] ] ++ ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 , j ] > 0 ) { dp [ i , j ] ++ ; dp [ i , j + arr [ i - 1 ] ] ++ ; } } } int count = 0 ; for ( int j = 1 ; j <= sum ; j ++ ) if ( dp [ len , j ] > 0 ) if ( j % m == 0 ) count += dp [ len , j ] ; return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int m = 3 ; int len = arr . Length ; Console . Write ( sumSubSequence ( arr , len , m ) + " STRNEWLINE " ) ; } }
Longest Decreasing Subsequence | C # program to find the length of the longest decreasing subsequence ; Function that returns the length of the longest decreasing subsequence ; Initialize LDS with 1 for all index . The minimum LDS starting with any element is always 1 ; Compute LDS from every index in bottom up manner ; Select the maximum of all the LDS values ; returns the length of the LDS ; Driver Code
using System ; class GFG { static int lds ( int [ ] arr , int n ) { int [ ] lds = new int [ n ] ; int i , j , max = 0 ; for ( i = 0 ; i < n ; i ++ ) lds [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lds [ i ] ) max = lds [ i ] ; return max ; } public static void Main ( ) { int [ ] arr = { 15 , 27 , 14 , 38 , 63 , 55 , 46 , 65 , 85 } ; int n = arr . Length ; Console . Write ( " Length ▁ of ▁ LDS ▁ is ▁ " + lds ( arr , n ) ) ; } }
Total number of decreasing paths in a matrix | C # program to count number of decreasing path in a matrix ; Function that returns the number of decreasing paths from a cell ( i , j ) ; checking if already calculated ; all possible paths ; counts the total number of paths ; In all four allowed direction . ; new co - ordinates ; Checking if not going out of matrix and next cell value is less than current cell value . ; function that returns the answer ; Function that counts the total decreasing path in the matrix ; Initialising dp [ ] [ ] to - 1. ; Calculating number of decreasing path from each cell . ; Driver code ; function call that returns the count of decreasing paths in a matrix
using System ; class GFG { public static int CountDecreasingPathsCell ( int [ , ] mat , int [ , ] dp , int n , int x , int y ) { if ( dp [ x , y ] != - 1 ) return dp [ x , y ] ; int [ , ] delta = { { 0 , 1 } , { 1 , 0 } , { - 1 , 0 } , { 0 , - 1 } } ; int newx , newy ; int ans = 1 ; for ( int i = 0 ; i < 4 ; i ++ ) { newx = x + delta [ i , 0 ] ; newy = y + delta [ i , 1 ] ; if ( newx >= 0 && newx < n && newy >= 0 && newy < n && mat [ newx , newy ] < mat [ x , y ] ) { ans += CountDecreasingPathsCell ( mat , dp , n , newx , newy ) ; } } return dp [ x , y ] = ans ; } public static int countDecreasingPathsMatrix ( int n , int [ , ] mat ) { int [ , ] dp = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = - 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += CountDecreasingPathsCell ( mat , dp , n , i , j ) ; return sum ; } static public void Main ( ) { int n = 2 ; int [ , ] mat = { { 1 , 2 } , { 1 , 3 } } ; Console . WriteLine ( countDecreasingPathsMatrix ( n , mat ) ) ; } }
Sum of product of consecutive Binomial Coefficients | C # Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; 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 . ; finding the sum of product of consecutive coefficient . ; Driven Code
using System ; class GFG { static int MAX = 100 ; static void binomialCoeff ( int [ ] C , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } static int sumOfproduct ( int n ) { int sum = 0 ; int [ ] C = new int [ MAX ] ; binomialCoeff ( C , n ) ; for ( int i = 0 ; i <= n ; i ++ ) sum += C [ i ] * C [ i + 1 ] ; return sum ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( sumOfproduct ( n ) ) ; } }
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | C # Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driver Code
using System ; class GFG { static int MAX = 100 ; static void binomialCoeff ( int n , int [ ] C ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } static int summation ( int n ) { int [ ] C = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) C [ i ] = 0 ; binomialCoeff ( n , C ) ; int sum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) sum += ( i * C [ i ] ) ; return sum ; } public static void Main ( ) { int n = 2 ; Console . Write ( summation ( n ) ) ; } }
Number of arrays of size N whose elements are positive integers and sum is K | C # Program to find the number of arrays of size N whose elements are positive integers and sum is K ; Return nCr ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; 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 countArray ( int N , int K ) { return binomialCoeff ( K - 1 , N - 1 ) ; } static public void Main ( ) { int N = 2 , K = 3 ; Console . WriteLine ( countArray ( N , K ) ) ; } }
Partitioning into two contiguous element subarrays with equal sums | C # Program to find the minimum element to be added such that the array can be partitioned into two contiguous subarrays with equal sums ; Structure to store the minimum element and its position ; initialize prefix and suffix sum arrays with 0 ; add current element to Sum ; add current element to Sum ; initialize the minimum element to be a large value ; check for the minimum absolute difference between current prefix sum and the next suffix sum element ; if prefixsum has a greater value then position is the next element , else it 's the same element. ; return the data in struct . ; Driver Code
using System ; class GFG { public class data { public int element ; public int position ; } ; static data findMinElement ( int [ ] arr , int n ) { data result = new data ( ) ; int [ ] prefixSum = new int [ n ] ; int [ ] suffixSum = new int [ n ] ; prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; } int min = suffixSum [ 0 ] ; int pos = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( Math . Abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < min ) { min = Math . Abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) ; if ( suffixSum [ i + 1 ] < prefixSum [ i ] ) pos = i + 1 ; else pos = i ; } } result . element = min ; result . position = pos ; return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 2 , 3 , 4 } ; int n = arr . Length ; data values ; values = findMinElement ( arr , n ) ; Console . WriteLine ( " Minimum ▁ element ▁ : ▁ " + values . element + " STRNEWLINE Position ▁ : ▁ " + values . position ) ; } }