codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of operations required to sum to binary string S | C # program to find the minimum number of operations required to sum to N ; Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 , 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i , 1 ] ; Transition from dp [ i - 1 , 0 ] ; 1. Transition from dp [ i - 1 , 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 , 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 , 1 ] ; 1. Transition from dp [ i - 1 , 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 , 0 ] by just doing 1 extra operation of adding 2 ^ i ; Swap values of left and right ; Driver Code
using System ; class GFG { static int findMinOperations ( String S ) { S = reverse ( S ) ; int n = S . Length ; int [ , ] dp = new int [ n + 1 , 2 ] ; if ( S [ 0 ] == '0' ) { dp [ 0 , 0 ] = 0 ; } else { dp [ 0 , 0 ] = 1 ; } dp [ 0 , 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ i ] == '0' ) { dp [ i , 0 ] = dp [ i - 1 , 0 ] ; dp [ i , 1 ] = 1 + Math . Min ( dp [ i - 1 , 1 ] , dp [ i - 1 , 0 ] ) ; } else { dp [ i , 1 ] = dp [ i - 1 , 1 ] ; dp [ i , 0 ] = 1 + Math . Min ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) ; } } return dp [ n - 1 , 0 ] ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( ) { String S = "100" ; Console . WriteLine ( findMinOperations ( S ) ) ; S = "111" ; Console . WriteLine ( findMinOperations ( S ) ) ; } }
K | C # implementation of above approach ; Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1' ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Main Method ; get the Nth value of K - Fibonacci series
using System ; class GFG { public static void solve ( int N , int K ) { int [ ] Array = new int [ N + 1 ] ; if ( N <= K ) { Console . WriteLine ( "1" ) ; return ; } int i = 0 ; int sum = K ; for ( i = 1 ; i <= K ; ++ i ) { Array [ i ] = 1 ; } Array [ i ] = sum ; for ( i = K + 2 ; i <= N ; ++ i ) { Array [ i ] = sum - Array [ i - K - 1 ] + Array [ i - 1 ] ; sum = Array [ i ] ; } Console . WriteLine ( Array [ N ] ) ; } public static void Main ( string [ ] args ) { int N = 4 , K = 2 ; solve ( N , K ) ; } }
Minimum sum possible of any bracket sequence of length N | C # program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; / Not a proper bracket expression ; If reaches at end ; / If proper bracket expression ; else if not , return max ; If already visited ; To find out minimum sum ; Driver Code
using System ; class GFG { public static int MAX_VAL = 10000000 ; public static int [ , ] dp = new int [ 100 , 100 ] ; public static int find ( int index , int openbrk , int n , int [ , ] adj ) { if ( openbrk < 0 ) return MAX_VAL ; if ( index == n ) { if ( openbrk == 0 ) { return 0 ; } return MAX_VAL ; } if ( dp [ index , openbrk ] != - 1 ) return dp [ index , openbrk ] ; dp [ index , openbrk ] = Math . Min ( adj [ index , 1 ] + find ( index + 1 , openbrk + 1 , n , adj ) , adj [ index , 0 ] + find ( index + 1 , openbrk - 1 , n , adj ) ) ; return dp [ index , openbrk ] ; } static void Main ( ) { int n = 4 ; int [ , ] adj = new int [ , ] { { 5000 , 3000 } , { 6000 , 2000 } , { 8000 , 1000 } , { 9000 , 6000 } } ; for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 100 ; j ++ ) dp [ i , j ] = - 1 ; Console . Write ( find ( 1 , 1 , n , adj ) + adj [ 0 , 1 ] + " STRNEWLINE " ) ; } }
Understanding The Coin Change Problem With Dynamic Programming | We have input values of N and an array Coins that holds all of the coins . We use data type of long because we want to be able to test large values without integer overflow ; Create the ways array to 1 plus the amount to stop overflow ; Set the first way to 1 because its 0 and there is 1 way to make 0 with 0 coins ; Go through all of the coins ; Make a comparison to each index value of ways with the coin value . ; Update the ways array ; return the value at the Nth position of the ways array . ; Driver code
using System ; public class getWays { static long getNumberOfWays ( long N , long [ ] Coins ) { long [ ] ways = new long [ ( int ) N + 1 ] ; ways [ 0 ] = 1 ; for ( int i = 0 ; i < Coins . Length ; i ++ ) { for ( int j = 0 ; j < ways . Length ; j ++ ) { if ( Coins [ i ] <= j ) { ways [ j ] += ways [ ( int ) ( j - Coins [ i ] ) ] ; } } } return ways [ ( int ) N ] ; } static void printArray ( long [ ] coins ) { foreach ( long i in coins ) Console . WriteLine ( i ) ; } public static void Main ( String [ ] args ) { long [ ] Coins = { 1 , 5 , 10 } ; Console . WriteLine ( " The ▁ Coins ▁ Array : " ) ; printArray ( Coins ) ; Console . WriteLine ( " Solution : " ) ; Console . WriteLine ( getNumberOfWays ( 12 , Coins ) ) ; } }
Minimum cost to buy N kilograms of sweet for M persons | C # program to minimum cost to buy N kilograms of sweet for M persons ; Function to find the minimum cost of sweets ; Defining the sweet array ; DP array to store the values ; Since index starts from 1 we reassign the array into sweet ; Assigning base cases for dp array ; At 0 it is free ; Package not available for desirable amount of sweets ; Buying the ' k ' kg package and assigning it to dp array ; If no solution , select from previous k - 1 packages ; If solution does not exist ; Print the solution ; Driver code ; Calling the desired function
using System ; class GFG { static int find ( int m , int n , int [ ] adj ) { int [ ] sweet = new int [ n + 1 ] ; int [ , , ] dp = new int [ n + 1 , n + 1 , n + 1 ] ; sweet [ 0 ] = 0 ; for ( int i = 1 ; i <= m ; ++ i ) sweet [ i ] = adj [ i - 1 ] ; for ( int i = 0 ; i <= m ; ++ i ) { for ( int k = 0 ; k <= n ; ++ k ) dp [ i , 0 , k ] = 0 ; for ( int k = 1 ; k <= n ; ++ k ) dp [ i , k , 0 ] = - 1 ; } for ( int i = 0 ; i <= m ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { for ( int k = 1 ; k <= n ; ++ k ) { dp [ i , j , k ] = - 1 ; if ( i > 0 && j >= k && sweet [ k ] > 0 && dp [ i - 1 , j - k , k ] != - 1 ) dp [ i , j , k ] = dp [ i - 1 , j - k , k ] + sweet [ k ] ; if ( dp [ i , j , k ] == - 1 || ( dp [ i , j , k - 1 ] != - 1 && dp [ i , j , k ] > dp [ i , j , k - 1 ] ) ) dp [ i , j , k ] = dp [ i , j , k - 1 ] ; } } } if ( dp [ m , n , n ] == - 1 ) return 0 ; else return dp [ m , n , n ] ; } public static void Main ( ) { int m = 3 ; int [ ] adj = { 2 , 1 , 3 , 0 , 4 , 10 } ; int n = adj . Length ; Console . Write ( find ( m , n , adj ) ) ; } }
Maximum length of segments of 0 ' s ▁ and ▁ 1' s | C # implementation of above approach ; Recursive Function to find total length of the array Where 1 is greater than zero ; If reaches till end ; If dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for start index ; Driver Code ; Size of string ; Calling the function to find the value of function
using System ; class GFG { public static int find ( int start , string adj , int n , int [ ] dp ) { if ( start == n ) return 0 ; if ( dp [ start ] != - 1 ) return dp [ start ] ; dp [ start ] = 0 ; int one = 0 , zero = 0 , k ; for ( k = start ; k < n ; k ++ ) { if ( adj [ k ] == '1' ) one ++ ; else zero ++ ; if ( one > zero ) dp [ start ] = Math . Max ( dp [ start ] , find ( k + 1 , adj , n , dp ) + k - start + 1 ) ; else dp [ start ] = Math . Max ( dp [ start ] , find ( k + 1 , adj , n , dp ) ) ; } return dp [ start ] ; } static void Main ( ) { string adj = "100110001010001" ; int n = adj . Length ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = - 1 ; Console . Write ( find ( 0 , adj , n , dp ) + " STRNEWLINE " ) ; } }
Length of longest common subsequence containing vowels | C # implementation to find the length of longest common subsequence which contains all vowel characters ; function to check whether ' ch ' is a vowel or not ; Utility max ; function to find the length of longest common subsequence which contains all vowel characters ; 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 ] which contains all vowel characters ; Driver Code
using System ; class GFG { static int isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return 1 ; return 0 ; } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int lcs ( String X , String Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; int i , j ; for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( ( X [ i - 1 ] == Y [ j - 1 ] ) && isVowel ( X [ i - 1 ] ) == 1 ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ m , n ] ; } static public void Main ( String [ ] args ) { String X = " aieef " ; String Y = " klaief " ; int m = X . Length ; int n = Y . Length ; Console . WriteLine ( " Length ▁ of ▁ LCS ▁ = ▁ " + lcs ( X , Y , m , n ) ) ; } }
Minimum number of single digit primes required whose sum is equal to N | C # program to find the minimum number of single digit prime numbers required which when summed equals to a given number N . ; function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number N . ; Not possible ; Driver Code
using System ; class GFG { static Boolean check ( int i , int val ) { if ( i - val < 0 ) return false ; else return true ; } static double MinimumPrimes ( int n ) { double [ ] dp ; dp = new double [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] = 1e9 ; dp [ 0 ] = dp [ 2 ] = dp [ 3 ] = dp [ 5 ] = dp [ 7 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( check ( i , 2 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 2 ] ) ; if ( check ( i , 3 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 3 ] ) ; if ( check ( i , 5 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 5 ] ) ; if ( check ( i , 7 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 7 ] ) ; } if ( dp [ n ] == ( 1e9 ) ) return - 1 ; else return dp [ n ] ; } public static void Main ( String [ ] args ) { int n = 12 ; int minimal = ( int ) MinimumPrimes ( n ) ; if ( minimal != - 1 ) Console . WriteLine ( " Minimum ▁ number ▁ of ▁ single ▁ " + " digit ▁ primes ▁ required : ▁ " + minimal ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; } }
Find all distinct subset ( or subsequence ) sums of an array | Set | C # program to find total sum of all distinct subset sums in O ( sum ) space . ; Function to print all th distinct sum ; Declare a boolean array of size equal to total sum of the array ; Fill the first row beforehand ; dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false ; Iterate from maxSum to 1 and avoid lookup on any other row ; Do not change the dp array for j less than arr [ i ] ; If dp [ j ] is true then print ; Function to find the total sum and print the distinct sum ; find the sum of array elements ; Function to print all the distinct sum ; Driver Code
using System ; class GFG { static void subsetSum ( int [ ] arr , int n , int maxSum ) { bool [ ] dp = new bool [ maxSum + 1 ] ; Array . Fill ( dp , false ) ; dp [ arr [ 0 ] ] = true ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = maxSum ; j >= 1 ; j -- ) { if ( arr [ i ] <= j ) { if ( arr [ i ] == j || dp [ j ] || dp [ ( j - arr [ i ] ) ] ) dp [ j ] = true ; else dp [ j ] = false ; } } } Console . Write ( 0 + " ▁ " ) ; for ( int j = 0 ; j < maxSum + 1 ; j ++ ) { if ( dp [ j ] == true ) Console . Write ( j + " ▁ " ) ; } Console . Write ( "21" ) ; } static void printDistinct ( int [ ] a , int n ) { int maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxSum += a [ i ] ; } subsetSum ( a , n , maxSum ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistinct ( arr , 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 ) | recursive approach ; base condition ; reaches the point ; 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 ; public static int maxCost ( int [ , ] a , int m , int n ) { if ( n < 0 m < 0 ) return - 1 ; else if ( m == 0 && n == 0 ) return 0 ; else { int num = m + n ; if ( ( num & ( num - 1 ) ) == 0 ) return a [ m , n ] + maxCost ( a , m - 1 , n - 1 ) ; else return a [ m , n ] + Math . Max ( maxCost ( a , m - 1 , n ) , maxCost ( a , m , n - 1 ) ) ; } } public static int answer ( int [ , ] a , int n ) { return maxCost ( a , n - 1 , n - 1 ) ; } static void Main ( ) { int [ , ] a = new int [ , ] { { 1 , 2 , 3 , 1 } , { 4 , 5 , 6 , 1 } , { 7 , 8 , 9 , 1 } , { 1 , 1 , 1 , 1 } } ; int n = 4 ; Console . Write ( answer ( a , n ) ) ; } }
Longest Common Subsequence | DP using Memoization | C # program to memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Driver Code ; assign - 1 to all positions
using System ; class GFG { static readonly int maximum = 1000 ; static int lcs ( String X , String Y , int m , int n , int [ , ] dp ) { if ( m == 0 n == 0 ) { return 0 ; } if ( dp [ m - 1 , n - 1 ] != - 1 ) { return dp [ m - 1 , n - 1 ] ; } if ( X [ m - 1 ] == Y [ n - 1 ] ) { dp [ m - 1 , n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) ; return dp [ m - 1 , n - 1 ] ; } else { dp [ m - 1 , n - 1 ] = Math . Max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) ; return dp [ m - 1 , n - 1 ] ; } } public static void Main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; int m = X . Length ; int n = Y . Length ; int [ , ] dp = new int [ m , maximum ] ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < maximum ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( " Length ▁ of ▁ LCS : ▁ " + lcs ( X , Y , m , n , dp ) ) ; } }
Stepping Numbers | A C # program to find all the Stepping Numbers in range [ n , m ] using DFS Approach ; Method display 's all the stepping numbers  in range [n, m] ; If Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater than m then return ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Prints all stepping numbers in range [ n , m ] using DFS . ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
using System ; public class GFG { static void dfs ( int n , int m , int stepNum ) { if ( stepNum <= m && stepNum >= n ) Console . Write ( stepNum + " ▁ " ) ; if ( stepNum == 0 stepNum > m ) return ; int lastDigit = stepNum % 10 ; int stepNumA = stepNum * 10 + ( lastDigit - 1 ) ; int stepNumB = stepNum * 10 + ( lastDigit + 1 ) ; if ( lastDigit == 0 ) dfs ( n , m , stepNumB ) ; else if ( lastDigit == 9 ) dfs ( n , m , stepNumA ) ; else { dfs ( n , m , stepNumA ) ; dfs ( n , m , stepNumB ) ; } } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = 0 ; i <= 9 ; i ++ ) dfs ( n , m , i ) ; } static public void Main ( ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }
Memoization ( 1D , 2D and 3D ) | C # program to memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Utility function to get max of 2 integers ; Driver code
using System ; public class GFG { public static int [ , ] arr = new int [ 1000 , 1000 ] ; public static int lcs ( String X , String Y , int m , int n ) { if ( m == 0 n == 0 ) return 0 ; if ( arr [ m - 1 , n - 1 ] != - 1 ) return arr [ m - 1 , n - 1 ] ; if ( X [ m - 1 ] == Y [ n - 1 ] ) { arr [ m - 1 , n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 ) ; return arr [ m - 1 , n - 1 ] ; } else { arr [ m - 1 , n - 1 ] = max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; return arr [ m - 1 , n - 1 ] ; } } public static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static public void Main ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) { for ( int j = 0 ; j < 1000 ; j ++ ) { arr [ i , j ] = - 1 ; } } String X = " AGGTAB " ; String Y = " GXTXAYB " ; int m = X . Length ; int n = Y . Length ; Console . WriteLine ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , m , n ) ) ; } }
Number of Unique BST with a given key | Dynamic Programming | C # code to find number of unique BSTs Dynamic Programming solution ; DP to store the number of unique BST with key i ; Base case ; fill the dp table in top - down approach . ; n - i in right * i - 1 in left ; Driver Code
using System ; class GFG { static int numberOfBST ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { dp [ i ] = dp [ i ] + ( dp [ i - j ] * dp [ j - 1 ] ) ; } } return dp [ n ] ; } public static void Main ( ) { int n = 3 ; Console . Write ( " Number ▁ of ▁ structurally ▁ " + " Unique ▁ BST ▁ with ▁ " + n + " ▁ keys ▁ are ▁ : ▁ " + numberOfBST ( n ) ) ; } }
Minimum splits in a binary string such that every substring is a power of 4 or 6. | C # program for Minimum splits in a string such that substring is a power of 4 or 6. ; Function to find if given number is power of another number or not . ; Divide given number repeatedly by base value . ; return false ; not a power ; Function to find minimum number of partitions of given binary string so that each partition is power of 4 or 6. ; Variable to store integer value of given binary string partition . ; DP table to store results of partitioning done at differentindices . ; If the last digit is 1 , hence 4 ^ 0 = 1 and 6 ^ 0 = 1 ; Fix starting position for partition ; Binary representation with leading zeroes is not allowed . ; Iterate for all different partitions starting from i ; Find integer value of current binary partition . ; Check if the value is a power of 4 or 6 or not apply recurrence relation ; If no partitions are possible , then make dp [ i ] = - 1 to represent this . ; Driver code
using System ; class GFG { static bool isPowerOf ( long val , int b ) { while ( val > 1 ) { if ( val % b != 0 ) val /= b ; } return true ; } static int numberOfPartitions ( string binaryNo ) { int i , j , n = binaryNo . Length ; long val ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = ( ( ( binaryNo [ n - 1 ] - '0' ) == 0 ) ? - 1 : 1 ) ; for ( i = n - 2 ; i >= 0 ; i -- ) { val = 0 ; if ( ( binaryNo [ i ] - '0' ) == 0 ) { dp [ i ] = - 1 ; continue ; } dp [ i ] = int . MaxValue ; for ( j = i ; j < n ; j ++ ) { val = ( val * 2 ) + ( long ) ( binaryNo [ j ] - '0' ) ; if ( isPowerOf ( val , 4 ) || isPowerOf ( val , 6 ) ) { if ( j == n - 1 ) { dp [ i ] = 1 ; } else { if ( dp [ j + 1 ] != - 1 ) dp [ i ] = Math . Min ( dp [ i ] , dp [ j + 1 ] + 1 ) ; } } } if ( dp [ i ] == int . MaxValue ) dp [ i ] = - 1 ; } return dp [ 0 ] ; } public static void Main ( ) { string binaryNo = "100110110" ; Console . Write ( numberOfPartitions ( binaryNo ) ) ; } }
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 summation of r * nCr ; Driver Code
using System ; class GFG { static int summation ( int n ) { return n << ( n - 1 ) ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( summation ( n ) ) ; } }
Maximum average sum partition of an array | C # program for maximum average sum partition ; bottom up approach to calculate score ; storing averages from starting to each i ; ; Driver code ; atmost partitioning size
using System ; using System . Collections . Generic ; class GFG { static int MAX = 1000 ; static double [ , ] memo = new double [ MAX , MAX ] ; public static double score ( int n , List < int > A , int k ) { if ( memo [ n , k ] > 0 ) return memo [ n , k ] ; double sum = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { sum += A [ i ] ; memo [ n , k ] = Math . Max ( memo [ n , k ] , score ( i , A , k - 1 ) + sum / ( n - i ) ) ; } return memo [ n , k ] ; } public static double largestSumOfAverages ( List < int > A , int K ) { int n = A . Count ; double sum = 0 ; for ( int i = 0 ; i < memo . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < memo . GetLength ( 1 ) ; j ++ ) memo [ i , j ] = 0.0 ; } for ( int i = 0 ; i < n ; i ++ ) { sum += A [ i ] ; memo [ i + 1 , 1 ] = sum / ( i + 1 ) ; } return score ( n , A , K ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 1 , 2 , 3 , 9 } ; List < int > A = new List < int > ( arr ) ; int K = 3 ; } Console . WriteLine ( largestSumOfAverages ( A , K ) ) ; }