codeparrot/xlcost-text-to-code huggingface dataset:
Maximum subarray sum in O ( n ) using prefix sum | C # program to find out maximum subarray sum in linear time using prefix sum . ; Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array , keep track of minimum prefix sum so far and maximum subarray sum . ; Driver Program ; Test case 1 ; Test case 2
using System ; class GFG { static int maximumSumSubarray ( int [ ] arr , int n ) { int min_prefix_sum = 0 ; int res = int . MinValue ; int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { res = Math . Max ( res , prefix_sum [ i ] - min_prefix_sum ) ; min_prefix_sum = Math . Min ( min_prefix_sum , prefix_sum [ i ] ) ; } return res ; } public static void Main ( ) { int [ ] arr1 = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n1 = arr1 . Length ; Console . WriteLine ( maximumSumSubarray ( arr1 , n1 ) ) ; int [ ] arr2 = { 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 } ; int n2 = arr2 . Length ; Console . WriteLine ( maximumSumSubarray ( arr2 , n2 ) ) ; } }
Counting numbers of n digits that are monotone | C # program to count numbers of n digits that are monotone . ; Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code .
using System ; class GFG { static int DP_s = 9 ; static int getNumMonotone ( int len ) { int [ , ] DP = new int [ len , DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 , i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i , 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i , j ] = DP [ i - 1 , j ] + DP [ i , j - 1 ] ; return DP [ len - 1 , DP_s - 1 ] ; } public static void Main ( ) { Console . WriteLine ( getNumMonotone ( 10 ) ) ; } }
Newman | C # Code for Newman - Conway Sequence ; Function to find the n - th element ; Declare array to store sequence ; Driver Code
using System ; class GFG { static int sequence ( int n ) { int [ ] f = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; int i ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } public static void Main ( ) { int n = 10 ; Console . Write ( sequence ( n ) ) ; } }
Maximum product of an increasing subsequence | Dynamic programming C # implementation of maximum product of an increasing subsequence ; Returns product of maximum product increasing subsequence . ; Initialize MPIS values ; Compute optimized MPIS values considering every element as ending element of sequence ; Pick maximum of all product values ; Driver program to test above function
using System ; using System . Linq ; public class GFG { static long lis ( long [ ] arr , long n ) { long [ ] mpis = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; return mpis . Max ( ) ; } static public void Main ( ) { long [ ] arr = { 3 , 100 , 4 , 5 , 150 , 6 } ; long n = arr . Length ; Console . WriteLine ( lis ( arr , n ) ) ; } }
Hosoya 's Triangle | C # Program to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driver program to test above function
using System ; class GFG { static int Hosoya ( int n , int m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } static void printHosoya ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) Console . Write ( Hosoya ( i , j ) + " ▁ " ) ; Console . WriteLine ( " " ) ; } } public static void Main ( ) { int n = 5 ; printHosoya ( n ) ; } }
Eulerian Number | C # program to find Eulerian number A ( n , m ) ; Return eulerian number A ( n , m ) ; driver code
using System ; class Eulerian { public static int eulerian ( int n , int m ) { if ( m >= n n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } public static void Main ( ) { int n = 3 , m = 1 ; Console . WriteLine ( eulerian ( n , m ) ) ; } }
Largest divisible pairs subset | C # program to find the largest subset which where each pair is divisible . ; function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements . ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; driver code to check the above function
using System ; using System . Linq ; public class GFG { static int largestSubset ( int [ ] a , int n ) { int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int mxm = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] == 0 a [ i ] % a [ j ] == 0 ) mxm = Math . Max ( mxm , dp [ j ] ) ; dp [ i ] = 1 + mxm ; } return dp . Max ( ) ; } static public void Main ( ) { int [ ] a = { 1 , 3 , 6 , 13 , 17 , 18 } ; int n = a . Length ; Console . WriteLine ( largestSubset ( a , n ) ) ; } }
How to solve a Dynamic Programming Problem ? | initialize to - 1 ; this function returns the number of arrangements to form ' n ' ; base case ; checking if already calculated ; storing the result and returning
public static int [ ] dp = new int [ MAXN ] ; static int solve ( int n ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; return dp [ n ] = solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ; }
Friends Pairing Problem | C # program solution for friends pairing problem ; Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver code
using System ; class GFG { static int countFriendsPairings ( int n ) { int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; } public static void Main ( ) { int n = 4 ; Console . Write ( countFriendsPairings ( n ) ) ; } }
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
using System ; class GFG { static int countFriendsPairings ( int n ) { int a = 1 , b = 2 , c = 0 ; if ( n <= 2 ) { return n ; } for ( int i = 3 ; i <= n ; i ++ ) { c = b + ( i - 1 ) * a ; a = b ; b = c ; } return c ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( countFriendsPairings ( n ) ) ; } }
Check whether row or column swaps produce maximum size binary sub | C # program to find maximum binary sub - matrix with row swaps and column swaps . ; Precompute the number of consecutive 1 below the ( i , j ) in j - th column and the number of consecutive 1 s on right side of ( i , j ) in i - th row . ; Travesing the 2d matrix from top - right . ; If ( i , j ) contain 0 , do nothing ; } Counting consecutive 1 on right side ; Travesing the 2d matrix from bottom - left . ; If ( i , j ) contain 0 , do nothing ; } Counting consecutive 1 down to ( i , j ) . ; Return maximum size submatrix with row swap allowed . ; Copying the column ; Sort the copied array ; Find maximum submatrix size . ; Return maximum size submatrix with column swap allowed . ; Copying the row . ; Sort the copied array ; Find maximum submatrix size . ; Solving for row swap and column swap ; Comparing both . ; Driven Program
using System ; public class GFG { static int R = 5 ; static int C = 3 ; static void precompute ( int [ , ] mat , int [ , ] ryt , int [ , ] dwn ) { for ( int j = C - 1 ; j >= 0 ; j -- ) { for ( int i = 0 ; i < R ; ++ i ) { if ( mat [ i , j ] == 0 ) { ryt [ i , j ] = 0 ; else { ryt [ i , j ] = ryt [ i , j + 1 ] + 1 ; } } } for ( int i = R - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < C ; ++ j ) { if ( mat [ i , j ] == 0 ) { dwn [ i , j ] = 0 ; else { dwn [ i , j ] = dwn [ i + 1 , j ] + 1 ; } } } } static int solveRowSwap ( int [ , ] ryt ) { int [ ] b = new int [ R ] ; int ans = 0 ; for ( int j = 0 ; j < C ; j ++ ) { for ( int i = 0 ; i < R ; i ++ ) { b [ i ] = ryt [ i , j ] ; } Array . Sort ( b ) ; for ( int i = 0 ; i < R ; ++ i ) { ans = Math . Max ( ans , b [ i ] * ( R - i ) ) ; } } return ans ; } static int solveColumnSwap ( int [ , ] dwn ) { int [ ] b = new int [ C ] ; int ans = 0 ; for ( int i = 0 ; i < R ; ++ i ) { for ( int j = 0 ; j < C ; ++ j ) { b [ j ] = dwn [ i , j ] ; } Array . Sort ( b ) ; for ( int k = 0 ; k < C ; ++ k ) { ans = Math . Max ( ans , b [ k ] * ( C - k ) ) ; } } return ans ; } static void findMax1s ( int [ , ] mat ) { int [ , ] ryt = new int [ R + 2 , C + 2 ] ; int [ , ] dwn = new int [ R + 2 , C + 2 ] ; precompute ( mat , ryt , dwn ) ; int rswap = solveRowSwap ( ryt ) ; int cswap = solveColumnSwap ( dwn ) ; if ( rswap > cswap ) { Console . WriteLine ( " Row ▁ Swap STRNEWLINE " + rswap ) ; } else { Console . WriteLine ( " Column ▁ Swap STRNEWLINE " + cswap ) ; } } public static void Main ( ) { int [ , ] mat = { { 0 , 0 , 0 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } , { 0 , 0 , 0 } , { 1 , 1 , 0 } } ; findMax1s ( mat ) ; } }
LCS ( Longest Common Subsequence ) of three strings | C # program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver Code
using System ; class GFG { static int lcsOf3 ( String X , String Y , String Z , int m , int n , int o ) { int [ , , ] L = new int [ m + 1 , n + 1 , o + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { for ( int k = 0 ; k <= o ; k ++ ) { if ( i == 0 j == 0 k == 0 ) L [ i , j , k ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] && X [ i - 1 ] == Z [ k - 1 ] ) L [ i , j , k ] = L [ i - 1 , j - 1 , k - 1 ] + 1 ; else L [ i , j , k ] = Math . Max ( Math . Max ( L [ i - 1 , j , k ] , L [ i , j - 1 , k ] ) , L [ i , j , k - 1 ] ) ; } } } return L [ m , n , o ] ; } public static void Main ( ) { string X = " AGGT12" ; string Y = "12TXAYB " ; string Z = "12XBA " ; int m = X . Length ; int n = Y . Length ; int o = Z . Length ; Console . Write ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( X , Y , Z , m , n , o ) ) ; } }
Printing Shortest Common Supersequence | A dynamic programming based C # program print shortest supersequence of two strings ; returns shortest supersequence of X and Y ; dp [ i , j ] contains length of shortest supersequence for X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Fill table in bottom up manner ; Below steps follow recurrence relation ; string to store the shortest supersequence ; Start from the bottom right corner and one by one push characters in output string ; If current character in X and Y are same , then current character is part of shortest supersequence ; Put current character in result ; reduce values of i , j and index ; If current character in X and Y are different ; Put current character of Y in result ; reduce values of j and index ; Put current character of X in result ; reduce values of i and index ; If Y reaches its end , put remaining characters of X in the result string ; If X reaches its end , put remaining characters of Y in the result string ; reverse the string and return it ; Swap values of left and right ; Driver code
using System ; class GFG { static String printShortestSuperSeq ( String X , String Y ) { int m = X . Length ; int n = Y . Length ; int [ , ] dp = new int [ m + 1 , n + 1 ] ; int i , j ; for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) { dp [ i , j ] = j ; } else if ( j == 0 ) { dp [ i , j ] = i ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; } else { dp [ i , j ] = 1 + Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } } String str = " " ; i = m ; j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { str += ( X [ i - 1 ] ) ; i -- ; j -- ; } else if ( dp [ i - 1 , j ] > dp [ i , j - 1 ] ) { str += ( Y [ j - 1 ] ) ; j -- ; } else { str += ( X [ i - 1 ] ) ; i -- ; } } while ( i > 0 ) { str += ( X [ i - 1 ] ) ; i -- ; } while ( j > 0 ) { str += ( Y [ j - 1 ] ) ; j -- ; } str = reverse ( str ) ; return str ; } 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 X = " AGGTAB " ; String Y = " GXTXAYB " ; Console . WriteLine ( printShortestSuperSeq ( X , Y ) ) ; } }
Longest Repeating Subsequence | C # program to find the longest repeating subsequence ; Function to find the longest repeating subsequence ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match ; driver program to check above function
using System ; class GFG { static int findLongestRepeatingSubSeq ( string str ) { int n = str . Length ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = Math . Max ( dp [ i , j - 1 ] , dp [ i - 1 , j ] ) ; } } return dp [ n , n ] ; } public static void Main ( ) { string str = " aabb " ; Console . Write ( " The ▁ length ▁ of ▁ the ▁ largest ▁ " + " subsequence ▁ that ▁ repeats ▁ itself ▁ is ▁ : ▁ " + findLongestRepeatingSubSeq ( str ) ) ; } }
Count total number of N digit numbers such that the difference between sum of even and odd digits is 1 | A memoization based recursive program to count numbers with difference between odd and even digit sums as 1 ; A lookup table used for memoization . ; Memoization based recursive function to count numbers with even and odd digit sum difference as 1. This function considers leading zero as a digit ; Base Case ; If current subproblem is already computed ; Initialize result ; If current digit is odd , then add it to odd sum and recur ; else Add to even sum and recur ; Store current result in lookup table and return the same ; This is mainly a wrapper over countRec . It explicitly handles leading digit and calls countRec ( ) for remaining digits . ; Initialize number digits considered so far ; Initialize all entries of lookup table ; Initialize final answer ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function countRec for remaining digits . Note that the first digit is considered as even digit . ; Driver code
using System ; class GFG { static int [ , , , ] lookup = new int [ 50 , 1000 , 1000 , 2 ] ; static int countRec ( int digits , int esum , int osum , int isOdd , int n ) { if ( digits == n ) return ( esum - osum == 1 ) ? 1 : 0 ; if ( lookup [ digits , esum , osum , isOdd ] != - 1 ) return lookup [ digits , esum , osum , isOdd ] ; int ans = 0 ; if ( isOdd == 1 ) for ( int i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum , osum + i , 0 , n ) ; for ( int i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , 1 , n ) ; return lookup [ digits , esum , osum , isOdd ] = ans ; } static int finalCount ( int n ) { int digits = 0 ; for ( int i = 0 ; i < 50 ; i ++ ) for ( int j = 0 ; j < 1000 ; j ++ ) for ( int k = 0 ; k < 1000 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) lookup [ i , j , k , l ] = - 1 ; int ans = 0 ; int esum = 0 , osum = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , 1 , n ) ; return ans ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( " Count ▁ of ▁ " + n + " ▁ digit ▁ numbers ▁ is ▁ " + finalCount ( n ) ) ; } }
Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 1D array to store results of subproblems ; Driver Code
using System ; class GFG { static int numberOfPaths ( int m , int n ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } public static void Main ( ) { Console . Write ( numberOfPaths ( 3 , 3 ) ) ; } }
Longest Arithmetic Progression | DP | C # program to find Length of the longest AP ( llap ) in a given sorted set . ; Returns length of the longest AP subset in a given set ; Driver Code
using System ; class GFG { static int Solution ( int [ ] A ) { int ans = 2 ; int n = A . Length ; if ( n <= 2 ) return n ; int [ ] llap = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) llap [ i ] = 2 ; Array . Sort ( A ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) { int i = j - 1 ; int k = j + 1 ; while ( i >= 0 && k < n ) { if ( A [ i ] + A [ k ] == 2 * A [ j ] ) { llap [ j ] = Math . Max ( llap [ k ] + 1 , llap [ j ] ) ; ans = Math . Max ( ans , llap [ j ] ) ; i -= 1 ; k += 1 ; } else if ( A [ i ] + A [ k ] < 2 * A [ j ] ) k += 1 ; else i -= 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 9 , 4 , 7 , 2 , 10 } ; Console . Write ( Solution ( a ) + " STRNEWLINE " ) ; } }
Minimum numbers to be appended such that mean of Array is equal to 1 | C # program for above approach ; Function to calculate minimum Number of operations ; Storing sum of array arr [ ] ; Driver Code ; Function Call
using System ; class GFG { static void minumumOperation ( int N , int [ ] arr ) { int sum_arr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum_arr = sum_arr + arr [ i ] ; } if ( sum_arr >= N ) Console . Write ( sum_arr - N ) ; else Console . Write ( 1 ) ; } static public void Main ( ) { int N = 4 ; int [ ] arr = { 8 , 4 , 6 , 2 } ; minumumOperation ( N , arr ) ; } }
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | C # program to find N - th term of the series : 1 , 4 , 15 , 72 , 420 ; Function to find factorial of N ; base condition ; use recursion ; calculate Nth term of series ; Driver Code
using System ; class GFG { static int factorial ( int N ) { if ( N == 0 N == 1 ) return 1 ; return N * factorial ( N - 1 ) ; } static int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } public static void Main ( ) { int N = 6 ; Console . Write ( nthTerm ( N ) ) ; } }
Modify string by increasing each character by its distance from the end of the word | C # implementation of the above approach ; Function to transform the given string ; Size of string ; Stores resultant string ; Iterate over given string ; End of word is reached ; Append the word ; For the last word ; Function to transform and return the transformed word ; Stores resulting word ; Iterate over the word ; Add the position value to the letter ; Convert it back to character ; Add it to the string ; Driver Code ; Given string ; Function Call
using System ; class GFG { public static void manipulate ( String s ) { int n = s . Length ; int i = 0 , j = 0 ; String res = " " ; while ( i < n ) { if ( s [ i ] == ' ▁ ' ) { res += util ( s . Substring ( j , i - j ) ) ; res = res + " ▁ " ; j = i + 1 ; i = j + 1 ; } else { i ++ ; } } res = res + util ( s . Substring ( j , i - j ) ) ; Console . WriteLine ( res ) ; } public static String util ( String sub ) { int n = sub . Length ; int i = 0 ; String ret = " " ; while ( i < n ) { int t = ( sub [ i ] - ' a ' ) + n - 1 - i ; char ch = ( char ) ( t % 26 + 97 ) ; ret = ret + String . Join ( " " , ch ) ; i ++ ; } return ret ; } public static void Main ( String [ ] args ) { String s = " acm ▁ fkz " ; manipulate ( s ) ; } }
Count of lexicographically smaller characters on right | Function to count the smaller characters on the right of index i ; store the length of String ; initialize each elements of arr to zero ; array to store count of smaller characters on the right side of that index ; initialize the variable to store the count of characters smaller than that at index i ; adding the count of characters smaller than index i ; print the count of characters smaller than index i stored in ans array ; Driver Code
using System ; class GFG { static void countSmaller ( String str ) { int n = str . Length ; int [ ] arr = new int [ 26 ] ; int [ ] ans = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { arr [ str [ i ] - ' a ' ] ++ ; int ct = 0 ; for ( int j = 0 ; j < str [ i ] - ' a ' ; j ++ ) { ct += arr [ j ] ; } ans [ i ] = ct ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " edcbaa " ; countSmaller ( str ) ; } }
Find the longest sub | C # program to find that substring which is its suffix prefix and also found somewhere in betweem ; Z - algorithm function ; BIT array ; bit update function which updates values from index " idx " to last by value " val " ; Query function in bit ; Driver Code ; Making the z array ; update in the bit array from index z [ i ] by increment of 1 ; if the value in z [ i ] is not equal to ( n - i ) then no need to move further ; queryng for the maximum length substring from bit array
using System ; class GFG { static int [ ] z_function ( char [ ] s ) { int n = s . Length ; int [ ] z = new int [ n ] ; for ( int i = 1 , l = 0 , r = 0 ; i < n ; i ++ ) { if ( i <= r ) z [ i ] = Math . Min ( r - i + 1 , z [ i - l ] ) ; while ( i + z [ i ] < n && s [ z [ i ] ] == s [ i + z [ i ] ] ) z [ i ] ++ ; if ( i + z [ i ] - 1 > r ) { l = i ; r = i + z [ i ] - 1 ; } } return z ; } static int n , len = 0 ; static int [ ] bit = new int [ 1000005 ] ; static String s ; static int [ ] z ; static void update ( int idx , int val ) { if ( idx == 0 ) return ; while ( idx <= n ) { bit [ idx ] += val ; idx += ( idx & - idx ) ; } } static int pref ( int idx ) { int ans = 0 ; while ( idx > 0 ) { ans += bit [ idx ] ; idx -= ( idx & - idx ) ; } return ans ; } public static void Main ( String [ ] args ) { s = " geeksisforgeeksinplatformgeeks " ; z = new int [ s . Length ] ; n = s . Length ; z = z_function ( s . ToCharArray ( ) ) ; for ( int i = 1 ; i < n ; i ++ ) { update ( z [ i ] , 1 ) ; } for ( int i = n - 1 ; i > 1 ; i -- ) { if ( z [ i ] != ( n - i ) ) continue ; if ( pref ( n ) - pref ( z [ i ] - 1 ) >= 2 ) { len = Math . Max ( len , z [ i ] ) ; } } if ( len == 0 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( s . Substring ( 0 , len ) ) ; } }
Count of words ending at the given suffix in Java | C # implementation of the approach ; Function to return the count of words in the given sentence that end with the given suffix ; To store the count ; Extract words from the sentence ; For every word ; If it ends with the given suffix ; Driver code
using System ; class GFG { static int endingWith ( string str , string suff ) { int cnt = 0 ; string [ ] sep = { " ▁ " } ; string [ ] words = str . Split ( sep , StringSplitOptions . RemoveEmptyEntries ) ; for ( int i = 0 ; i < words . Length ; i ++ ) { if ( words [ i ] . EndsWith ( suff ) ) cnt ++ ; } return cnt ; } public static void Main ( string [ ] args ) { string str = " GeeksForGeeks ▁ is ▁ a ▁ computer " + " ▁ science ▁ portal ▁ for ▁ geeks " ; string suff = " ks " ; Console . Write ( endingWith ( str , suff ) ) ; } }