codeparrot/xlcost-text-to-code huggingface dataset:
Path with maximum product in 2 | C # Program to find maximum product path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Function to find maximum product ; It will store the maximum product till a given cell . ; It will store the minimum product till a given cell ( for - ve elements ) ; If we are at topmost or leftmost , just copy the elements . ; If we 're not at the  above, we can consider  the above value. ; If we 're not on the  leftmost, we can consider  the left value. ; Store max & min product till i , j . ; Return the max product path from 0 , 0. N - 1 , M - 1. ; Driver Code ; Print maximum product from ( 0 , 0 ) to ( N - 1 , M - 1 )
using System ; class GFG { static readonly int N = 3 ; static readonly int M = 3 ; static int maxProductPath ( int [ , ] arr ) { int [ , ] maxPath = new int [ N , M ] ; int [ , ] minPath = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int minVal = int . MaxValue ; int maxVal = int . MinValue ; if ( i == 0 && j == 0 ) { maxVal = arr [ i , j ] ; minVal = arr [ i , j ] ; } if ( i > 0 ) { int tempMax = Math . Max ( maxPath [ i - 1 , j ] * arr [ i , j ] , minPath [ i - 1 , j ] * arr [ i , j ] ) ; maxVal = Math . Max ( maxVal , tempMax ) ; int tempMin = Math . Min ( maxPath [ i - 1 , j ] * arr [ i , j ] , minPath [ i - 1 , j ] * arr [ i , j ] ) ; minVal = Math . Min ( minVal , tempMin ) ; } if ( j > 0 ) { int tempMax = Math . Max ( maxPath [ i , j - 1 ] * arr [ i , j ] , minPath [ i , j - 1 ] * arr [ i , j ] ) ; maxVal = Math . Max ( maxVal , tempMax ) ; int tempMin = Math . Min ( maxPath [ i , j - 1 ] * arr [ i , j ] , minPath [ i , j - 1 ] * arr [ i , j ] ) ; minVal = Math . Min ( minVal , tempMin ) ; } maxPath [ i , j ] = maxVal ; minPath [ i , j ] = minVal ; } } return maxPath [ N - 1 , M - 1 ] ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , - 2 , 3 } , { 4 , - 5 , 6 } , { - 7 , - 8 , 9 } } ; Console . Write ( maxProductPath ( arr ) + " STRNEWLINE " ) ; } }
Vertical and Horizontal retrieval ( MRT ) on Tapes | C # program to print Vertical filling ; 2D matrix for vertical insertion on tapes ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; It is used for calculating mean retrieval time ; vertical insertion on tape ; initialize variables to 0 for each iteration ; Used for getting ' sum ' sizes of records in tape to determine whether tape is full or not ; if tape is not full ; check for ability of tapes to hold value ; initialize variables to 0 for each iteration ; display elements of tape ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . Count is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
using System ; class GFG { static void vertical_Fill ( int [ ] records , int [ ] tape , int m , int n ) { int [ , ] v = new int [ m , n ] ; int sum = 0 ; int Retrieval_Time = 0 ; double Mrt ; int z = 0 , j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { sum = 0 ; for ( int k = 0 ; k < i ; k ++ ) { sum += v [ j , k ] ; } if ( sum + records [ z ] <= tape [ j ] ) { v [ j , i ] = records [ z ] ; z ++ ; } } if ( v [ 2 , i ] == 0 ) { break ; } } for ( int i = 0 ; i < m ; i ++ ) { Retrieval_Time = 0 ; Console . Write ( " tape ▁ " + ( i + 1 ) + " ▁ : ▁ [ ▁ " ) ; for ( j = 0 ; j < n ; j ++ ) { if ( v [ i , j ] != 0 ) { Console . Write ( v [ i , j ] + " ▁ " ) ; } else { break ; } } Console . Write ( " ] " ) ; for ( int k = 0 ; v [ i , k ] != 0 ; k ++ ) { Retrieval_Time += v [ i , k ] * ( j - k ) ; } Mrt = ( double ) Retrieval_Time / j ; Console . Write ( " TABSYMBOL MRT ▁ : ▁ " + Mrt + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int [ ] records = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 } ; int [ ] tape = { 25 , 80 , 160 } ; int n = records . Length ; int m = tape . Length ; Array . Sort ( records ) ; vertical_Fill ( records , tape , m , n ) ; } }
Find two equal subsequences of maximum length with at least one different index | C # implementation of the approach ; Function to return the required length of the subsequences ; To store the result ; To store the last visited position of lowercase letters ; Initialisation of frequency array to - 1 to indicate no character has previously occured ; For every character of the String ; Get the index of the current character ; If the current character has appeared before in the String ; Update the result ; Update the last position of the current character ; Driver code
using System ; class GFG { static int MAX = 26 ; static int maxLength ( string str , int len ) { int res = 0 ; int [ ] lastPos = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { lastPos [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int C = str [ i ] - ' a ' ; if ( lastPos [ C ] != - 1 ) { res = Math . Max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; } lastPos [ C ] = i ; } return res ; } public static void Main ( ) { string str = " geeksforgeeks " ; int len = str . Length ; Console . WriteLine ( maxLength ( str , len ) ) ; } }
Partitions possible such that the minimum element divides all the other elements of the partition | C # implementation of the approach ; Function to return the count partitions possible from the given array such that the minimum element of any partition divides all the other elements of that partition ; Initialize the count variable ; Find the minimum element ; Break if no minimum element present ; Increment the count if minimum element present ; Replace all the element divisible by min_elem ; Driver code
using System ; class GFG { static int INT_MAX = int . MaxValue ; static int min_element ( int [ ] A , int N ) { int min = A [ 0 ] ; int i ; for ( i = 1 ; i < N ; i ++ ) { if ( min > A [ i ] ) { min = A [ i ] ; } } return min ; } static int countPartitions ( int [ ] A , int N ) { int count = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { int min_elem = min_element ( A , N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( j = 0 ; j < N ; j ++ ) { if ( A [ j ] % min_elem == 0 ) A [ j ] = INT_MAX ; } } return count ; } public static void Main ( ) { int [ ] arr = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 } ; int N = arr . Length ; Console . WriteLine ( countPartitions ( arr , N ) ) ; } }
Minimum number of swaps to make two binary string equal | C # program for the above approach ; Function to calculate min swaps to make binary strings equal ; Count of zero 's  ; Count of one 's  ; As discussed above ; Driver code
using System ; class GFG { static int minSwaps ( string s1 , string s2 ) { int c0 = 0 , c1 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == '0' && s2 [ i ] == '1' ) { c0 ++ ; } else if ( s1 [ i ] == '1' && s2 [ i ] == '0' ) { c1 ++ ; } } int ans = c0 / 2 + c1 / 2 ; if ( c0 % 2 == 0 && c1 % 2 == 0 ) { return ans ; } else if ( ( c0 + c1 ) % 2 == 0 ) { return ans + 2 ; } else { return - 1 ; } } public static void Main ( ) { string s1 = "0011" , s2 = "1111" ; int ans = minSwaps ( s1 , s2 ) ; Console . WriteLine ( ans ) ; } }
Minimum changes required to make all element in an array equal | C # program to find minimum changes required to make all elements of the array equal ; Function to count of minimum changes required to make all elements equal ; Store the count of each element as key - value pair in Dictionary ; Traverse through the Dictionary and find the maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int minChanges ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int maxElem = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { maxElem = Math . Max ( maxElem , entry . Value ) ; } return n - maxElem ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( minChanges ( arr , n ) ) ; } }
Minimum operations to make two numbers equal | C # implementation of the above approach ; Function to find the minimum no of operations ; find the maximum of two and store it in p ; increase it until it is achievable from given n and m ; Here value added to n and m will be S ( n ) = p - n + p - m ; check whether integer value of n exist by the formula n = ( - 1 + Math . sqrt ( 1 + 8 * S ( n ) ) ) / 2 ; Driver code ; Function calling
using System ; class GFG { static int minOperations ( int n , int m ) { int a = 0 , k = 1 ; int p = Math . Max ( n , m ) ; while ( n != m ) { float s = ( float ) ( p - n + p - m ) ; float q = ( float ) ( ( - 1 + Math . Sqrt ( 8 * s + 1 ) ) / 2 ) ; if ( q - Math . Floor ( q ) == 0 ) { a = ( int ) q ; n = m ; } p = p + 1 ; } return a ; } public static void Main ( ) { int n = 1 , m = 3 ; Console . Write ( minOperations ( n , m ) ) ; } }
Count of subarrays with sum at least K | C # implementation of the approach ; Function to return the number of subarrays with sum atleast k ; To store the right index and the current sum ; To store the number of sub - arrays ; For all left indexes ; Get elements till current sum is less than k ; No such subarray is possible ; Add all possible subarrays ; Remove the left most element ; Return the required answer ; Driver code
using System ; class GFG { static int k_sum ( int [ ] a , int n , int k ) { int r = 0 , sum = 0 ; int ans = 0 ; for ( int l = 0 ; l < n ; l ++ ) { while ( sum < k ) { if ( r == n ) break ; else { sum += a [ r ] ; r ++ ; } } if ( sum < k ) break ; ans += n - r + 1 ; sum -= a [ l ] ; } return ans ; } public static void Main ( ) { int [ ] a = { 6 , 1 , 2 , 7 } ; int k = 10 ; int n = a . Length ; Console . WriteLine ( k_sum ( a , n , k ) ) ; } }
Find if a crest is present in the index range [ L , R ] of the given array | C # implementation of the approach ; Function that returns true if the array contains a crest in the index range [ L , R ] ; To keep track of elements which satisfy the Property ; Property is satisfied for the current element ; Cumulative Sum ; If a crest is present in the given index range ; Driver code
using System ; class GFG { static bool hasCrest ( int [ ] arr , int n , int L , int R ) { int [ ] present = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { present [ i ] = 0 ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( int i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 } ; int N = arr . Length ; int L = 2 ; int R = 6 ; if ( hasCrest ( arr , N , L , R ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Maximum Sum of Products of two arrays by toggling adjacent bits | C # program to find the maximum SoP of two arrays by toggling adjacent bits in the second array ; Function to return Max Sum ; intialParity and finalParity are 0 if total no . of 1 's is even else 1 ; minPositive and maxNegative will store smallest positive and smallest negative integer respectively . ; Count of Initial Parity ; if arr1 [ i ] is positive then add 1 in finalParity to get 1 at arr2 [ i ] ; if both parity are odd or even then return sum ; else add one more 1 or remove 1 ; if minPositive > maxNegative , put 1 at maxNegative and add it to our sum ; else remove minPositive no . ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr1 , int [ ] arr2 , int n ) { int initialParity = 0 , finalParity = 0 ; int sum = 0 , minPositive = int . MaxValue , maxNegative = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { initialParity += arr2 [ i ] ; if ( arr1 [ i ] >= 0 ) { finalParity += 1 ; sum += arr1 [ i ] ; minPositive = Math . Min ( minPositive , arr1 [ i ] ) ; } else { maxNegative = Math . Max ( maxNegative , arr1 [ i ] ) ; } } if ( initialParity % 2 == finalParity % 2 ) { return sum ; } else { if ( minPositive + maxNegative >= 0 ) { return sum + maxNegative ; } else { return sum - minPositive ; } } } public static void Main ( String [ ] args ) { int [ ] arr1 = { 2 , - 4 , 5 , 3 } ; int [ ] arr2 = { 0 , 1 , 0 , 1 } ; int n = arr1 . Length ; Console . WriteLine ( maxSum ( arr1 , arr2 , n ) ) ; } }
Minimum number to be added to all digits of X to make X > Y | C # program to find Minimum number to be . Added to all digits of X to make X > Y ; Function to check if X is lexicographically larger Y ; It is lexicographically larger ; Utility function to check minimum value of d ; If X is already larger do not need to . Add anything ; Adding d to all elements of X ; If X is larger now print d ; else print d + 1 ; Driver Code ; Taking the numbers as sequences
using System ; class GFG { static bool IsLarger ( int [ ] X , int [ ] Y , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } static int solve ( int [ ] X , int [ ] Y , int n ) { int ans = 0 ; if ( IsLarger ( X , Y , n ) ) ans = 0 ; else { int d = Y [ 0 ] - X [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) X [ i ] += d ; if ( IsLarger ( X , Y , n ) ) ans = d ; else { ans = d + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] X = { 2 , 3 , 6 , 9 } ; int [ ] Y = { 3 , 4 , 8 , 1 } ; int n = X . Length ; Console . WriteLine ( solve ( X , Y , n ) ) ; } }
Maximum possible Bitwise OR of the two numbers from the range [ L , R ] | C # implementation of the approach ; Function to return the maximum bitwise OR of any pair from the given range ; Converting L to its binary representation ; Converting R to its binary representation ; In order to make the number of bits of L and R same ; Push 0 to the MSB ; When ith bit of R is 1 and ith bit of L is 0 ; From MSB side set all bits of L to be 1 ; From ( i + 1 ) th bit , all bits of L changed to be 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int max_bitwise_or ( int L , int R ) { List < int > v1 = new List < int > ( ) , v2 = new List < int > ( ) , v3 = new List < int > ( ) ; int z = 0 , i , ans = 0 , cnt = 1 ; while ( L > 0 ) { v1 . Add ( L % 2 ) ; L = L / 2 ; } while ( R > 0 ) { v2 . Add ( R % 2 ) ; R = R / 2 ; } while ( v1 . Count != v2 . Count ) { v1 . Add ( 0 ) ; } for ( i = v2 . Count - 1 ; i >= 0 ; i -- ) { if ( v2 [ i ] == 1 && v1 [ i ] == 0 && z == 0 ) { z = 1 ; continue ; } if ( z == 1 ) { v1 . RemoveAt ( i ) ; v1 . Insert ( i , 1 ) ; } } for ( i = 0 ; i < v2 . Count ; i ++ ) { v3 . Add ( v2 [ i ] v1 [ i ] ) ; } for ( i = 0 ; i < v2 . Count ; i ++ ) { if ( v3 [ i ] == 1 ) { ans += cnt ; } cnt *= 2 ; } return ans ; } public static void Main ( String [ ] args ) { int L = 10 , R = 20 ; Console . WriteLine ( max_bitwise_or ( L , R ) ) ; } }
Find the minimum value of X for an expression | C # implementation of above approach ; Function to calculate value of X ; Check for both possibilities ; Driver Code
using System ; class GFG { static int valueofX ( int [ ] ar , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ar [ i ] ; } if ( sum % n == 0 ) { return sum / n ; } else { int A = sum / n , B = sum / n + 1 ; int ValueA = 0 , ValueB = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) ; ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) ; } if ( ValueA < ValueB ) { return A ; } else { return B ; } } } public static void Main ( String [ ] args ) { int n = 7 ; int [ ] arr = { 6 , 9 , 1 , 6 , 1 , 3 , 7 } ; Console . WriteLine ( valueofX ( arr , n ) ) ; } }
Minimum length String with Sum of the alphabetical values of the characters equal to N | C # iprogram to find the Minimum length String with Sum of the alphabetical values of the characters equal to N ; Function to find the minimum length ; Function to find the minimum length String ; Driver code
using System ; class GFG { static int minLength ( int n ) { int ans = n / 26 ; if ( n % 26 != 0 ) ans ++ ; return ans ; } static String minString ( int n ) { int ans = n / 26 ; String res = " " ; while ( ans -- != 0 ) { res = res + " z " ; } if ( n % 26 != 0 ) { res = res + ( char ) ( ( n % 26 ) + 96 ) ; } return res ; } public static void Main ( String [ ] args ) { int n = 50 ; Console . WriteLine ( minLength ( n ) ) ; Console . WriteLine ( minString ( n ) ) ; } }
Minimum halls required for class scheduling | C # implementation of the approach ; Function to return the minimum number of halls required ; Array to store the number of lectures ongoing at time t ; For every lecture increment start point s decrement ( end point + 1 ) ; Perform prefix sum and update the ans to maximum ; Driver code
using System ; class GFG { static int MAX = 100001 ; static int minHalls ( int [ , ] lectures , int n ) { int [ ] prefix_sum = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ lectures [ i , 0 ] ] ++ ; prefix_sum [ lectures [ i , 1 ] + 1 ] -- ; } int ans = prefix_sum [ 0 ] ; for ( int i = 1 ; i < MAX ; i ++ ) { prefix_sum [ i ] += prefix_sum [ i - 1 ] ; ans = Math . Max ( ans , prefix_sum [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] lectures = { { 0 , 5 } , { 1 , 2 } , { 1 , 10 } } ; int n = lectures . GetLength ( 0 ) ; Console . WriteLine ( minHalls ( lectures , n ) ) ; } }
Find the minimum capacity of the train required to hold the passengers | C # implementation of the approach ; Function to return the minimum capacity required ; To store the minimum capacity ; To store the current capacity of the train ; For every station ; Add the number of people entering the train and subtract the number of people exiting the train to get the current capacity of the train ; Update the minimum capacity ; Driver code
using System ; class GFG { static int minCapacity ( int [ ] enter , int [ ] exit , int n ) { int minCap = 0 ; int currCap = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currCap = currCap + enter [ i ] - exit [ i ] ; minCap = Math . Max ( minCap , currCap ) ; } return minCap ; } public static void Main ( String [ ] args ) { int [ ] enter = { 3 , 5 , 2 , 0 } ; int [ ] exit = { 0 , 2 , 4 , 4 } ; int n = enter . Length ; Console . WriteLine ( minCapacity ( enter , exit , n ) ) ; } }
Count of numbers in the range [ L , R ] which satisfy the given conditions | C # implementation of the approach ; Maximum possible valid number ; To store all the required number from the range [ 1 , MAX ] ; Function that returns true if x satisfies the given conditions ; To store the digits of x ; If current digit appears more than once ; If current digit is greater than 5 ; Put the digit in the map ; Function to generate all the required numbers in the range [ 1 , MAX ] ; Insert first 5 valid numbers ; Inserting 0 externally because 0 cannot be the leading digit in any number ; If x satisfies the given conditions ; Cannot append anymore digit as adding a digit will repeat one of the already present digits ; Append all the valid digits one by one and push the new generated number to the queue ; Append the digit ; Push the newly generated number to the queue ; Function to copmpare two Strings which represent a numerical value ; Function to return the count of valid numbers in the range [ l , r ] ; Generate all the valid numbers in the range [ 1 , MAX ] ; To store the count of numbers in the range [ l , r ] ; For every valid number in the range [ 1 , MAX ] ; If current number is within the required range ; If number is equal to either l or r ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 543210 ; static List < String > ans = new List < String > ( ) ; static bool isValidNum ( String x ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < x . Length ; i ++ ) { if ( mp . ContainsKey ( x [ i ] - '0' ) ) { return false ; } else if ( x [ i ] - '0' > 5 ) { return false ; } else { mp . Add ( x [ i ] - '0' , 1 ) ; } } return true ; } static void generate ( ) { Queue < String > q = new Queue < String > ( ) ; q . Enqueue ( "1" ) ; q . Enqueue ( "2" ) ; q . Enqueue ( "3" ) ; q . Enqueue ( "4" ) ; q . Enqueue ( "5" ) ; bool flag = true ; ans . Add ( "0" ) ; while ( q . Count != 0 ) { String x = q . Peek ( ) ; q . Dequeue ( ) ; if ( isValidNum ( x ) ) { ans . Add ( x ) ; } if ( x . Length == 6 ) continue ; for ( int i = 0 ; i <= 5 ; i ++ ) { String z = i . ToString ( ) ; String temp = x + z ; q . Enqueue ( temp ) ; } } } static bool comp ( String a , String b ) { if ( a . Length == b . Length ) { int i = a . CompareTo ( b ) ; return i < 0 ? true : false ; } else return a . Length < b . Length ; } static int findcount ( String l , String r ) { generate ( ) ; int count = 0 ; for ( int i = 0 ; i < ans . Count ; i ++ ) { String a = ans [ i ] ; if ( comp ( l , a ) && comp ( a , r ) ) { count ++ ; } else if ( a == l a == r ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { String l = "1" , r = "1000" ; Console . WriteLine ( findcount ( l , r ) ) ; } }
Find permutation with maximum remainder Sum | C # implementation of the approach ; Function to find the permutation ; Put n at the first index 1 ; Put all the numbers from 2 to n sequentially ; Driver code ; Display the permutation
using System ; class GFG { static int [ ] Findpermutation ( int n ) { int [ ] a = new int [ n + 1 ] ; a [ 1 ] = n ; for ( int i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] v = Findpermutation ( n ) ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( v [ i ] + " ▁ " ) ; } }
Lexicographically smallest string of length N and sum K | C # implementation of the approach ; Function to return the lexicographically smallest string of length n that satisfies the given condition ; Iteration from the last position in the array ; If k is a positive integer ; ' z ' needs to be inserted ; Add the required character ; Driver code
using System ; class GFG { public static char [ ] lexo_small ( int n , int k ) { char [ ] arr = new char [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = ' a ' ; for ( i = n - 1 ; i >= 0 ; i -- ) { k -= i ; if ( k >= 0 ) { if ( k >= 26 ) { arr [ i ] = ' z ' ; k -= 26 ; } else { arr [ i ] = ( char ) ( k + 97 - 1 ) ; k -= arr [ i ] - ' a ' + 1 ; } } else break ; k += i ; } return arr ; } public static void Main ( ) { int n = 5 , k = 42 ; char [ ] arr = lexo_small ( n , k ) ; Console . WriteLine ( new string ( arr ) ) ; } }