codeparrot/xlcost-text-to-code huggingface dataset:
Equal Sum and XOR | C # program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver program to test above function
using System ; public class GFG { public static int countValues ( int n ) { int unset_bits = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) unset_bits ++ ; n = n >> 1 ; } return 1 << unset_bits ; } public static void Main ( String [ ] args ) { int n = 12 ; Console . WriteLine ( countValues ( n ) ) ; } }
Find profession in a special family | C # program to find profession of a person at given level and position ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's  profession. If parent  is a Doctor, this node  will be a Doctor if it  is at odd position and an  engineer if at even position ; If parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; Driver code
using System ; class GFG { static char findProffesion ( int level , int pos ) { if ( level == 1 ) return ' e ' ; if ( findProffesion ( level - 1 , ( pos + 1 ) / 2 ) == ' d ' ) return ( pos % 2 > 0 ) ? ' d ' : ' e ' ; return ( pos % 2 > 0 ) ? ' e ' : ' d ' ; } public static void Main ( ) { int level = 4 , pos = 2 ; if ( findProffesion ( level , pos ) == ' e ' ) Console . WriteLine ( " Engineer " ) ; else Console . WriteLine ( " Doctor " ) ; } }
Print first n numbers with exactly two set bits | C # program to print first n numbers with exactly two set bits ; Function to print first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver program
using System ; class GFG { static void printTwoSetBitNums ( int n ) { int x = 1 ; while ( n > 0 ) { int y = 0 ; while ( y < x ) { Console . Write ( ( ( 1 << x ) + ( 1 << y ) ) + " ▁ " ) ; n -- ; if ( n == 0 ) return ; y ++ ; } x ++ ; } } public static void Main ( ) { int n = 4 ; printTwoSetBitNums ( n ) ; } }
Find even occurring elements in an array of limited range | C # Program to find the even occurring elements in given array ; Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code
using System ; class GFG { static void printRepeatingEven ( int [ ] arr , int n ) { long _xor = 0L ; long pos ; for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( ( pos & _xor ) != 0 ) ) { Console . Write ( arr [ i ] + " ▁ " ) ; _xor ^= pos ; } } } public static void Main ( ) { int [ ] arr = { 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 } ; int n = arr . Length ; printRepeatingEven ( arr , n ) ; } }
Check if a number is Bleak | A simple C # program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't  be Bleak ; Driver code
using System ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static bool isBleak ( int n ) { for ( int x = 1 ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void Main ( ) { if ( isBleak ( 3 ) ) Console . Write ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( isBleak ( 4 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Find the maximum subset XOR of a given set | C # program to find maximum XOR subset ; Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to  smaller i ; Put maximum element with i ' th ▁ bit ▁ set ▁ ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver code
using System ; class GFG { static int INT_BITS = 32 ; static int maxSubarrayXOR ( int [ ] set , int n ) { int index = 0 ; for ( int i = INT_BITS - 1 ; i >= 0 ; i -- ) { int maxInd = index ; int maxEle = int . MinValue ; for ( int j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) { maxEle = set [ j ] ; maxInd = j ; } } if ( maxEle == - 2147483648 ) continue ; int temp = set [ index ] ; set [ index ] = set [ maxInd ] ; set [ maxInd ] = temp ; maxInd = index ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } public static void Main ( ) { int [ ] set = { 9 , 8 , 5 } ; int n = set . Length ; Console . Write ( " Max ▁ subset ▁ XOR ▁ is ▁ " ) ; Console . Write ( maxSubarrayXOR ( set , n ) ) ; } }
Given a set , find XOR of the XOR 's of all subsets. | C # program to find XOR of XOR 's of all subsets ; Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver code
using System ; class GFG { static int findXOR ( int [ ] Set , int n ) { if ( n == 1 ) return Set [ 0 ] ; else return 0 ; } public static void Main ( ) { int [ ] Set = { 1 , 2 , 3 } ; int n = Set . Length ; Console . Write ( " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " + findXOR ( Set , n ) ) ; } }
Find XOR of two number without using XOR operator | C # program to find XOR without using ^ ; Returns XOR of x and y ; Initialize result ; Assuming 32 - bit int ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver Code
using System ; class GFG { static int myXOR ( int x , int y ) { int res = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { int b1 = ( ( x & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int b2 = ( ( y & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int xoredBit = ( ( b1 & b2 ) != 0 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } public static void Main ( String [ ] args ) { int x = 3 , y = 5 ; Console . WriteLine ( " XOR ▁ is ▁ " + myXOR ( x , y ) ) ; } }
Find XOR of two number without using XOR operator | C # program to find XOR without using ^ ; Returns XOR of x and y ; Driver Code
using System ; class GFG { static int myXOR ( int x , int y ) { return ( x y ) & ( ~ x ~ y ) ; } static public void Main ( ) { int x = 3 , y = 5 ; Console . WriteLine ( " XOR ▁ is ▁ " + ( myXOR ( x , y ) ) ) ; } }
How to swap two bits in a given integer ? | C # program to swap bits in an integer ; This function swaps bit at positions p1 and p2 in an integer n ; Move p1 'th to rightmost side  ; Move p2 'th to rightmost side  ; XOR the two bits ; Put the xor bit back to their original positions ; XOR ' x ' with the original number so that the two sets are swapped ; Driver code
using System ; class GFG { static int swapBits ( int n , int p1 , int p2 ) { int bit1 = ( n >> p1 ) & 1 ; int bit2 = ( n >> p2 ) & 1 ; int x = ( bit1 ^ bit2 ) ; x = ( x << p1 ) | ( x << p2 ) ; int result = n ^ x ; return result ; } public static void Main ( string [ ] args ) { int res = swapBits ( 28 , 0 , 3 ) ; Console . Write ( " Result ▁ = ▁ " + res ) ; } }
Write a function that returns 2 for input 1 and returns 1 for 2 |
static int invert ( int x ) { if ( x == 1 ) return 2 ; else return 1 ; }
Maximum length sub | C # implementation of the approach ; Function to return the maximum length of the required sub - array ; For the first consecutive pair of elements ; While a consecutive pair can be selected ; If current pair forms a valid sub - array ; 2 is the length of the current sub - array ; To extend the sub - array both ways ; While elements at indices l and r are part of a valid sub - array ; Update the maximum length so far ; Select the next consecutive pair ; Return the maximum length ; Driver code
using System ; class GFG { static int maxLength ( int [ ] arr , int n ) { int maxLen = 0 ; int i = 0 ; int j = i + 1 ; while ( j < n ) { if ( arr [ i ] != arr [ j ] ) { maxLen = Math . Max ( maxLen , 2 ) ; int l = i - 1 ; int r = j + 1 ; while ( l >= 0 && r < n && arr [ l ] == arr [ i ] && arr [ r ] == arr [ j ] ) { l -- ; r ++ ; } maxLen = Math . Max ( maxLen , 2 * ( r - j ) ) ; } i ++ ; j = i + 1 ; } return maxLen ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxLength ( arr , n ) ) ; } }
Freivaldâ €™ s Algorithm to check if a matrix is product of two | C # code to implement Freivald 's Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now compute B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now compute C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now compute A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code
using System ; class GFG { static int N = 2 ; static bool freivald ( int [ , ] a , int [ , ] b , int [ , ] c ) { Random rand = new Random ( ) ; int [ ] r = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) r [ i ] = ( int ) ( rand . Next ( ) ) % 2 ; int [ ] br = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) br [ i ] = br [ i ] + b [ i , j ] * r [ j ] ; int [ ] cr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) cr [ i ] = cr [ i ] + c [ i , j ] * r [ j ] ; int [ ] axbr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) axbr [ i ] = axbr [ i ] + a [ i , j ] * br [ j ] ; for ( int i = 0 ; i < N ; i ++ ) if ( axbr [ i ] - cr [ i ] != 0 ) return false ; return true ; } static bool isProduct ( int [ , ] a , int [ , ] b , int [ , ] c , int k ) { for ( int i = 0 ; i < k ; i ++ ) if ( freivald ( a , b , c ) == false ) return false ; return true ; } static void Main ( ) { int [ , ] a = new int [ , ] { { 1 , 1 } , { 1 , 1 } } ; int [ , ] b = new int [ , ] { { 1 , 1 } , { 1 , 1 } } ; int [ , ] c = new int [ , ] { { 2 , 2 } , { 2 , 2 } } ; int k = 2 ; if ( isProduct ( a , b , c , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Expectation or expected value of an array | C # code to calculate expected value of an array ; Function to calculate expectation ; Variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver program ; Function for calculating expectation ; Display expectation of given array
using System ; class GFG { static float calc_Expectation ( float [ ] a , float n ) { float prb = ( 1 / n ) ; float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } public static void Main ( ) { float expect , n = 6f ; float [ ] a = { 1f , 2f , 3f , 4f , 5f , 6f } ; expect = calc_Expectation ( a , n ) ; Console . WriteLine ( " Expectation " + " ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " + expect ) ; } }
Sum of N | C # implementation to illustrate the program ; Function to calculate the sum recursively ; Base cases ; If n is odd ; If n is even ; Function to prlong the value of Sum ; Driver Code ; First element ; Common difference ; Number of elements ; Mod value
using System ; class GFG { static long SumGPUtil ( long r , long n , long m ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return ( 1 + r ) % m ; long ans ; if ( n % 2 == 1 ) { ans = ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n - 1 ) / 2 , m ) ; } else { ans = 1 + ( r * ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n / 2 ) - 1 , m ) ) ; } return ( ans % m ) ; } static void SumGP ( long a , long r , long N , long M ) { long answer ; answer = a * SumGPUtil ( r , N , M ) ; answer = answer % M ; Console . WriteLine ( answer ) ; } public static void Main ( ) { long a = 1 ; long r = 4 ; long N = 10000 ; long M = 100000 ; SumGP ( a , r , N , M ) ; } }
Length of longest subarray in which elements greater than K are more than elements not greater than K | C # implementation of above approach ; Function to find the length of a longest subarray in which elements greater than K are more than elements not greater than K ; Create a new array in which we store 1 if a [ i ] > k otherwise we store - 1. ; Taking prefix sum over it ; len will store maximum length of subarray ; This indicate there is at least one subarray of length mid that has sum > 0 ; Check every subarray of length mid if it has sum > 0 or not if sum > 0 then it will satisfy our required condition ; x will store the sum of subarray of length mid ; Satisfy our given condition ; Check for higher length as we get length mid ; Check for lower length as we did not get length mid ; Driver code
using System ; class GFG { static int LongestSubarray ( int [ ] a , int n , int k ) { int [ ] pre = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > k ) pre [ i ] = 1 ; else pre [ i ] = - 1 ; } for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + pre [ i ] ; int len = 0 ; int lo = 1 , hi = n ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; bool ok = false ; for ( int i = mid - 1 ; i < n ; i ++ ) { int x = pre [ i ] ; if ( i - mid >= 0 ) x -= pre [ i - mid ] ; if ( x > 0 ) { ok = true ; break ; } } if ( ok == true ) { len = mid ; lo = mid + 1 ; } else hi = mid - 1 ; } return len ; } public static void Main ( ) { int [ ] a = { 2 , 3 , 4 , 5 , 3 , 7 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( LongestSubarray ( a , n , k ) ) ; } }
Choose points from two ranges such that no point lies in both the ranges | C # implementation of the approach ; Function to find the required points ; Driver code
using System ; class GFG { static void findPoints ( int l1 , int r1 , int l2 , int r2 ) { int x = ( l1 != l2 ) ? Math . Min ( l1 , l2 ) : - 1 ; int y = ( r1 != r2 ) ? Math . Max ( r1 , r2 ) : - 1 ; Console . WriteLine ( x + " ▁ " + y ) ; } public static void Main ( ) { int l1 = 5 , r1 = 10 , l2 = 1 , r2 = 7 ; findPoints ( l1 , r1 , l2 , r2 ) ; } }
Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver program to test above function
using System ; class GFG { static int fact ( int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } public static void Main ( ) { Console . Write ( fact ( 5 ) ) ; } }
Minimum number of working days required to achieve each of the given scores | C # program for the above approach ; Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Driver Code
using System ; class GFG { static void minDays ( int [ ] P , int [ ] arr ) { for ( int i = 1 ; i < P . Length ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { int index = binarySeach ( P , arr [ i ] ) ; if ( index != - 1 ) { Console . Write ( index + 1 + " ▁ " ) ; } else { Console . Write ( - 1 + " ▁ " ) ; } } } static int binarySeach ( int [ ] P , int N ) { int i = 0 ; int j = P . Length - 1 ; int index = - 1 ; while ( i <= j ) { int mid = i + ( j - i ) / 2 ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } public static void Main ( string [ ] args ) { int [ ] arr = { 400 , 200 , 700 , 900 , 1400 } ; int [ ] P = { 100 , 300 , 400 , 500 , 600 } ; minDays ( P , arr ) ; } }
Maximize profit that can be earned by selling an item among N buyers | C # program for the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver Code
using System ; class GFG { public static int maximumProfit ( int [ ] arr ) { int ans = Int32 . MinValue ; int price = 0 ; int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void Main ( string [ ] args ) { int [ ] arr = { 22 , 87 , 9 , 50 , 56 , 43 } ; Console . Write ( maximumProfit ( arr ) ) ; } }
Maximum length palindromic substring for every index such that it starts and ends at that index | C # program for the above approach ; Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Driver Code
using System ; class GFG { static bool isPalindrome ( string S , int i , int j ) { while ( i < j ) { if ( S [ i ] != S [ j ] ) return false ; i ++ ; j -- ; } return true ; } static void printLongestPalindrome ( string S , int N ) { int [ ] palLength = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int maxlength = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( S [ j ] == S [ i ] ) { if ( ( isPalindrome ( S , j , i ) ) != false ) { maxlength = i - j + 1 ; break ; } } } for ( int j = N - 1 ; j > i ; j -- ) { if ( S [ j ] == S [ i ] ) { if ( isPalindrome ( S , i , j ) ) { maxlength = Math . Max ( j - i + 1 , maxlength ) ; break ; } } } palLength [ i ] = maxlength ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( palLength [ i ] + " ▁ " ) ; } } static public void Main ( ) { string S = " bababa " ; int N = S . Length ; printLongestPalindrome ( S , N ) ; } }
Sum of array elements which are multiples of a given number | C # program for the above approach ; Function to find the sum of array elements which are multiples of N ; Stores the sum ; Traverse the given array ; If current element is a multiple of N ; Print total sum ; Driver Code ; Given arr [ ]
using System ; public class GFG { static void mulsum ( int [ ] arr , int n , int N ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % N == 0 ) { sum = sum + arr [ i ] ; } } Console . Write ( sum ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 5 , 6 } ; int n = arr . Length ; int N = 3 ; mulsum ( arr , n , N ) ; } }
Farthest index that can be reached from the Kth index of given array by given operations | C # program for the above approach ; Function to find the farthest index that can be reached ; Declare a priority queue ; Iterate the array ; If current element is greater than the next element ; Otherwise , store their difference ; Push diff into pq ; If size of pq exceeds Y ; Decrease X by the top element of pq ; Remove top of pq ; If X is exhausted ; Current index is the farthest possible ; Print N - 1 as farthest index ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static void farthestHill ( int [ ] arr , int X , int Y , int N , int K ) { int i , diff ; List < int > pq = new List < int > ( ) ; for ( i = K ; i < N - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i + 1 ] ) continue ; diff = arr [ i + 1 ] - arr [ i ] ; pq . Add ( diff ) ; pq . Sort ( ) ; pq . Reverse ( ) ; if ( pq . Count > Y ) { X -= pq [ 0 ] ; pq . RemoveAt ( 0 ) ; } if ( X < 0 ) { Console . Write ( i ) ; return ; } } Console . Write ( N - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 7 , 6 , 9 , 14 , 12 } ; int X = 5 , Y = 1 ; int K = 0 ; int N = arr . Length ; farthestHill ( arr , X , Y , N , K ) ; } }
Lexicographically largest string possible consisting of at most K consecutive similar characters | C # solution for above approach ; Function to find largest string ; Stores the frequency of characters ; Traverse the string ; Stores the resultant string ; Append larger character ; Decrease count in charset ; Increase count ; Check if count reached to charLimit ; Find nearest lower char ; If no character can be appended ; Append nearest lower character ; Reset count for next calculation ; Return new largest string ; Function to return nearest lower character ; Traverse charset from start - 1 ; If no character can be appended ; Driver Code
using System ; using System . Text ; class GFG { static String newString ( String originalLabel , int limit ) { int n = originalLabel . Length ; int [ ] charset = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { charset [ originalLabel [ i ] - ' a ' ] ++ ; } StringBuilder newString = new StringBuilder ( n ) ; for ( int i = 25 ; i >= 0 ; i -- ) { int count = 0 ; while ( charset [ i ] > 0 ) { newString . Append ( ( char ) ( i + ' a ' ) ) ; charset [ i ] -- ; count ++ ; if ( charset [ i ] > 0 && count == limit ) { char next = nextAvailableChar ( charset , i ) ; if ( next == 0 ) return newString . ToString ( ) ; newString . Append ( next ) ; count = 0 ; } } } return newString . ToString ( ) ; } static char nextAvailableChar ( int [ ] charset , int start ) { for ( int i = start - 1 ; i >= 0 ; i -- ) { if ( charset [ i ] > 0 ) { charset [ i ] -- ; return ( char ) ( i + ' a ' ) ; } } return ' \0' ; } public static void Main ( String [ ] args ) { String S = " ccbbb " ; int K = 2 ; Console . WriteLine ( newString ( S , K ) ) ; } }