codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum in a 2 x n grid such that no two elements are adjacent | C # program Code for Maximum sum in a 2 x n grid such that no two elements are adjacent ; Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's  element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't  to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver Code
using System ; class GFG { public static int maxSum ( int [ , ] grid , int n ) { int incl = Math . Max ( grid [ 0 , 0 ] , grid [ 1 , 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = Math . Max ( excl , incl ) ; incl = excl + Math . Max ( grid [ 0 , i ] , grid [ 1 , i ] ) ; excl = excl_new ; } return Math . Max ( excl , incl ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } } ; int n = 5 ; Console . Write ( maxSum ( grid , n ) ) ; } }
Minimum insertions to sort an array | C # program to get minimum number of insertion steps to sort an array ; method returns min steps of insertion we need to perform to sort array ' arr ' ; lis [ i ] is going to store length of lis that ends with i . ; Initialize lis values for all indexes ; Compute optimized lis values in bottom up manner ; The overall LIS must end with of the array elements . Pick maximum of all lis values ; return size of array minus length of LIS as final result ; Driver code to test above methods
using System ; class GfG { static int minInsertionStepToSortArray ( int [ ] arr , int N ) { int [ ] lis = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] >= arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int max = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return ( N - max ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 1 , 4 , 7 , 6 } ; int N = arr . Length ; Console . Write ( minInsertionStepToSortArray ( arr , N ) ) ; } }
Print all distinct characters of a string in order ( 3 Methods ) | C # program to print distinct characters of a string . ; Print duplicates present in the passed string ; Create an array of size 256 and count of every character in it ; Count array with frequency of characters ; Print characters having count more than 0 ; Driver program
using System ; public class GFG { static int NO_OF_CHARS = 256 ; static void printDistinct ( String str ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str . Length ; i ++ ) if ( str [ i ] != ' ▁ ' ) count [ ( int ) str [ i ] ] ++ ; int n = i ; for ( i = 0 ; i < n ; i ++ ) if ( count [ ( int ) str [ i ] ] == 1 ) Console . Write ( str [ i ] ) ; } public static void Main ( ) { String str = " GeeksforGeeks " ; printDistinct ( str ) ; } }
Shortest Uncommon Subsequence | A simple recursive C # program to find shortest uncommon subsequence . ; A recursive function to find the length of shortest uncommon subsequence ; S String is empty ; T String is empty ; Loop to search for current character ; char not found in T ; Return minimum of following two Not including current char in answer Including current char ; Driver code
using System ; class GFG { static readonly int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T , int m , int n ) { if ( m == 0 ) return MAX ; if ( n <= 0 ) return 1 ; int k ; for ( k = 0 ; k < n ; k ++ ) if ( T [ k ] == S [ 0 ] ) break ; if ( k == n ) return 1 ; char [ ] St1 = new Char [ S . Length - 1 ] ; Array . Copy ( S , 1 , St1 , 0 , S . Length - 1 ) ; char [ ] St2 = new Char [ S . Length - 1 ] ; Array . Copy ( S , 1 , St2 , 0 , S . Length - 1 ) ; char [ ] Tt1 = new Char [ T . Length - ( k + 1 ) ] ; Array . Copy ( T , k + 1 , Tt1 , 0 , T . Length - ( k + 1 ) ) ; return Math . Min ( shortestSeq ( St1 , T , m - 1 , n ) , 1 + shortestSeq ( St2 , Tt1 , m - 1 , n - k - 1 ) ) ; } public static void Main ( String [ ] args ) { char [ ] S = " babab " . ToCharArray ( ) ; char [ ] T = " babba " . ToCharArray ( ) ; int m = S . Length , n = T . Length ; int ans = shortestSeq ( S , T , m , n ) ; if ( ans >= MAX ) ans = - 1 ; Console . Write ( " Length ▁ of ▁ shortest ▁ subsequence ▁ is : ▁ " + ans + " STRNEWLINE " ) ; } }
Shortest Uncommon Subsequence | A dynamic programming based C # program to find shortest uncommon subsequence . ; Returns length of shortest common subsequence ; declaring 2D array of m + 1 rows and n + 1 columns dynamically ; T string is empty ; S string is empty ; char not present in T ; Driver code
using System ; class GFG { static readonly int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T ) { int m = S . Length , n = T . Length ; int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { dp [ i , 0 ] = 1 ; } for ( int i = 0 ; i <= n ; i ++ ) { dp [ 0 , i ] = MAX ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { char ch = S [ i - 1 ] ; int k ; for ( k = j - 1 ; k >= 0 ; k -- ) { if ( T [ k ] == ch ) { break ; } } if ( k == - 1 ) { dp [ i , j ] = 1 ; } else { dp [ i , j ] = Math . Min ( dp [ i - 1 , j ] , dp [ i - 1 , k ] + 1 ) ; } } } int ans = dp [ m , n ] ; if ( ans >= MAX ) { ans = - 1 ; } return ans ; } public static void Main ( String [ ] args ) { char [ ] S = " babab " . ToCharArray ( ) ; char [ ] T = " babba " . ToCharArray ( ) ; int m = S . Length , n = T . Length ; Console . WriteLine ( " Length ▁ of ▁ shortest " + " subsequence ▁ is ▁ : ▁ " + shortestSeq ( S , T ) ) ; } }
Count number of ways to jump to reach end | C # implementation to count number of ways to jump to reach end ; function to count ways to jump to reach end for each array element ; count_jump [ i ] store number of ways arr [ i ] can reach to the end ; Last element does not require to jump . Count ways to jump for remaining elements ; if the element can directly jump to the end ; add the count of all the elements that can reach to end and arr [ i ] can reach to them ; if element can reach to end then add its count to count_jump [ i ] ; if arr [ i ] cannot reach to the end ; print count_jump for each array element ; Driver code
using System ; class GFG { static void countWaysToJump ( int [ ] arr , int n ) { int [ ] count_jump = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) count_jump [ i ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= n - i - 1 ) count_jump [ i ] ++ ; for ( int j = i + 1 ; j < n - 1 && j <= arr [ i ] + i ; j ++ ) if ( count_jump [ j ] != - 1 ) count_jump [ i ] += count_jump [ j ] ; if ( count_jump [ i ] == 0 ) count_jump [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( count_jump [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 8 , 9 , 1 , 0 , 7 , 6 , 8 , 9 } ; int n = arr . Length ; countWaysToJump ( arr , n ) ; } }
Minimum steps to delete a string after repeated deletion of palindrome substrings | C # program to find minimum step to delete a string ; method returns minimum step for deleting the string , where in one step a palindrome is removed ; declare dp array and initialize it with 0 s ; loop for substring length we are considering ; loop with two variables i and j , denoting starting and ending of substrings ; If substring length is 1 , then 1 step will be needed ; delete the ith char individually and assign result for subproblem ( i + 1 , j ) ; if current and next char are same , choose min from current and subproblem ( i + 2 , j ) ; loop over all right characters and suppose Kth char is same as ith character then choose minimum from current and two substring after ignoring ith and Kth char ; Uncomment below snippet to print actual dp tablex for ( int i = 0 ; i < N ; i ++ ) { System . out . println ( ) ; for ( int j = 0 ; j < N ; j ++ ) System . out . print ( dp [ i ] [ j ] + " ▁ " ) ; } ; Driver code to test above methods
using System ; class GFG { static int minStepToDeleteString ( string str ) { int N = str . Length ; int [ , ] dp = new int [ N + 1 , N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) for ( int j = 0 ; j <= N ; j ++ ) dp [ i , j ] = 0 ; for ( int len = 1 ; len <= N ; len ++ ) { for ( int i = 0 , j = len - 1 ; j < N ; i ++ , j ++ ) { if ( len == 1 ) dp [ i , j ] = 1 ; else { dp [ i , j ] = 1 + dp [ i + 1 , j ] ; if ( str [ i ] == str [ i + 1 ] ) dp [ i , j ] = Math . Min ( 1 + dp [ i + 2 , j ] , dp [ i , j ] ) ; for ( int K = i + 2 ; K <= j ; K ++ ) if ( str [ i ] == str [ K ] ) dp [ i , j ] = Math . Min ( dp [ i + 1 , K - 1 ] + dp [ K + 1 , j ] , dp [ i , j ] ) ; } } } return dp [ 0 , N - 1 ] ; } public static void Main ( ) { string str = "2553432" ; Console . Write ( minStepToDeleteString ( str ) ) ; } }
Clustering / Partitioning an array such that sum of square differences is minimum | C # program to find minimum cost k partitions of array . ; Initialize answer as infinite . ; function to generate all possible answers . and compute minimum of all costs . i -- > is index of previous partition par -- > is current number of partitions a [ ] and n -- > Input array and its size current_ans -- > Cost of partitions made so far . ; If number of partitions is more than k ; If we have mad k partitions and have reached last element ; 1 ) Partition array at different points 2 ) For every point , increase count of partitions , " par " by 1. 3 ) Before recursive call , add cost of the partition to current_ans ; Driver code
using System ; class GFG { static int inf = 1000000000 ; static int ans = inf ; static void solve ( int i , int par , int [ ] a , int n , int k , int current_ans ) { if ( par > k ) return ; if ( par == k && i == n - 1 ) { ans = Math . Min ( ans , current_ans ) ; return ; } for ( int j = i + 1 ; j < n ; j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j ] - a [ i + 1 ] ) ) ; } public static void Main ( ) { int k = 2 ; int [ ] a = { 1 , 5 , 8 , 10 } ; int n = a . Length ; solve ( - 1 , 0 , a , n , k , 0 ) ; Console . Write ( ans ) ; } }
Minimum steps to minimize n as per given condition | C # program to minimize n to 1 by given rule in minimum steps ; function to calculate min steps ; base case ; store temp value for n as min ( f ( n - 1 ) , f ( n / 2 ) , f ( n / 3 ) ) + 1 ; store memo [ n ] and return ; This function mainly initializes memo [ ] and calls getMinSteps ( n , memo ) ; initialize memoized array ; Driver Code
using System ; class GFG { static int getMinSteps ( int n , int [ ] memo ) { if ( n == 1 ) return 0 ; if ( memo [ n ] != - 1 ) return memo [ n ] ; int res = getMinSteps ( n - 1 , memo ) ; if ( n % 2 == 0 ) res = Math . Min ( res , getMinSteps ( n / 2 , memo ) ) ; if ( n % 3 == 0 ) res = Math . Min ( res , getMinSteps ( n / 3 , memo ) ) ; memo [ n ] = 1 + res ; return memo [ n ] ; } static int getMinSteps ( int n ) { int [ ] memo = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) memo [ i ] = - 1 ; return getMinSteps ( n , memo ) ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( getMinSteps ( n ) ) ; } }
Count of arrays in which all adjacent elements are such that one of them divide the another | C # program to count number of arrays of size n such that every element is in range [ 1 , m ] and adjacen are divisible ; For storing factors . ; For storing multiples . ; calculating the factors and multiples of elements [ 1. . . m ] . ; Initialising for size 1 array for each i <= m . ; Calculating the number of array possible of size i and starting with j . ; For all previous possible values . Adding number of factors . ; Adding number of multiple . ; Calculating the total count of array which start from [ 1. . . m ] . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 1000 ; static int numofArray ( int n , int m ) { int [ , ] dp = new int [ MAX , MAX ] ; List < int > [ ] di = new List < int > [ MAX ] ; List < int > [ ] mu = new List < int > [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = 0 ; i < MAX ; i ++ ) { di [ i ] = new List < int > ( ) ; mu [ i ] = new List < int > ( ) ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 2 * i ; j <= m ; j += i ) { di [ j ] . Add ( i ) ; mu [ i ] . Add ( j ) ; } di [ i ] . Add ( i ) ; } for ( int i = 1 ; i <= m ; i ++ ) dp [ 1 , i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i , j ] = 0 ; foreach ( int x in di [ j ] ) dp [ i , j ] += dp [ i - 1 , x ] ; foreach ( int x in mu [ j ] ) dp [ i , j ] += dp [ i - 1 , x ] ; } } int ans = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { ans += dp [ n , i ] ; di [ i ] . Clear ( ) ; mu [ i ] . Clear ( ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 3 , m = 3 ; Console . WriteLine ( numofArray ( n , m ) ) ; } }
Temple Offerings | C # program to find total offerings required ; To store count of increasing order temples on left and right ( including current temple ) ; Returns count of minimum offerings for n temples of given heights . ; Initialize counts for all temples ; Values corner temples ; Filling left and right values using same values of previous ( or next ) ; Computing max of left and right for all temples and returing sum . ; Driver code
using System ; class GFG { public class Temple { public int L ; public int R ; } ; static int offeringNumber ( int n , int [ ] templeHeight ) { Temple [ ] chainSize = new Temple [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { chainSize [ i ] = new Temple ( ) ; chainSize [ i ] . L = - 1 ; chainSize [ i ] . R = - 1 ; } chainSize [ 0 ] . L = 1 ; chainSize [ n - 1 ] . R = 1 ; for ( int i = 1 ; i < n ; ++ i ) { if ( templeHeight [ i - 1 ] < templeHeight [ i ] ) chainSize [ i ] . L = chainSize [ i - 1 ] . L + 1 ; else chainSize [ i ] . L = 1 ; } for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( templeHeight [ i + 1 ] < templeHeight [ i ] ) chainSize [ i ] . R = chainSize [ i + 1 ] . R + 1 ; else chainSize [ i ] . R = 1 ; } int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += Math . Max ( chainSize [ i ] . L , chainSize [ i ] . R ) ; return sum ; } static void Main ( ) { int [ ] arr1 = { 1 , 2 , 2 } ; Console . Write ( offeringNumber ( 3 , arr1 ) + " STRNEWLINE " ) ; int [ ] arr2 = { 1 , 4 , 3 , 6 , 2 , 1 } ; Console . Write ( offeringNumber ( 6 , arr2 ) + " STRNEWLINE " ) ; } }
Smallest length string with repeated replacement of two distinct adjacent | C # program to find smallest possible length of a string of only three characters ; Returns smallest possible length with given operation allowed . ; Counint occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; If all characters are same . ; If all characters are present even number of times or all are present odd number of times . ; Answer is 1 for all other cases . ; Driver code
using System ; public class GFG { static int stringReduction ( String str ) { int n = str . Length ; int [ ] count = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) { count [ str [ i ] - ' a ' ] ++ ; } if ( count [ 0 ] == n count [ 1 ] == n count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) { return 2 ; } return 1 ; } public static void Main ( ) { String str = " abcbbaacb " ; Console . WriteLine ( stringReduction ( str ) ) ; } }
Largest sum Zigzag sequence in a matrix | Memoization based C # program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver code
using System ; class GFG { static int MAX = 100 ; static int [ , ] dp = new int [ MAX , MAX ] ; static int largestZigZagSumRec ( int [ , ] mat , int i , int j , int n ) { if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; if ( i == n - 1 ) return ( dp [ i , j ] = mat [ i , j ] ) ; int zzs = 0 ; for ( int k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = Math . Max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return ( dp [ i , j ] = ( zzs + mat [ i , j ] ) ) ; } static int largestZigZag ( int [ , ] mat , int n ) { for ( int i = 0 ; i < MAX ; i ++ ) for ( int k = 0 ; k < MAX ; k ++ ) dp [ i , k ] = - 1 ; int res = 0 ; for ( int j = 0 ; j < n ; j ++ ) res = Math . Max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } public static void Main ( String [ ] args ) { int n = 3 ; int [ , ] mat = { { 4 , 2 , 1 } , { 3 , 9 , 6 } , { 11 , 3 , 15 } } ; Console . Write ( " Largest ▁ zigzag ▁ sum : ▁ " + largestZigZag ( mat , n ) ) ; } }
Number of subsequences of the form a ^ i b ^ j c ^ k | C # program to count subsequences of the form a ^ i b ^ j c ^ k ; Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of ' a ' ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of ' b ' ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' ; Traverse all characters of given string ; If current character is ' a ' , then there are following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not part of  bCount subsequences. ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not part of  cCount subsequences. ; Driver code
using System ; public class GFG { static int countSubsequences ( String s ) { int aCount = 0 ; int bCount = 0 ; int cCount = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' a ' ) aCount = ( 1 + 2 * aCount ) ; else if ( s [ i ] == ' b ' ) bCount = ( aCount + 2 * bCount ) ; else if ( s [ i ] == ' c ' ) cCount = ( bCount + 2 * cCount ) ; } return cCount ; } public static void Main ( ) { String s = " abbc " ; Console . Write ( countSubsequences ( s ) ) ; } }
Highway Billboard Problem | C # program to find maximum revenue by placing billboard on the highway with given constraints . ; Array to store maximum revenue at each miles . ; actual minimum distance between 2 billboards . ; check if all billboards are already placed . ; check if we have billboard for that particular mile . If not , copy the previous maximum revenue . ; we do have billboard for this mile . ; If current position is less than or equal to t , then we can have only one billboard . ; Else we may have to remove previously placed billboard ; Driver Code
using System ; class GFG { static int maxRevenue ( int m , int [ ] x , int [ ] revenue , int n , int t ) { int [ ] maxRev = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) maxRev [ i ] = 0 ; int nxtbb = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { if ( nxtbb < n ) { if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i - 1 ] ; else { if ( i <= t ) maxRev [ i ] = Math . Max ( maxRev [ i - 1 ] , revenue [ nxtbb ] ) ; else maxRev [ i ] = Math . Max ( maxRev [ i - t - 1 ] + revenue [ nxtbb ] , maxRev [ i - 1 ] ) ; nxtbb ++ ; } } else maxRev [ i ] = maxRev [ i - 1 ] ; } return maxRev [ m ] ; } static void Main ( ) { int m = 20 ; int [ ] x = new int [ ] { 6 , 7 , 12 , 13 , 14 } ; int [ ] revenue = new int [ ] { 5 , 6 , 5 , 3 , 1 } ; int n = x . Length ; int t = 5 ; Console . Write ( maxRevenue ( m , x , revenue , n , t ) ) ; } }
Finding the maximum square sub | C # program to find maximum K such that K x K is a submatrix with equal elements . ; Returns size of the largest square sub - matrix with all same elements . ; If elements is at top row or first column , it wont form a square matrix 's bottom-right ; Check if adjacent elements are equal ; If not equal , then it will form a 1 x1 submatrix ; Update result at each ( i , j ) ; Driver Code
using System ; class GFG { static int Row = 6 , Col = 6 ; static int largestKSubmatrix ( int [ , ] a ) { int [ , ] dp = new int [ Row , Col ] ; int result = 0 ; for ( int i = 0 ; i < Row ; i ++ ) { for ( int j = 0 ; j < Col ; j ++ ) { if ( i == 0 j == 0 ) dp [ i , j ] = 1 ; else { if ( a [ i , j ] == a [ i - 1 , j ] && a [ i , j ] == a [ i , j - 1 ] && a [ i , j ] == a [ i - 1 , j - 1 ] ) { dp [ i , j ] = ( dp [ i - 1 , j ] > dp [ i , j - 1 ] && dp [ i - 1 , j ] > dp [ i - 1 , j - 1 ] + 1 ) ? dp [ i - 1 , j ] : ( dp [ i , j - 1 ] > dp [ i - 1 , j ] && dp [ i , j - 1 ] > dp [ i - 1 , j - 1 ] + 1 ) ? dp [ i , j - 1 ] : dp [ i - 1 , j - 1 ] + 1 ; } else dp [ i , j ] = 1 ; } result = result > dp [ i , j ] ? result : dp [ i , j ] ; } } return result ; } public static void Main ( ) { int [ , ] a = { { 2 , 2 , 3 , 3 , 4 , 4 } , { 5 , 5 , 7 , 7 , 7 , 4 } , { 1 , 2 , 7 , 7 , 7 , 4 } , { 4 , 4 , 7 , 7 , 7 , 4 } , { 5 , 5 , 5 , 1 , 2 , 7 } , { 8 , 7 , 9 , 4 , 4 , 4 } } ; Console . Write ( largestKSubmatrix ( a ) ) ; } }
Number of subsequences in a string divisible by n | C # program to count subsequences of a string divisible by n ; Returns count of subsequences of str divisible by n . ; division by n can leave only n remainder [ 0. . n - 1 ] . dp [ i ] [ j ] indicates number of subsequences in string [ 0. . i ] which leaves remainder j after division by n . ; Filling value for first digit in str ; start a new subsequence with index i ; exclude i 'th character from all the  current subsequences of string [0...i-1] ; include i 'th character in all the current  subsequences of string [0...i-1] ; Driver code
using System ; class GFG { static int countDivisibleSubseq ( string str , int n ) { int len = str . Length ; int [ , ] dp = new int [ len , n ] ; dp [ 0 , ( str [ 0 ] - '0' ) % n ] ++ ; for ( int i = 1 ; i < len ; i ++ ) { dp [ i , ( str [ i ] - '0' ) % n ] ++ ; for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] += dp [ i - 1 , j ] ; dp [ i , ( j * 10 + ( str [ i ] - '0' ) ) % n ] += dp [ i - 1 , j ] ; } } return dp [ len - 1 , 0 ] ; } public static void Main ( ) { String str = "1234" ; int n = 4 ; Console . Write ( countDivisibleSubseq ( str , n ) ) ; } }
Probability of getting at least K heads in N tosses of Coins | C # Code for Probability of getting atleast K heads in N tosses of Coins ; Returns probability of getting at least k heads in n tosses . ; Probability of getting exactly i heads out of n heads ; Note : 1 << n = pow ( 2 , n ) ; Preprocess all factorial only upto 19 , as after that it will overflow ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 12 head out of 18 coins
using System ; class GFG { public static double [ ] fact ; public static double probability ( int k , int n ) { double ans = 0 ; for ( int i = k ; i <= n ; ++ i ) ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] ) ; ans = ans / ( 1 << n ) ; return ans ; } public static void precompute ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 2 ; i < 20 ; ++ i ) fact [ i ] = fact [ i - 1 ] * i ; } public static void Main ( ) { fact = new double [ 100 ] ; precompute ( ) ; Console . WriteLine ( probability ( 2 , 3 ) ) ; Console . WriteLine ( probability ( 3 , 6 ) ) ; Console . Write ( probability ( 12 , 18 ) ) ; } }