codeparrot/xlcost-text-to-code huggingface dataset:
Probability of getting at least K heads in N tosses of Coins | Dynamic and Logarithm approach find probability of at least k heads ; dp [ i ] is going to store Log ( i ! ) in base 2 ; Initialize result ; Iterate from k heads to n heads ; Preprocess all the logarithm value on base 2 ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 500 head out of 10000 coins
using System ; class GFG { static int MAX = 100001 ; static double [ ] dp = new double [ MAX ] ; static double probability ( int k , int n ) { double ans = 0.0 ; for ( int i = k ; i <= n ; ++ i ) { double res = dp [ n ] - dp [ i ] - dp [ n - i ] - n ; ans += Math . Pow ( 2.0 , res ) ; } return ans ; } static void precompute ( ) { for ( int i = 2 ; i < MAX ; ++ i ) dp [ i ] = ( Math . Log ( i ) / Math . Log ( 2 ) ) + dp [ i - 1 ] ; } public static void Main ( ) { precompute ( ) ; Console . WriteLine ( probability ( 2 , 3 ) ) ; Console . WriteLine ( probability ( 3 , 6 ) ) ; Console . WriteLine ( Math . Round ( probability ( 500 , 1000 ) , 6 ) ) ; } }
Check if all people can vote on two machines | C # program to check if all people can vote using two machines within limited time ; Returns true if n people can vote using two machines in x time . ; dp [ i ] [ j ] stores maximum possible number of people among arr [ 0. . i - 1 ] can vote in j time . ; Find sum of all times ; Fill dp [ ] [ ] in bottom up manner ( Similar to knapsack ) . ; If remaining people can go to other machine . ; Driver code
using System ; class GFG { static bool canVote ( int [ ] a , int n , int x ) { int [ , ] dp = new int [ n + 1 , x + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < x + 1 ; j ++ ) { dp [ i , j ] = 0 ; } } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 1 ; j <= x ; j ++ ) if ( a [ i ] <= j ) dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , a [ i ] + dp [ i - 1 , j - a [ i ] ] ) ; else dp [ i , j ] = dp [ i - 1 , j ] ; return ( sum - dp [ n , x ] >= x ) ; } static void Main ( ) { int n = 3 , x = 4 ; int [ ] a = { 2 , 4 , 2 } ; if ( canVote ( a , n , x ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }
Maximum path sum for each position with jumps under divisibility condition | C # program to print maximum path sum ending with each position x such that all path step positions divide x . ; Create an array such that dp [ i ] stores maximum path sum ending with i . ; Calculating maximum sum path for each element . ; Finding previous step for arr [ i ] Moving from 1 to sqrt ( i + 1 ) since all the divisors are present from sqrt ( i + 1 ) . ; Checking if j is divisor of i + 1. ; Checking which divisor will provide greater value . ; Printing the answer ( Maximum path sum ending with every position i + 1. ) ; Driver code ; Function calling
using System ; class GFG { static void printMaxSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; int maxi = 0 ; for ( int j = 1 ; j <= Math . Sqrt ( i + 1 ) ; j ++ ) { if ( ( ( i + 1 ) % j == 0 ) && ( i + 1 ) != j ) { if ( dp [ j - 1 ] > maxi ) maxi = dp [ j - 1 ] ; if ( dp [ ( i + 1 ) / j - 1 ] > maxi && j != 1 ) maxi = dp [ ( i + 1 ) / j - 1 ] ; } } dp [ i ] += maxi ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( dp [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 1 , 4 , 6 , 5 } ; int n = arr . Length ; printMaxSum ( arr , n ) ; } }
Minimum sum subsequence such that at least one of every four consecutive elements is picked | C # program to calculate minimum possible sum for given constraint ; function to calculate min sum using dp ; if elements are less than or equal to 4 ; save start four element as it is ; compute sum [ ] for all rest elements ; sum [ i ] = ar [ i ] + ( * min_element ( sum + i - 4 , sum + i ) ) ; ; Since one of the last 4 elements must be present ; Driver Code
using System ; using System . Linq ; class GFG { static int minSum ( int [ ] ar , int n ) { if ( n <= 4 ) return ar . Min ( ) ; int [ ] sum = new int [ n ] ; sum [ 0 ] = ar [ 0 ] ; sum [ 1 ] = ar [ 1 ] ; sum [ 2 ] = ar [ 2 ] ; sum [ 3 ] = ar [ 3 ] ; int [ ] tempArr ; for ( int i = 4 ; i < n ; i ++ ) { tempArr = new int [ 4 ] ; Array . Copy ( sum , i - 4 , tempArr , 0 , 4 ) ; sum [ i ] = ar [ i ] + tempArr . Min ( ) ; } tempArr = new int [ 4 ] ; Array . Copy ( sum , n - 4 , tempArr , 0 , 4 ) ; return tempArr . Min ( ) ; } public static void Main ( String [ ] args ) { int [ ] ar = { 2 , 4 , 1 , 5 , 2 , 3 , 6 , 1 , 2 , 4 } ; int n = ar . Length ; Console . WriteLine ( " Minimum ▁ sum ▁ = ▁ " + minSum ( ar , n ) ) ; } }
Maximum sum alternating subsequence | C # program to find sum of maximum sum alternating sequence starting with first element . ; Return sum of maximum sum alternating sequence starting with arr [ 0 ] and is first decreasing . ; handling the edge case ; stores sum of decreasing and increasing sub - sequence ; store sum of increasing and decreasing sun - sequence ; As per question , first element must be part of solution . ; Traverse remaining elements of array ; IF current sub - sequence is decreasing the update dec [ j ] if needed . dec [ i ] by current inc [ j ] + arr [ i ] ; Revert the flag , if first decreasing is found ; If next element is greater but flag should be 1 i . e . this element should be counted after the first decreasing element gets counted ; If current sub - sequence is increasing then update inc [ i ] ; find maximum sum in b / w inc [ ] and dec [ ] ; return maximum sum alternate sun - sequence ; Driver Method
using System ; class GFG { static int maxAlternateSum ( int [ ] arr , int n ) { if ( n == 1 ) return arr [ 0 ] ; int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( min > arr [ i ] ) min = arr [ i ] ; } if ( min == arr [ 0 ] ) { return arr [ 0 ] ; } int [ ] dec = new int [ n ] ; int [ ] inc = new int [ n ] ; dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { dec [ i ] = Math . Max ( dec [ i ] , inc [ j ] + arr [ i ] ) ; flag = 1 ; } else if ( arr [ j ] < arr [ i ] && flag == 1 ) inc [ i ] = Math . Max ( inc [ i ] , dec [ j ] + arr [ i ] ) ; } } int result = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( result < inc [ i ] ) result = inc [ i ] ; if ( result < dec [ i ] ) result = dec [ i ] ; } return result ; } public static void Main ( ) { int [ ] arr = { 8 , 2 , 3 , 5 , 7 , 9 , 10 } ; Console . Write ( " Maximum ▁ sum ▁ = ▁ " + maxAlternateSum ( arr , arr . Length ) ) ; } }
Padovan Sequence | C # program to find n 'th term in Padovan Sequence using Dynamic Programming ; Function to calculate padovan number P ( n ) ; 0 th , 1 st and 2 nd number of the series are 1 ; Driver Program
using System ; class GFG { static int pad ( int n ) { int pPrevPrev = 1 , pPrev = 1 , pCurr = 1 , pNext = 1 ; for ( int i = 3 ; i <= n ; i ++ ) { pNext = pPrevPrev + pPrev ; pPrevPrev = pPrev ; pPrev = pCurr ; pCurr = pNext ; } return pNext ; } public static void Main ( ) { int n = 12 ; Console . WriteLine ( pad ( n ) ) ; } }
Find length of the largest region in Boolean Matrix | C # program to find the length of the largest region in boolean 2D - matrix ; A function to check if a given cell ( row , col ) can be included in DFS ; row number is in range , column number is in range and value is 1 and not yet visited ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; These arrays are used to get row and column numbers of 8 neighbours of a given cell ; Mark this cell as visited ; Recur for all connected neighbours ; increment region length by one ; The main function that returns largest length region of a given boolean 2D matrix ; Make a boolean array to mark visited cells . Initially all cells are unvisited ; Initialize result as 0 and traverse through the all cells of given matrix ; If a cell with value 1 is not ; visited yet , then new region found ; maximum region ; Driver code ; Function call
using System ; class GFG { static int ROW , COL , count ; static Boolean isSafe ( int [ , ] M , int row , int col , Boolean [ , ] visited ) { return ( ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ( M [ row , col ] == 1 && ! visited [ row , col ] ) ) ; } static void DFS ( int [ , ] M , int row , int col , Boolean [ , ] visited ) { int [ ] rowNbr = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; int [ ] colNbr = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; visited [ row , col ] = true ; for ( int k = 0 ; k < 8 ; k ++ ) { if ( isSafe ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ) { count ++ ; DFS ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ; } } } static int largestRegion ( int [ , ] M ) { Boolean [ , ] visited = new Boolean [ ROW , COL ] ; int result = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( M [ i , j ] == 1 && ! visited [ i , j ] ) { count = 1 ; DFS ( M , i , j , visited ) ; result = Math . Max ( result , count ) ; } } } return result ; } public static void Main ( String [ ] args ) { int [ , ] M = { { 0 , 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 } } ; ROW = 4 ; COL = 5 ; Console . WriteLine ( largestRegion ( M ) ) ; } }
Lucas Numbers | Iterative C # program to find n 'th Lucas Number ; Iterative function ; declaring base values for positions 0 and 1 ; generating number ; Driver Code
using System ; class GFG { static int lucas ( int n ) { int a = 2 , b = 1 , c , i ; if ( n == 0 ) return a ; for ( i = 2 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return b ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( lucas ( n ) ) ; } }
Recursively break a number in 3 parts to get maximum sum | A simple recursive C # program to find maximum sum by recursively breaking a number in 3 parts . ; Function to find the maximum sum ; base conditions ; Fill in bottom - up manner using recursive formula . ; Driver program to test the above function
using System ; class GFG { static int breakSum ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = Math . Max ( dp [ i / 2 ] + dp [ i / 3 ] + dp [ i / 4 ] , i ) ; return dp [ n ] ; } public static void Main ( ) { int n = 24 ; Console . WriteLine ( breakSum ( n ) ) ; } }
Longest repeating and non | C # program to find the longest repeated non - overlapping substring ; Returns the longest repeating non - overlapping substring in str ; building table in bottom - up manner ; ( j - i ) > LCSRe [ i - 1 ] [ j - 1 ] to remove overlapping ; updating maximum length of the substring and updating the finishing index of the suffix ; If we have non - empty result , then insert all characters from first character to last character of String ; Driver program to test the above function
using System ; public class GFG { static String longestRepeatedSubstring ( String str ) { int n = str . Length ; int [ , ] LCSRe = new int [ n + 1 , n + 1 ] ; int i , index = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && LCSRe [ i - 1 , j - 1 ] < ( j - i ) ) { LCSRe [ i , j ] = LCSRe [ i - 1 , j - 1 ] + 1 ; if ( LCSRe [ i , j ] > res_length ) { res_length = LCSRe [ i , j ] ; index = Math . Max ( i , index ) ; } } else { LCSRe [ i , j ] = 0 ; } } } if ( res_length > 0 ) { for ( i = index - res_length + 1 ; i <= index ; i ++ ) { res += str [ i - 1 ] ; } } return res ; } public static void Main ( ) { String str = " geeksforgeeks " ; Console . WriteLine ( longestRepeatedSubstring ( str ) ) ; } }
Minimum cost to fill given weight in a bag | C # program to find minimum cost to get exactly W Kg with given packets ; Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver code
using System ; class GFG { static int minCost ( int [ ] cost , int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int min_cost = Int32 . MaxValue ; for ( int j = 0 ; j < i ; j ++ ) if ( j < n && cost [ j ] != - 1 ) min_cost = Math . Min ( min_cost , cost [ j ] + dp [ i - j - 1 ] ) ; dp [ i ] = min_cost ; } return dp [ n ] ; } static void Main ( ) { int [ ] cost = { 20 , 10 , 4 , 50 , 100 } ; int W = cost . Length ; Console . Write ( minCost ( cost , W ) ) ; } }
Printing Maximum Sum Increasing Subsequence | Dynamic Programming solution to construct Maximum Sum Increasing Subsequence ; Utility function to calculate sum of all vector elements ; Function to construct Maximum Sum Increasing Subsequence ; L [ i ] - The Maximum Sum Increasing Subsequence that ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; for every j less than i ; * L [ i ] = { MaxSum ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores Maximum Sum Increasing Subsequence of arr [ 0. . i ] that ends with arr [ i ] ; find max ; max will contain result ; Driver Code ; construct and print Max Sum IS of arr
using System ; using System . Collections . Generic ; class GFG { static int findSum ( List < int > arr ) { int sum = 0 ; foreach ( int i in arr ) sum += i ; return sum ; } static void printMaxSumIs ( int [ ] arr , int n ) { List < int > [ ] L = new List < int > [ n ] ; for ( int i = 0 ; i < n ; i ++ ) L [ i ] = new List < int > ( ) ; L [ 0 ] . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( findSum ( L [ i ] ) < findSum ( L [ j ] ) ) ) { foreach ( int k in L [ j ] ) if ( ! L [ i ] . Contains ( k ) ) L [ i ] . Add ( k ) ; } } L [ i ] . Add ( arr [ i ] ) ; } List < int > res = new List < int > ( L [ 0 ] ) ; foreach ( List < int > x in L ) if ( findSum ( x ) > findSum ( res ) ) res = x ; foreach ( int i in res ) Console . Write ( i + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 6 , 4 , 5 , 1 } ; int n = arr . Length ; printMaxSumIs ( arr , n ) ; } }
Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence | Dynamic Programming solution to construct Longest Increasing Subsequence ; Utility function to print LIS ; Function to construct and print Longest Increasing Subsequence ; L [ i ] - The longest increasing sub - sequence ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; do for every j less than i ; L [ i ] = { Max ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores increasing sub - sequence of arr [ 0. . i ] that ends with arr [ i ] ; LIS will be max of all increasing sub - sequences of arr ; max will contain LIS ; Driver code ; construct and print LIS of arr
using System ; using System . Collections . Generic ; class GFG { static void printLIS ( List < int > arr ) { foreach ( int x in arr ) { Console . Write ( x + " ▁ " ) ; } Console . WriteLine ( ) ; } static void constructPrintLIS ( int [ ] arr , int n ) { List < List < int > > L = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { L . Add ( new List < int > ( ) ) ; } L [ 0 ] . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( L [ i ] . Count < L [ j ] . Count + 1 ) ) L [ i ] = L [ j ] ; } L [ i ] . Add ( arr [ i ] ) ; } List < int > max = L [ 0 ] ; foreach ( List < int > x in L ) { if ( x . Count > max . Count ) { max = x ; } } printLIS ( max ) ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 4 , 5 , 1 } ; int n = arr . Length ; constructPrintLIS ( arr , n ) ; } }
Find if string is K | C # program to find if given String is K - Palindrome or not ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 , n + 1 ] in bottom up fashion . Note that L [ i , j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m , n ] contains length of LCS for X and Y ; find if given String is K - Palindrome or not ; Find reverse of String ; find longest palindromic subsequence of given String ; If the difference between longest palindromic subsequence and the original String is less than equal to k , then the String is k - palindrome ; Swap values of left and right ; Driver code
using System ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i , j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; } else { L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } } return L [ m , n ] ; } static bool isKPal ( String str , int k ) { int n = str . Length ; str = reverse ( str ) ; int lps = lcs ( str , str , n , n ) ; return ( n - lps <= k ) ; } 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 [ ] args ) { String str = " abcdeca " ; int k = 2 ; if ( isKPal ( str , k ) ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No " ) ; } }
Wildcard Pattern Matching | C # program to implement wildcard pattern matching algorithm ; Function that matches input str with given wildcard pattern ; empty pattern can only match with empty string ; lookup table for storing results of subproblems ; initialize lookup table to false ; empty pattern can match with empty string ; Only ' * ' can match with empty string ; fill the table in bottom - up fashion ; Two cases if we see a ' * ' a ) We ignore ' * ' ' ▁ character ▁ and ▁ move ▁ to ▁ next ▁ character ▁ in ▁ the ▁ pattern , ▁ i . e . , ▁ ' * ' ▁ indicates ▁ an ▁ empty ▁ sequence . ▁ b ) ▁ ' * ' character matches with ith  character in input ; Current characters are considered as matching in two cases ( a ) current character of pattern is ' ? ' ( b ) characters actually match ; If characters don 't match ; Driver Code ; String pattern = " ba * * * * * ab " ; String pattern = " ba * ab " ; String pattern = " a * ab " ; String pattern = " a * * * * * ab " ; String pattern = " * a * * * * * ab " ; String pattern = " ba * ab * * * * " ; String pattern = " * * * * " ; String pattern = " * " ; String pattern = " aa ? ab " ; String pattern = " b * b " ; String pattern = " a * a " ; String pattern = " baaabab " ; String pattern = " ? baaabab " ; String pattern = " * baaaba * " ;
using System ; class GFG { static Boolean strmatch ( String str , String pattern , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; Boolean [ , ] lookup = new Boolean [ n + 1 , m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < m + 1 ; j ++ ) lookup [ i , j ] = false ; lookup [ 0 , 0 ] = true ; for ( int j = 1 ; j <= m ; j ++ ) if ( pattern [ j - 1 ] == ' * ' ) lookup [ 0 , j ] = lookup [ 0 , j - 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( pattern [ j - 1 ] == ' * ' ) lookup [ i , j ] = lookup [ i , j - 1 ] || lookup [ i - 1 , j ] ; else if ( pattern [ j - 1 ] == ' ? ' str [ i - 1 ] == pattern [ j - 1 ] ) lookup [ i , j ] = lookup [ i - 1 , j - 1 ] ; else lookup [ i , j ] = false ; } } return lookup [ n , m ] ; } public static void Main ( String [ ] args ) { String str = " baaabab " ; String pattern = " * * * * * ba * * * * * ab " ; if ( strmatch ( str , pattern , str . Length , pattern . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find if string is K | A Naive recursive C # program to find if given string is K - Palindrome or not ; find if given string is K - Palindrome or not ; If first string is empty , the only option is to remove all characters of second string ; If second string is empty , the only option is to remove all characters of first string ; If last characters of two strings are same , ignore last characters and get count for remaining strings . ; If last characters are not same , 1. Remove last char from str1 and recur for m - 1 and n 2. Remove last char from str2 and recur for m and n - 1 Take minimum of above two operations return 1 + Math . Min ( isKPalRec ( str1 , str2 , m - 1 , n ) , Remove from str1 isKPalRec ( str1 , str2 , m , n - 1 ) ) ; Remove from str2 ; Returns true if str is k palindrome . ; Swap values of left and right ; Driver code
using System ; class GFG { static int isKPalRec ( String str1 , String str2 , int m , int n ) { if ( m == 0 ) { return n ; } if ( n == 0 ) { return m ; } if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) { return isKPalRec ( str1 , str2 , m - 1 , n - 1 ) ; } } static bool isKPal ( String str , int k ) { String revStr = str ; revStr = reverse ( revStr ) ; int len = str . Length ; return ( isKPalRec ( str , revStr , len , len ) <= k * 2 ) ; } 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 [ ] args ) { String str = " acdcb " ; int k = 2 ; if ( isKPal ( str , k ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Minimum time to finish tasks without skipping two consecutive | C # program to find minimum time to finish tasks such that no two consecutive tasks are skipped . ; arr [ ] represents time taken by n given tasks ; Corner Cases ; Initialize value for the case when there is only one task in task list . First task is included ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included . There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code
using System ; class GFG { static int minTime ( int [ ] arr , int n ) { if ( n <= 0 ) return 0 ; int incl = arr [ 0 ] ; int excl = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int incl_new = arr [ i ] + Math . Min ( excl , incl ) ; int excl_new = incl ; incl = incl_new ; excl = excl_new ; } return Math . Min ( incl , excl ) ; } public static void Main ( ) { int [ ] arr1 = { 10 , 5 , 2 , 7 , 10 } ; int n1 = arr1 . Length ; Console . WriteLine ( minTime ( arr1 , n1 ) ) ; int [ ] arr2 = { 10 , 5 , 7 , 10 } ; int n2 = arr2 . Length ; Console . WriteLine ( minTime ( arr2 , n2 ) ) ; int [ ] arr3 = { 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 } ; int n3 = arr3 . Length ; Console . WriteLine ( minTime ( arr3 , n3 ) ) ; } }