codeparrot/xlcost-text-to-code huggingface dataset:
Calculate nCr using Pascal 's Triangle | C # implementation of the approach ; Initialize the matrix with 0 ; 0 C0 = 1 ; Set every nCr = 1 where r = 0 ; Value for the current cell of Pascal 's triangle ; Function to return the value of nCr ; Return nCr ; Driver code ; Build the Pascal 's triangle
using System ; class GFG { static int [ , ] l = new int [ 1001 , 1001 ] ; static void initialize ( ) { l [ 0 , 0 ] = 1 ; for ( int i = 1 ; i < 1001 ; i ++ ) { l [ i , 0 ] = 1 ; for ( int j = 1 ; j < i + 1 ; j ++ ) { l [ i , j ] = ( l [ i - 1 , j - 1 ] + l [ i - 1 , j ] ) ; } } } static int nCr ( int n , int r ) { return l [ n , r ] ; } public static void Main ( ) { initialize ( ) ; int n = 8 ; int r = 3 ; Console . WriteLine ( nCr ( n , r ) ) ; } }
Closest sum partition ( into two subsets ) of numbers from 1 to n | C # implementation of the above approach ; Function to return the minimum required absolute difference ; Driver code
using System ; class GFG { static int minAbsDiff ( int n ) { int mod = n % 4 ; if ( mod == 0 mod == 3 ) { return 0 ; } return 1 ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( minAbsDiff ( n ) ) ; } }
Check if the Xor of the frequency of all digits of a number N is zero or not | C # implementation of the above approach ; creating a frequency array ; Finding the last digit of the number ; Dividing the number by 10 to eliminate last digit ; counting frequency of each digit ; checking if the xor of all frequency is zero or not ; Driver code
using System ; class GFG { static bool check ( int s ) { int [ ] freq = new int [ 10 ] ; int r , i ; for ( i = 0 ; i < 10 ; i ++ ) { freq [ i ] = 0 ; } while ( s != 0 ) { r = s % 10 ; s = ( int ) ( s / 10 ) ; freq [ r ] += 1 ; } int xor__ = 0 ; for ( i = 0 ; i < 10 ; i ++ ) { xor__ = xor__ ^ freq [ i ] ; if ( xor__ == 0 ) return true ; else return false ; } return true ; } public static void Main ( ) { int s = 122233 ; if ( check ( s ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K | C # implementation of the above approach ; Function to print N lines ; Iterate N times to print N lines ; Driver Code
using System ; class GFG { static void printLines ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( k * ( 6 * i + 1 ) + " ▁ " + k * ( 6 * i + 2 ) + " ▁ " + k * ( 6 * i + 3 ) + " ▁ " + k * ( 6 * i + 5 ) ) ; } } public static void Main ( ) { int n = 2 , k = 2 ; printLines ( n , k ) ; } }
Sum of first n term of Series 3 , 5 , 9 , 17 , 33. ... | C # program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; Driver Code ; number of terms to be included in sum ; find the Sn
using System ; class GFG { static int calculateSum ( int n ) { return ( ( int ) Math . Pow ( 2 , n + 1 ) + n - 2 ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }
Count the number of Special Strings of a given length N | C # program to count the number of special strings of a given length N ; Function to return count of special Strings ; Stores the answer for a particular value of n ; For n = 0 we have empty String ; For n = 1 we have 2 special Strings ; Calculate count of special String of length i ; fib [ n ] stores the count of special Strings of length n ; Driver code ; Initialise n
using System ; class GFG { const int mod = 1000000007 ; static int count_special ( int n ) { int [ ] fib = new int [ n + 1 ] ; fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { fib [ i ] = ( fib [ i - 1 ] % mod + fib [ i - 2 ] % mod ) % mod ; } return fib [ n ] ; } public static void Main ( ) { int n = 3 ; Console . Write ( count_special ( n ) + " STRNEWLINE " ) ; } }
Counts Path in an Array | C # implementation of the above approach ; Find the number of ways to reach the end ; Base case ; Recursive structure ; Driver code
using System ; class GFG { static int mod = 1000000000 ; static int ways ( int i , int [ ] arr , int n ) { if ( i == n - 1 ) return 1 ; int sum = 0 ; for ( int j = 1 ; j + i < n && j <= arr [ i ] ; j ++ ) { sum += ( ways ( i + j , arr , n ) ) % mod ; sum %= mod ; } return sum % mod ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 1 , 4 , 3 } ; int n = arr . Length ; Console . WriteLine ( ways ( 0 , arr , n ) ) ; } }
Counts Path in an Array | C # implementation of above approach ; find the number of ways to reach the end ; dp to store value ; base case ; Bottom up dp structure ; F [ i ] is dependent of F [ i + 1 ] to F [ i + k ] ; Return value of dp [ 0 ] ; Driver code
using System ; class GFG { static readonly int mod = ( int ) ( 1e9 + 7 ) ; static int ways ( int [ ] arr , int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { dp [ i ] = 0 ; for ( int j = 1 ; ( ( j + i ) < n && j <= arr [ i ] ) ; j ++ ) { dp [ i ] += dp [ i + j ] ; dp [ i ] %= mod ; } } return dp [ 0 ] % mod ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 1 , 4 , 3 } ; int n = arr . Length ; Console . WriteLine ( ways ( arr , n ) % mod ) ; } }
Number of Subsequences with Even and Odd Sum | C # program to get minimum cost to sort strings by reversal operation ; Returns the count of odd and even subsequences ; Variables to store the count of even subsequences and odd subsequences ; Initialising count_even and count_odd to 0 since as there is no subsequence before the iteration with even or odd count . ; Find sum of all subsequences with even count and odd count and storing them as we iterate . ; if the number is even ; if the number is odd ; Driver code ; Calling the function
using System ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static pair countSum ( int [ ] arr , int n ) { int count_odd , count_even ; count_odd = 0 ; count_even = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 == 0 ) { count_even = count_even + count_even + 1 ; count_odd = count_odd + count_odd ; } else { int temp = count_even ; count_even = count_even + count_odd ; count_odd = count_odd + temp + 1 ; } } return new pair ( count_even , count_odd ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 } ; int n = arr . Length ; pair ans = countSum ( arr , n ) ; Console . Write ( " EvenSum ▁ = ▁ " + ans . first ) ; Console . Write ( " ▁ OddSum ▁ = ▁ " + ans . second ) ; } }
Count of integers of length N and value less than K such that they contain digits only from the given set | C # implementation of the approach ; Function to convert a number into vector ; Push all the digits of N from the end one by one to the vector ; If the original number was 0 ; Reverse the vector elements ; Return the required vector ; Function to return the count of B length integers which are less than C and they contain digits from set A [ ] only ; Convert number to digit array ; Case 1 : No such number possible as the generated numbers will always be greater than C ; Case 2 : All integers of length B are valid as they all are less than C ; contain 0 ; Case 3 ; Update the lower [ ] array such that lower [ i ] stores the count of elements in A [ ] which are less than i ; For first index we can 't use 0 ; Whether ( i - 1 ) digit of generated number can be equal to ( i - 1 ) digit of C ; Is digit [ i - 1 ] present in A ? ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 10 ; static List < int > numToVec ( int N ) { List < int > digit = new List < int > ( ) ; while ( N != 0 ) { digit . Add ( N % 10 ) ; N = N / 10 ; } if ( digit . Count == 0 ) digit . Add ( 0 ) ; digit . Reverse ( ) ; return digit ; } static int solve ( List < int > A , int B , int C ) { List < int > digit = new List < int > ( ) ; int d , d2 ; digit = numToVec ( C ) ; d = A . Count ; if ( B > digit . Count d == 0 ) return 0 ; else if ( B < digit . Count ) { if ( A [ 0 ] == 0 && B != 1 ) return ( int ) ( ( d - 1 ) * Math . Pow ( d , B - 1 ) ) ; else return ( int ) Math . Pow ( d , B ) ; } else { int [ ] dp = new int [ B + 1 ] ; int [ ] lower = new int [ MAX + 1 ] ; for ( int i = 0 ; i < d ; i ++ ) lower [ A [ i ] + 1 ] = 1 ; for ( int i = 1 ; i <= MAX ; i ++ ) lower [ i ] = lower [ i - 1 ] + lower [ i ] ; Boolean flag = true ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= B ; i ++ ) { d2 = lower [ digit [ i - 1 ] ] ; dp [ i ] = dp [ i - 1 ] * d ; if ( i == 1 && A [ 0 ] == 0 && B != 1 ) d2 = d2 - 1 ; if ( flag ) dp [ i ] += d2 ; flag = ( flag & ( lower [ digit [ i - 1 ] + 1 ] == lower [ digit [ i - 1 ] ] + 1 ) ) ; } return dp [ B ] ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 5 } ; List < int > A = new List < int > ( arr ) ; int N = 2 ; int k = 21 ; Console . WriteLine ( solve ( A , N , k ) ) ; } }
Number of Paths of Weight W in a K | C # program to count the number of paths with weight W in a K - ary tree ; Function to return the number of ways having weight as wt in K - ary tree ; Return 0 if weight becomes less than zero ; Return one only if the current path has included edge weight of atleast M ; If the current edge weight is greater than or equal to M , set used as true ; Driver Code to test above function
using System ; class GFG { public static int solve ( int [ , ] dp , int wt , int K , int M , int used ) { if ( wt < 0 ) return 0 ; if ( wt == 0 ) { if ( used == 1 ) return 1 ; return 0 ; } if ( dp [ wt , used ] != - 1 ) return dp [ wt , used ] ; int ans = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { if ( i >= M ) ans += solve ( dp , wt - i , K , M , used 1 ) ; else ans += solve ( dp , wt - i , K , M , used ) ; } return dp [ wt , used ] = ans ; } static void Main ( ) { int W = 3 , K = 3 , M = 2 ; int [ , ] dp = new int [ W + 1 , 2 ] ; for ( int i = 0 ; i < W + 1 ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) dp [ i , j ] = - 1 ; Console . Write ( solve ( dp , W , K , M , 0 ) + " STRNEWLINE " ) ; } }
Ways to write N as sum of two or more positive integers | Set | C # implementation of above approach ; Function to find the number of partitions of N ; Base case ; Driver code
using System ; class GFG { static long partitions ( int n ) { long [ ] p = new long [ n + 1 ] ; p [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { int k = 1 ; while ( ( k * ( 3 * k - 1 ) ) / 2 <= i ) { p [ i ] += ( k % 2 != 0 ? 1 : - 1 ) * p [ i - ( k * ( 3 * k - 1 ) ) / 2 ] ; if ( k > 0 ) { k *= - 1 ; } else { k = 1 - k ; } } } return p [ n ] ; } public static void Main ( String [ ] args ) { int N = 20 ; Console . WriteLine ( partitions ( N ) ) ; } }
Longest Increasing Path in Matrix | C # program to find longest increasing path in a matrix . ; Return the length of LIP in 2D matrix ; If value not calculated yet . ; If reach bottom left cell , return 1. ; If reach the corner of the matrix . ; If value greater than below cell . ; If value greater than left cell . ; Wrapper function ; Driver code
using System ; public class GFG { static int LIP ( int [ , ] dp , int [ , ] mat , int n , int m , int x , int y ) { if ( dp [ x , y ] < 0 ) { int result = 0 ; if ( x == n - 1 && y == m - 1 ) return dp [ x , y ] = 1 ; if ( x == n - 1 y == m - 1 ) result = 1 ; if ( x + 1 < n && mat [ x , y ] < mat [ x + 1 , y ] ) result = 1 + LIP ( dp , mat , n , m , x + 1 , y ) ; if ( y + 1 < m && mat [ x , y ] < mat [ x , y + 1 ] ) result = Math . Max ( result , 1 + LIP ( dp , mat , n , m , x , y + 1 ) ) ; dp [ x , y ] = result ; } return dp [ x , y ] ; } static int wrapper ( int [ , ] mat , int n , int m ) { int [ , ] dp = new int [ 10 , 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { dp [ i , j ] = - 1 ; } } return LIP ( dp , mat , n , m , 0 , 0 ) ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 2 , 2 , 3 , 4 } , { 3 , 2 , 3 , 4 } , { 4 , 5 , 6 , 7 } , } ; int n = 4 , m = 4 ; Console . WriteLine ( wrapper ( mat , n , m ) ) ; } }
Counts paths from a point to reach Origin | C # program to count total number of paths from a point to origin ; Recursive function to count number of paths ; If we reach bottom or top left , we are have only one way to reach ( 0 , 0 ) ; Else count sum of both ways ; Driver Code
using System ; public class GFG { static int countPaths ( int n , int m ) { if ( n == 0 m == 0 ) return 1 ; return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) ; } public static void Main ( ) { int n = 3 , m = 2 ; Console . WriteLine ( " ▁ Number ▁ of " + " ▁ Paths ▁ " + countPaths ( n , m ) ) ; } }
Gold Mine Problem | C # program to solve Gold Mine problem ; Returns maximum amount of gold that can be collected when journey started from first column and moves allowed are right , right - up and right - down ; Create a table for storing intermediate results and initialize all cells to 0. The first row of goldMineTable gives the maximum gold that the miner can collect when starts that row ; Gold collected on going to the cell on the right ( -> ) ; Gold collected on going to the cell to right up ( / ) ; Gold collected on going to the cell to right down ( \ ) ; Max gold collected from taking either of the above 3 paths ; The max amount of gold collected will be the max value in first column of all rows ; Driver Code
using System ; class GFG { static int MAX = 100 ; static int getMaxGold ( int [ , ] gold , int m , int n ) { int [ , ] goldTable = new int [ m , n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) goldTable [ i , j ] = 0 ; for ( int col = n - 1 ; col >= 0 ; col -- ) { for ( int row = 0 ; row < m ; row ++ ) { int right = ( col == n - 1 ) ? 0 : goldTable [ row , col + 1 ] ; int right_up = ( row == 0 col == n - 1 ) ? 0 : goldTable [ row - 1 , col + 1 ] ; int right_down = ( row == m - 1 col == n - 1 ) ? 0 : goldTable [ row + 1 , col + 1 ] ; goldTable [ row , col ] = gold [ row , col ] + Math . Max ( right , Math . Max ( right_up , right_down ) ) ; } } int res = goldTable [ 0 , 0 ] ; for ( int i = 1 ; i < m ; i ++ ) res = Math . Max ( res , goldTable [ i , 0 ] ) ; return res ; } static void Main ( ) { int [ , ] gold = new int [ , ] { { 1 , 3 , 1 , 5 } , { 2 , 2 , 4 , 1 } , { 5 , 0 , 2 , 3 } , { 0 , 6 , 1 , 2 } } ; int m = 4 , n = 4 ; Console . Write ( getMaxGold ( gold , m , n ) ) ; } }
Find minimum adjustment cost of an array | C # program to find minimum adjustment cost of an array ; Function to find minimum adjustment cost of an array ; dp [ i ] [ j ] stores minimal adjustment cost on changing A [ i ] to j ; handle first element of array separately ; do for rest elements of the array ; replace A [ i ] to j and calculate minimal adjustment cost dp [ i ] [ j ] ; initialize minimal adjustment cost to INT_MAX ; consider all k such that k >= max ( j - target , 0 ) and k <= min ( M , j + target ) and take minimum ; return minimum value from last row of dp table ; Driver program
using System ; class GFG { public static int M = 100 ; static int minAdjustmentCost ( int [ ] A , int n , int target ) { int [ , ] dp = new int [ n , M + 1 ] ; for ( int j = 0 ; j <= M ; j ++ ) dp [ 0 , j ] = Math . Abs ( j - A [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= M ; j ++ ) { dp [ i , j ] = int . MaxValue ; int k = Math . Max ( j - target , 0 ) ; for ( ; k <= Math . Min ( M , j + target ) ; k ++ ) dp [ i , j ] = Math . Min ( dp [ i , j ] , dp [ i - 1 , k ] + Math . Abs ( A [ i ] - j ) ) ; } } int res = int . MaxValue ; for ( int j = 0 ; j <= M ; j ++ ) res = Math . Min ( res , dp [ n - 1 , j ] ) ; return res ; } public static void Main ( ) { int [ ] arr = { 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 } ; int n = arr . Length ; int target = 10 ; Console . WriteLine ( " Minimum ▁ adjustment " + " ▁ cost ▁ is ▁ " + minAdjustmentCost ( arr , n , target ) ) ; } }
Count triplets from a given range having sum of two numbers of a triplet equal to the third number | C # program to implement the above approach ; Function to find the number of triplets from the range [ L , R ] having sum of two numbers from the triplet equal to the third number ; Stores the total number of triplets ; Find the difference of the range ; Case 1 : If triplets can 't  be formed, then return 0 ; Otherwise ; Update the total number of triplets ; Return the count ; Driver Code
using System ; class GFG { static int totalCombination ( int L , int R ) { int count = 0 ; int K = R - L ; if ( K < L ) return 0 ; int ans = K - L ; count = ( ( ans + 1 ) * ( ans + 2 ) ) / 2 ; return count ; } public static void Main ( ) { int L = 2 , R = 6 ; Console . WriteLine ( totalCombination ( L , R ) ) ; } }
Construct two N | C # program for the above approach ; Satisfying the given conditions ; Declare the two arrays A and B ; Iterate from range [ 1 , 2 * n ] ; Assign consecutive numbers to same indices of the two arrays ; Print the first array ; Print the second array , B ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printArrays ( int n ) { List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { if ( i % 2 == 0 ) A . Add ( i ) ; else B . Add ( i ) ; } Console . Write ( " { ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( A [ i ] ) ; if ( i != n - 1 ) Console . Write ( " , ▁ " ) ; } Console . Write ( " ▁ } STRNEWLINE " ) ; Console . Write ( " { ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( B [ i ] ) ; if ( i != n - 1 ) Console . Write ( " , ▁ " ) ; } Console . Write ( " ▁ } " ) ; } public static void Main ( ) { int N = 5 ; printArrays ( N ) ; } }
Numbers formed by flipping common set bits in two given integers | C # program to implement the above approach ; Function to flip bits of A and B which are set bits in A and B ; Iterater all possible bits of A and B ; If ith bit is set in both A and B ; Clear i - th bit of A ; Clear i - th bit of B ; Print A and B ; Driver Code
using System ; class GFG { static void flipBitsOfAandB ( int A , int B ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( A & ( 1 << i ) ) & ( B & ( 1 << i ) ) ) != 0 ) { A = A ^ ( 1 << i ) ; B = B ^ ( 1 << i ) ; } } Console . Write ( A + " ▁ " + B ) ; } public static void Main ( string [ ] args ) { int A = 7 , B = 4 ; flipBitsOfAandB ( A , B ) ; } }
Count of distinct pair sum between two 1 to N value Arrays | C # implementation to find count of distinct pair sum between two 1 to N value Arrays ; Function to find the distinct sums ; Driver code
using System ; class GFG { static int findDistinctSums ( int N ) { return ( 2 * N - 1 ) ; } public static void Main ( ) { int N = 3 ; Console . Write ( findDistinctSums ( N ) ) ; } }
Count of substrings from given Ternary strings containing characters at least once | C # program for the above approach ; Function to count the number of substrings consists of 0 , 1 , and 2 ; Initialize frequency array of size 3 ; Stores the resultant count ; Traversing string str ; Update frequency array ; If all the characters are present counting number of substrings possible ; Update number of substrings ; Return the number of substrings ; Driver Code
using System ; class GFG { public static int countSubstrings ( string str ) { int [ ] freq = new int [ 3 ] ; int count = 0 ; int i = 0 ; for ( int j = 0 ; j < str . Length ; j ++ ) { freq [ str [ j ] - '0' ] ++ ; while ( freq [ 0 ] > 0 && freq [ 1 ] > 0 && freq [ 2 ] > 0 ) { freq [ str [ i ++ ] - '0' ] -- ; } count += i ; } return count ; } public static void Main ( String [ ] args ) { string str = "00021" ; Console . Write ( countSubstrings ( str ) ) ; } }
Minimum flips to remove any consecutive 3 0 s or 1 s in given Binary string | C # program for the above approach ; Function to find the minimum number of flips to make all three pairs of consecutive characters different ; Stores resultant count of pairs ; Base Case ; Iterate over the range [ 0 , N - 2 ] ; If the consecutive 3 numbers are the same then increment the count and the counter ; Return the answer ; Driver Code
using System ; public class GFG { static int minFlips ( string str ) { int count = 0 ; if ( str . Length <= 2 ) { return 0 ; } for ( int i = 0 ; i < str . Length - 2 ; ) { if ( str [ i ] == str [ i + 1 ] && str [ i + 2 ] == str [ i + 1 ] ) { i = i + 3 ; count ++ ; } else { i ++ ; } } return count ; } public static void Main ( string [ ] args ) { string S = "0011101" ; Console . WriteLine ( minFlips ( S ) ) ; } }
Encrypt the string | C # program for above approach ; Function to convert Decimal to Hex ; Function to encrypt the string ; Iterate the characters of the string ; Iterate until S [ i ] is equal to ch ; Update count and i ; Decrement i by 1 ; Convert count to hexadecimal representation ; Append the character ; Append the characters frequency in hexadecimal representation ; Reverse the obtained answer ; Return required answer ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static string convertToHex ( int num ) { string temp = " " ; while ( num != 0 ) { int rem = num % 16 ; char c ; if ( rem < 10 ) { c = ( char ) ( rem + 48 ) ; } else { c = ( char ) ( rem + 87 ) ; } temp = temp + c ; num = num / 16 ; } return temp ; } static string encryptString ( string S , int N ) { string ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { char ch = S [ i ] ; int count = 0 ; string hex ; while ( i < N && S [ i ] == ch ) { count ++ ; i ++ ; } i -- ; hex = convertToHex ( count ) ; ans = ans + ch ; ans = ans + hex ; } char [ ] Ans = ans . ToCharArray ( ) ; Array . Reverse ( Ans ) ; ans = new string ( Ans ) ; return ans ; } static void Main ( ) { string S = " abc " ; int N = S . Length ; Console . WriteLine ( encryptString ( S , N ) ) ; } }
Count of Binary Strings of length N such that frequency of 1 ' s ▁ exceeds ▁ frequency ▁ of ▁ 0' s | C # program to implement the above approach ; Function to calculate and return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the count of binary Strings of length N such that frequency of 1 ' s ▁ exceed ▁ that ▁ of ▁ 0' s ; Count of N - length binary Strings ; Count of N - length binary Strings having equal count of 0 ' s ▁ and ▁ 1' s ; For even length Strings ; Driver Code
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int countOfString ( int N ) { int Stotal = ( int ) Math . Pow ( 2 , N ) ; int Sequal = 0 ; if ( N % 2 == 0 ) Sequal = binomialCoeff ( N , N / 2 ) ; int S1 = ( Stotal - Sequal ) / 2 ; return S1 ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . Write ( countOfString ( N ) ) ; } }
Remove all occurrences of a character in a string | Recursive approach | C # program for the above approach ; Function to remove all occurrences of a character in the string ; Base Case ; Check the first character of the given string ; Pass the rest of the string to recursion Function call ; Add the first character of str and string from recursion ; Driver Code ; Given String ; Given character ; Function call
using System ; class GFG { static String removeCharRecursive ( String str , char X ) { if ( str . Length == 0 ) { return " " ; } if ( str [ 0 ] == X ) { return removeCharRecursive ( str . Substring ( 1 ) , X ) ; } return str [ 0 ] + removeCharRecursive ( str . Substring ( 1 ) , X ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; char X = ' e ' ; str = removeCharRecursive ( str , X ) ; Console . WriteLine ( str ) ; } }