codeparrot/xlcost-text-to-code huggingface dataset:
Number of circular tours that visit all petrol pumps | C # Program to find the number of circular tour that visits all petrol pump ; Return the number of pumps from where we can start the journey . ; Making Circular Array . ; for each of the petrol pump . ; If tank is less than 0. ; If starting pump is greater than n , return ans as 0. ; For each of the petrol pump ; Finding the need array ; If need is 0 , increment the count . ; Drivers code
using System ; class GFG { static int N = 100 ; public static int count ( int n , int c , int [ ] a , int [ ] b ) { int [ ] need = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } int s = 0 ; int tank = 0 ; for ( int i = 0 ; i < 2 * n ; i ++ ) { tank += a [ i ] ; tank = Math . Min ( tank , c ) ; tank -= b [ i ] ; if ( tank < 0 ) { tank = 0 ; s = i + 1 ; } } if ( s >= n ) return 0 ; int ans = 1 ; need [ s + n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int id = s + n - i ; need [ id ] = Math . Max ( 0 , need [ id + 1 ] + b [ id ] - Math . Min ( a [ id ] , c ) ) ; if ( need [ id ] == 0 ) ans ++ ; } return ans ; } static void Main ( ) { int n = 3 ; int c = 3 ; int [ ] a = new int [ 6 ] { 3 , 1 , 2 , 0 , 0 , 0 } ; int [ ] b = new int [ 6 ] { 2 , 2 , 2 , 0 , 0 , 0 } ; Console . Write ( count ( n , c , a , b ) + " STRNEWLINE " ) ; } }
Print equal sum sets of array ( Partition Problem ) | Set 2 | C # program to print equal sum sets of array . ; Function to print equal sum sets of array . ; Finding sum of array elements ; Check sum is even or odd . If odd then array cannot be partitioned . Print - 1 and return . ; Divide sum by 2 to find sum of two possible subsets . ; Boolean DP table to store result of states . dp [ i , j ] = true if there is a subset of elements in first i elements of array that has sum equal to j . ; If number of elements are zero , then no sum can be obtained . ; Sum 0 can be obtained by not selecting any element . ; Fill the DP table in bottom up manner . ; Excluding current element . ; Including current element ; Required sets set1 and set2 . ; If partition is not possible print - 1 and return . ; Start from last element in dp table . ; If current element does not contribute to k , then it belongs to set 2. ; If current element contribute to k then it belongs to set 1. ; Print elements of both the sets . ; Driver Code .
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static void printEqualSumSets ( int [ ] arr , int n ) { int i , currSum , sum = 0 ; for ( i = 0 ; i < arr . Length ; i ++ ) sum += arr [ i ] ; if ( ( sum & 1 ) == 1 ) { Console . Write ( " - 1" ) ; return ; } int k = sum >> 1 ; bool [ , ] dp = new bool [ n + 1 , k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 , i ] = false ; for ( i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( currSum = 1 ; currSum <= k ; currSum ++ ) { dp [ i , currSum ] = dp [ i - 1 , currSum ] ; if ( arr [ i - 1 ] <= currSum ) dp [ i , currSum ] = dp [ i , currSum ] | dp [ i - 1 , currSum - arr [ i - 1 ] ] ; } } List < int > set1 = new List < int > ( ) ; List < int > set2 = new List < int > ( ) ; if ( ! dp [ n , k ] ) { Console . Write ( " - 1 STRNEWLINE " ) ; return ; } i = n ; currSum = k ; while ( i > 0 && currSum >= 0 ) { if ( dp [ i - 1 , currSum ] ) { i -- ; set2 . Add ( arr [ i ] ) ; } else if ( dp [ i - 1 , currSum - arr [ i - 1 ] ] ) { i -- ; currSum -= arr [ i ] ; set1 . Add ( arr [ i ] ) ; } } Console . Write ( " Set ▁ 1 ▁ elements : ▁ " ) ; for ( i = 0 ; i < set1 . Count ; i ++ ) Console . Write ( set1 [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE Set ▁ 2 ▁ elements : ▁ " ) ; for ( i = 0 ; i < set2 . Count ; i ++ ) Console . Write ( set2 [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 5 , 5 , 1 , 11 } ; int n = arr . Length ; printEqualSumSets ( arr , n ) ; } }
Maximum average sum partition of an array | C # program for maximum average sum partition ; storing prefix sums ; for each i to n storing averages ; Driver code ; int K = 3 ; atmost partitioning size
using System ; using System . Collections . Generic ; class GFG { static double largestSumOfAverages ( int [ ] A , int K ) { int n = A . Length ; double [ ] pre_sum = new double [ n + 1 ] ; pre_sum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; double [ ] dp = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i ) ; for ( int k = 0 ; k < K - 1 ; k ++ ) for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) dp [ i ] = Math . Max ( dp [ i ] , ( pre_sum [ j ] - pre_sum [ i ] ) / ( j - i ) + dp [ j ] ) ; return dp [ 0 ] ; } public static void Main ( String [ ] args ) { int [ ] A = { 9 , 1 , 2 , 3 , 9 } ; Console . WriteLine ( largestSumOfAverages ( A , K ) ) ; } }
Find maximum sum array of length less than or equal to m | C # program to find maximum sum of array of size less than or equal to m from given n arrays ; N and M to define sizes of arr , dp , current_arr and maxSum ; INF to define min value ; Function to find maximum sum ; dp array of size N x M ; current_arr of size M ; maxsum of size M ; if we have 0 elements from 0 th array ; compute the cumulative sum array ; calculating the maximum contiguous array for every length j , j is from 1 to lengtn of the array ; every state is depending on its previous state ; computation of dp table similar approach as knapsack problem ; now we have done processing with the last array lets find out what is the maximum sum possible ; Driver Code ; first element of each row is the size of that row
using System ; class GFG { static int N = 105 ; static int M = 1001 ; static int INF = - 1111111111 ; static int maxSum ( int [ ] [ ] arr ) { int [ , ] dp = new int [ N , M ] ; int [ ] current_arr = new int [ M ] ; int [ ] maxsum = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) dp [ i , j ] = - 1 ; } current_arr [ 0 ] = 0 ; dp [ 0 , 0 ] = 0 ; for ( int i = 1 ; i <= 5 ; i ++ ) { int len = arr [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= len ; j ++ ) { current_arr [ j ] = arr [ i - 1 ] [ j ] ; current_arr [ j ] += current_arr [ j - 1 ] ; maxsum [ j ] = INF ; } for ( int j = 1 ; j <= len && j <= 6 ; j ++ ) for ( int k = 1 ; k <= len ; k ++ ) if ( j + k - 1 <= len ) maxsum [ j ] = Math . Max ( maxsum [ j ] , current_arr [ j + k - 1 ] - current_arr [ k - 1 ] ) ; for ( int j = 0 ; j <= 6 ; j ++ ) dp [ i , j ] = dp [ i - 1 , j ] ; for ( int j = 1 ; j <= 6 ; j ++ ) for ( int cur = 1 ; cur <= j && cur <= len ; cur ++ ) dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i - 1 , j - cur ] + maxsum [ cur ] ) ; } int ans = 0 ; for ( int i = 0 ; i <= 6 ; i ++ ) ans = Math . Max ( ans , dp [ 5 , i ] ) ; return ans ; } static void Main ( ) { int [ ] [ ] arr = new int [ ] [ ] { new int [ ] { 3 , 2 , 3 , 5 } , new int [ ] { 2 , 7 , - 1 } , new int [ ] { 2 , 8 , 10 } , new int [ ] { 4 , 5 , 2 , 6 , 1 } , new int [ ] { 3 , 2 , 3 , - 2 } } ; Console . Write ( " Maximum ▁ sum ▁ can ▁ be ▁ " + " obtained ▁ is ▁ : ▁ " + maxSum ( arr ) + " STRNEWLINE " ) ; } }
Maximize array elements upto given number | C # program to find maximum value of number obtained by using array elements by using dynamic programming . ; Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index 1. dp [ i , j ] = 1 if value j can be obtained upto index i . 2. dp [ i , j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is true , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code
using System ; class GFG { static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ind ; int val ; int [ , ] dp = new int [ n , maxLimit + 1 ] ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val num + arr [ ind ] == val ) { dp [ ind , val ] = 1 ; } else { dp [ ind , val ] = 0 ; } } else { if ( val - arr [ ind ] >= 0 && val + arr [ ind ] <= maxLimit ) { if ( dp [ ind - 1 , val - arr [ ind ] ] == 1 dp [ ind - 1 , val + arr [ ind ] ] == 1 ) dp [ ind , val ] = 1 ; } else if ( val - arr [ ind ] >= 0 ) { dp [ ind , val ] = dp [ ind - 1 , val - arr [ ind ] ] ; } else if ( val + arr [ ind ] <= maxLimit ) { dp [ ind , val ] = dp [ ind - 1 , val + arr [ ind ] ] ; } else { dp [ ind , val ] = 0 ; } } } } for ( val = maxLimit ; val >= 0 ; val -- ) { if ( dp [ n - 1 , val ] == 1 ) { return val ; } } return - 1 ; } static void Main ( ) { int num = 1 ; int [ ] arr = new int [ ] { 3 , 10 , 6 , 4 , 5 } ; int n = arr . Length ; int maxLimit = 15 ; Console . Write ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }
Moser | C # code to generate first ' n ' terms of the Moser - de Bruijn Sequence ; Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code
using System ; class GFG { public static int gen ( int n ) { if ( n == 0 ) return 0 ; else if ( n == 1 ) return 1 ; else if ( n % 2 == 0 ) return 4 * gen ( n / 2 ) ; else if ( n % 2 == 1 ) return 4 * gen ( n / 2 ) + 1 ; return 0 ; } public static void moserDeBruijn ( int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( gen ( i ) + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( " First ▁ " + n + " ▁ terms ▁ of ▁ " + " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ " ) ; moserDeBruijn ( n ) ; } }
Minimum Sum Path in a Triangle | C # program for Dynamic Programming implementation of Min Sum Path in a Triangle ; Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; return the top element ; Driver Code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int minSumPath ( ref List < List < int > > A ) { int [ ] memo = new int [ A . Count ] ; int n = A . Count - 1 ; for ( int i = 0 ; i < A [ n ] . Count ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( int i = A . Count - 2 ; i >= 0 ; i -- ) for ( int j = 0 ; j < A [ i + 1 ] . Count - 1 ; j ++ ) memo [ j ] = A [ i ] [ j ] + ( int ) Math . Min ( memo [ j ] , memo [ j + 1 ] ) ; return memo [ 0 ] ; } public static void Main ( ) { List < List < int > > A = new List < List < int > > ( ) ; A . Add ( new List < int > { 2 } ) ; A . Add ( new List < int > { 3 , 9 } ) ; A . Add ( new List < int > { 1 , 6 , 7 } ) ; Console . WriteLine ( minSumPath ( ref A ) ) ; } }
Check if any valid sequence is divisible by M |  ; Base case ; Check if sum is divisible by M ; 1. Try placing ' + ' ; 2. Try placing ' - '
static bool isPossible ( int index , int sum ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } bool placeAdd = isPossible ( index + 1 , sum + arr [ index ] ) ; bool placeMinus = isPossible ( index + 1 , sum - arr [ index ] ) ; if ( placeAdd placeMinus ) return true ; return false ; }
Minimum removals from array to make max | C # program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver code
using System ; class GFG { static int MAX = 100 ; static int [ , ] dp = new int [ MAX , MAX ] ; static int countRemovals ( int [ ] a , int i , int j , int k ) { if ( i >= j ) return 0 ; else if ( ( a [ j ] - a [ i ] ) <= k ) return 0 ; else if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; else if ( ( a [ j ] - a [ i ] ) > k ) { dp [ i , j ] = 1 + Math . Min ( countRemovals ( a , i + 1 , j , k ) , countRemovals ( a , i , j - 1 , k ) ) ; } return dp [ i , j ] ; } static int removals ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; } if ( n == 1 ) return 0 ; else return countRemovals ( a , 0 , n - 1 , k ) ; } static void Main ( ) { int [ ] a = new int [ ] { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = a . Length ; int k = 4 ; Console . Write ( removals ( a , n , k ) ) ; } }
Minimum removals from array to make max | C # program for the above approach ; Function to find rightmost index which satisfies the condition arr [ j ] - arr [ i ] <= k ; Initialising start to i + 1 ; Initialising end to n - 1 ; Binary search implementation to find the rightmost element that satisfy the condition ; Check if the condition satisfies ; Store the position ; Make start = mid + 1 ; Make end = mid ; Return the rightmost position ; Function to calculate minimum number of elements to be removed ; Sort the given array ; Iterate from 0 to n - 1 ; Find j such that arr [ j ] - arr [ i ] <= k ; If there exist such j that satisfies the condition ; Number of elements to be removed for this particular case is ( j - i + 1 ) ; Return answer ; Driver code
using System ; class GFG { static int findInd ( int key , int i , int n , int k , int [ ] arr ) { int start , end , mid , ind = - 1 ; start = i + 1 ; end = n - 1 ; while ( start < end ) { mid = start + ( end - start ) / 2 ; if ( arr [ mid ] - key <= k ) { ind = mid ; start = mid + 1 ; } else { end = mid ; } } return ind ; } static int removals ( int [ ] arr , int n , int k ) { int i , j , ans = n - 1 ; Array . Sort ( arr ) ; for ( i = 0 ; i < n ; i ++ ) { j = findInd ( arr [ i ] , i , n , k , arr ) ; if ( j != - 1 ) { ans = Math . Min ( ans , n - ( j - i + 1 ) ) ; } } return ans ; } static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = a . Length ; int k = 4 ; Console . Write ( removals ( a , n , k ) ) ; } }
Number of ordered pairs such that ( Ai & Aj ) = 0 | C # program to calculate the number of ordered pairs such that their bitwise and is zero ; Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) arev considered different ; Driver Code
using System ; class GFG { static int countPairs ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( a [ i ] & a [ j ] ) == 0 ) count += 2 ; } return count ; } public static void Main ( ) { int [ ] a = { 3 , 4 , 2 } ; int n = a . Length ; Console . Write ( countPairs ( a , n ) ) ; } }
Number of ordered pairs such that ( Ai & Aj ) = 0 | C # program to calculate the number of ordered pairs such that their bitwise and is zero ; Efficient function to count pairs ; Stores the frequency of each number ; Count the frequency of every element ; Iterate for al possible values that a [ i ] can be ; If the last bit is ON ; is the last bit is OFF ; Iterate till n ; If mask 's ith bit is set ; If mask 's ith bit is not set ; Iterate for all the array element and count the number of pairs ; return answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int N = 15 ; static int countPairs ( int [ ] a , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; int [ , ] dp = new int [ 1 << N , N + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash . ContainsKey ( a [ i ] ) ) { hash [ a [ i ] ] += 1 ; } else { hash . Add ( a [ i ] , 1 ) ; } } for ( int mask = 0 ; mask < ( 1 << N ) ; ++ mask ) { if ( ( mask & 1 ) != 0 ) { if ( hash . ContainsKey ( mask ) ) { dp [ mask , 0 ] = hash [ mask ] ; } if ( hash . ContainsKey ( mask ^ 1 ) ) { dp [ mask , 0 ] += hash [ mask ^ 1 ] ; } } else { if ( hash . ContainsKey ( mask ) ) { dp [ mask , 0 ] = hash [ mask ] ; } } for ( int i = 1 ; i <= N ; ++ i ) { if ( ( mask & ( 1 << i ) ) != 0 ) { dp [ mask , i ] = dp [ mask , i - 1 ] + dp [ mask ^ ( 1 << i ) , i - 1 ] ; } else { dp [ mask , i ] = dp [ mask , i - 1 ] ; } } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += dp [ ( ( 1 << N ) - 1 ) ^ a [ i ] , N ] ; } return ans ; } static void Main ( ) { int [ ] a = { 5 , 4 , 1 , 6 } ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n ) ) ; } }
Minimum sum of multiplications of n numbers | C # program to find the minimum sum of multiplication of n numbers ; Used in recursive memoized solution ; Function to calculate the cumulative sum from a [ i ] to a [ j ] ; base case ; memoization , if the partition has been called before then return the stored value ; store a max value ; we break them into k partitions ; store the min of the formula thus obtained ; return the minimum ; Driver code
using System ; class GFG { static long [ , ] dp = new long [ 1000 , 1000 ] ; static long sum ( int [ ] a , int i , int j ) { long ans = 0 ; for ( int m = i ; m <= j ; m ++ ) ans = ( ans + a [ m ] ) % 100 ; return ans ; } static long solve ( int [ ] a , int i , int j ) { if ( i == j ) return 0 ; if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; dp [ i , j ] = 100000000 ; for ( int k = i ; k < j ; k ++ ) { dp [ i , j ] = Math . Min ( dp [ i , j ] , ( solve ( a , i , k ) + solve ( a , k + 1 , j ) + ( sum ( a , i , k ) * sum ( a , k + 1 , j ) ) ) ) ; } return dp [ i , j ] ; } static void initialize ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i , j ] = - 1 ; } public static void Main ( ) { int [ ] a = { 40 , 60 , 20 } ; int n = a . Length ; initialize ( n ) ; Console . WriteLine ( solve ( a , 0 , n - 1 ) ) ; } }
Print Fibonacci Series in reverse order | C # Program to print Fibonacci series in reverse order ; assigning first and second elements ; storing sum in the preceding location ; printing array in reverse order ; Driver function
using System ; class GFG { static void reverseFibonacci ( int n ) { int [ ] a = new int [ n ] ; a [ 0 ] = 0 ; a [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { a [ i ] = a [ i - 2 ] + a [ i - 1 ] ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( ) { int n = 5 ; reverseFibonacci ( n ) ; } }
Check if it is possible to transform one string to another | C # program to check if a string can be converted to another string by performing operations ; function to check if a string can be converted to another string by performing following operations ; calculates length ; mark 1 st position as true ; traverse for all DPi , j ; if possible for to convert i characters of s1 to j characters of s2 ; if upper_case ( s1 [ i ] ) == s2 [ j ] is same ; if not upper then deletion is possible ; Driver Code
using System ; class GFG { static bool check ( string s1 , string s2 ) { int n = s1 . Length ; int m = s2 . Length ; bool [ , ] dp = new bool [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i , j ] = false ; } } dp [ 0 , 0 ] = true ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { for ( int j = 0 ; j <= s2 . Length ; j ++ ) { if ( dp [ i , j ] ) { if ( j < s2 . Length && ( Char . ToUpper ( s1 [ i ] ) == s2 [ j ] ) ) dp [ i + 1 , j + 1 ] = true ; if ( ! Char . IsUpper ( s1 [ i ] ) ) dp [ i + 1 , j ] = true ; } } } return ( dp [ n , m ] ) ; } public static void Main ( ) { string s1 = " daBcd " ; string s2 = " ABC " ; if ( check ( s1 , s2 ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Probability of reaching a point with 2 or 3 steps at a time | C # Program to find probability to reach N with P probability to take 2 steps ( 1 - P ) to take 3 steps ; Returns probability to reach N ; Driver code
using System ; class GFG { static float find_prob ( int N , float P ) { double [ ] dp = new double [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 0 ; dp [ 2 ] = P ; dp [ 3 ] = 1 - P ; for ( int i = 4 ; i <= N ; ++ i ) dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] ; return ( ( float ) ( dp [ N ] ) ) ; } public static void Main ( ) { int n = 5 ; float p = 0.2f ; Console . WriteLine ( find_prob ( n , p ) ) ; } }
Minimum number of deletions to make a string palindrome | Set 2 | C # program to find minimum deletions to make palindrome . ; Find reverse of input string ; Create a DP table for storing edit distance of string and reverse . ; Find edit distance between input and revInput considering only delete operation . ; Go from bottom left to top right and find the minimum ; Driver Code
using System ; class GFG { static int getLevenstein ( String input ) { String revInput = Reverse ( input ) ; int n = input . Length ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 , i ] = i ; dp [ i , 0 ] = i ; } for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { if ( input [ i - 1 ] == revInput [ j - 1 ] ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 1 + Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } int res = int . MaxValue ; for ( int i = n , j = 0 ; i >= 0 ; i -- , j ++ ) { res = Math . Min ( res , dp [ i , j ] ) ; if ( i < n ) res = Math . Min ( res , dp [ i + 1 , j ] ) ; if ( i > 0 ) res = Math . Min ( res , dp [ i - 1 , j ] ) ; } return res ; } static String Reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String input = " myfirstgeekarticle " ; Console . WriteLine ( getLevenstein ( input ) ) ; } }
Number of ways to form a heap with n distinct integers | C # program to count max heaps with n distinct keys ; dp [ i ] = number of max heaps for i distinct integers ; nck [ i ] [ j ] = number of ways to choose j elements form i elements , no order ; log2 [ i ] = floor of logarithm of base 2 of i ; to calculate nCk ; calculate l for give value of n ; number of elements that are actually present in last level ( hth level ) ( 2 ^ h - 1 ) ; if more than half - filled ; return ( 1 << h ) - 1 ; ( 2 ^ h ) - 1 ; find maximum number of heaps for n ; function to initialize arrays ; for each power of two find logarithm ; driver function
using System ; class GFG { static int [ ] dp = new int [ MAXN ] ; static int [ , ] nck = new int [ MAXN , MAXN ] ; static int [ ] log2 = new int [ MAXN ] ; public static int choose ( int n , int k ) { if ( k > n ) return 0 ; if ( n <= 1 ) return 1 ; if ( k == 0 ) return 1 ; if ( nck [ n , k ] != - 1 ) return nck [ n , k ] ; int answer = choose ( n - 1 , k - 1 ) + choose ( n - 1 , k ) ; nck [ n , k ] = answer ; return answer ; } public static int getLeft ( int n ) { if ( n == 1 ) return 0 ; int h = log2 [ n ] ; int last = n - ( ( 1 << h ) - 1 ) ; if ( last >= ( numh / 2 ) ) else return ( 1 << h ) - 1 - ( ( numh / 2 ) - last ) ; } public static int numberOfHeaps ( int n ) { if ( n <= 1 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int left = getLeft ( n ) ; int ans = ( choose ( n - 1 , left ) * numberOfHeaps ( left ) ) * ( numberOfHeaps ( n - 1 - left ) ) ; dp [ n ] = ans ; return ans ; } public static int solve ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = - 1 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) nck [ i , j ] = - 1 ; int currLog2 = - 1 ; int currPower2 = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( currPower2 == i ) { currLog2 ++ ; currPower2 *= 2 ; } log2 [ i ] = currLog2 ; } return numberOfHeaps ( n ) ; } static void Main ( ) { int n = 10 ; Console . Write ( solve ( n ) ) ; } }