codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array using Bubble Sort without using loops | C # program for the above approach ; Function to implement bubble sort without using loops ; Base Case : If array contains a single element ; Base Case : If array contains two elements ; Store the first two elements of the list in variables a and b ; Store remaining elements in the list bs ; Store the list after each recursive call ; If a < b ; Otherwise , if b >= a ; Recursively call for the list less than the last element and and return the newly formed list ; Driver Code ; Print the array
using System ; using System . Collections . Generic ; class GFG { static List < int > bubble_sort ( List < int > ar ) { List < int > temp = new List < int > ( ) ; if ( ar . Count <= 1 ) return ar ; if ( ar . Count == 2 ) { if ( ar [ 0 ] < ar [ 1 ] ) return ar ; else { temp . Add ( ar [ 1 ] ) ; temp . Add ( ar [ 0 ] ) ; return temp ; } } int a = ar [ 0 ] ; int b = ar [ 1 ] ; List < int > bs = new List < int > ( ) ; for ( int i = 2 ; i < ar . Count ; i ++ ) bs . Add ( ar [ i ] ) ; List < int > res = new List < int > ( ) ; if ( a < b ) { List < int > temp1 = new List < int > ( ) ; temp1 . Add ( b ) ; for ( int i = 0 ; i < bs . Count ; i ++ ) temp1 . Add ( bs [ i ] ) ; List < int > v = bubble_sort ( temp1 ) ; v . Insert ( 0 , a ) ; res = v ; } else { List < int > temp1 = new List < int > ( ) ; temp1 . Add ( a ) ; for ( int i = 0 ; i < bs . Count ; i ++ ) temp1 . Add ( bs [ i ] ) ; List < int > v = bubble_sort ( temp1 ) ; v . Insert ( 0 , b ) ; res = v ; } List < int > pass = new List < int > ( ) ; for ( int i = 0 ; i < res . Count - 1 ; i ++ ) pass . Add ( res [ i ] ) ; List < int > ans = bubble_sort ( pass ) ; ans . Add ( res [ res . Count - 1 ] ) ; return ans ; } public static void Main ( ) { List < int > arr = new List < int > { 1 , 3 , 4 , 5 , 6 , 2 } ; List < int > res = bubble_sort ( arr ) ; for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } }
Modify a given matrix by placing sorted boundary elements in clockwise manner | C # program for the above approach ; Function to print the elements of the matrix in row - wise manner ; Function to sort boundary elements of a matrix starting from the outermost to the innermost boundary and place them in a clockwise manner ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Stores the current boundary elements ; Push the first row ; Push the last column ; Push the last row ; Push the first column ; Sort the boundary elements ; Update the first row ; Update the last column ; Update the last row ; Update the first column ; Print the resultant matrix ; Driver code ; Given matrix
using System ; using System . Collections . Generic ; class GFG { static void printMatrix ( List < List < int > > a ) { foreach ( List < int > x in a ) { foreach ( int y in x ) { Console . Write ( y + " ▁ " ) ; } Console . WriteLine ( ) ; } } static void sortBoundaryWise ( List < List < int > > a ) { int i , k = 0 , l = 0 ; int m = a . Count , n = a [ 0 ] . Count ; int n_k = 0 , n_l = 0 , n_m = m , n_n = n ; while ( k < m && l < n ) { List < int > boundary = new List < int > ( ) ; for ( i = l ; i < n ; ++ i ) { boundary . Add ( a [ k ] [ i ] ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { boundary . Add ( a [ i ] [ n - 1 ] ) ; } n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) { boundary . Add ( a [ m - 1 ] [ i ] ) ; } m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) { boundary . Add ( a [ i ] [ l ] ) ; } l ++ ; } boundary . Sort ( ) ; int ind = 0 ; for ( i = n_l ; i < n_n ; ++ i ) { a [ n_k ] [ i ] = boundary [ ind ++ ] ; } n_k ++ ; for ( i = n_k ; i < n_m ; ++ i ) { a [ i ] [ n_n - 1 ] = boundary [ ind ++ ] ; } n_n -- ; if ( n_k < n_m ) { for ( i = n_n - 1 ; i >= n_l ; -- i ) { a [ n_m - 1 ] [ i ] = boundary [ ind ++ ] ; } n_m -- ; } if ( n_l < n_n ) { for ( i = n_m - 1 ; i >= n_k ; -- i ) { a [ i ] [ n_l ] = boundary [ ind ++ ] ; } n_l ++ ; } } printMatrix ( a ) ; } static void Main ( ) { List < List < int > > matrix = new List < List < int > > ( ) ; matrix . Add ( new List < int > ( new int [ ] { 9 , 7 , 4 , 5 } ) ) ; matrix . Add ( new List < int > ( new int [ ] { 1 , 6 , 2 , - 6 } ) ) ; matrix . Add ( new List < int > ( new int [ ] { 12 , 20 , 2 , 0 } ) ) ; matrix . Add ( new List < int > ( new int [ ] { - 5 , - 6 , 7 , - 2 } ) ) ; sortBoundaryWise ( matrix ) ; } }
Minimum sum of absolute differences between pairs of a triplet from an array | C # Program for the above approach ; Function to find minimum sum of absolute differences of pairs of a triplet ; Sort the array ; Stores the minimum sum ; Traverse the array ; Update the minimum sum ; Print the minimum sum ; Driver Code ; Input ; Function call to find minimum sum of absolute differences of pairs in a triplet
using System ; using System . Collections . Generic ; class GFG { static int minimum_sum ( int [ ] A , int N ) { Array . Sort ( A ) ; int sum = 2147483647 ; for ( int i = 0 ; i <= N - 3 ; i ++ ) { sum = Math . Min ( sum , Math . Abs ( A [ i ] - A [ i + 1 ] ) + Math . Abs ( A [ i + 1 ] - A [ i + 2 ] ) ) ; } return sum ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 2 , 3 } ; int N = A . Length ; Console . WriteLine ( minimum_sum ( A , N ) ) ; } }
Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters | C # program for the above approach ; Function to check if all characters of the string can be made the same ; Sort the string ; Calculate ASCII value of the median character ; Stores the minimum number of operations required to make all characters equal ; Traverse the string ; Calculate absolute value of current character and median character ; Print the minimum number of operations required ; Driver Code
using System ; public class GFG { static void sameChar ( String S , int N ) { char [ ] temp = S . ToCharArray ( ) ; Array . Sort ( temp ) ; String s = new String ( temp ) ; int mid = s [ N / 2 ] ; int total_operations = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total_operations += Math . Abs ( ( s [ i ] - 0 ) - mid ) ; } Console . Write ( total_operations ) ; } static public void Main ( ) { String S = " geeks " ; int N = S . Length ; sameChar ( S , N ) ; } }
Maximum score possible from an array with jumps of at most length K | C # program for the above approach ; Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A [ ] ; Array to store memoization ; Initialize dp [ ] with - 1 ; Driver Code
using System ; class GFG { static int maxScore ( int i , int [ ] A , int K , int N , int [ ] dp ) { if ( i >= N - 1 ) return A [ N - 1 ] ; if ( dp [ i ] != - 1 ) return dp [ i ] ; int score = int . MinValue ; for ( int j = 1 ; j <= K ; j ++ ) { score = Math . Max ( score , maxScore ( i + j , A , K , N , dp ) ) ; } return dp [ i ] = score + A [ i ] ; } static void getScore ( int [ ] A , int N , int K ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( maxScore ( 0 , A , K , N , dp ) ) ; } static public void Main ( ) { int [ ] A = { 100 , - 30 , - 50 , - 15 , - 20 , - 30 } ; int K = 3 ; int N = A . Length ; getScore ( A , N , K ) ; } }
Count triplets from an array which can form quadratic equations with real roots | C # program for the above approach ; Function to find count of triplets ( a , b , c ) such that the equations ax ^ 2 + bx + c = 0 has real roots ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Generate all possible triplets ( a , b , c ) ; If the coefficient of X ^ 2 and X are equal ; If coefficient of X ^ 2 or x are equal to the constant ; Condition for having real roots ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static int getCount ( int [ ] arr , int N ) { int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { for ( int a = 0 ; a < N ; a ++ ) { if ( a == b ) continue ; for ( int c = 0 ; c < N ; c ++ ) { if ( c == a c == b ) continue ; int d = arr [ b ] * arr [ b ] / 4 ; if ( arr [ a ] * arr <= d ) count ++ ; } } } return count ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( getCount ( arr , N ) ) ; } }
Check if all array elements can be reduced to less than X | C # Program to implement the above approach ; Function to check if all array elements can be reduced to less than X or not ; Checks if all array elements are already a X or not ; Traverse every possible pair ; Calculate GCD of two array elements ; If gcd is a 1 ; If gcd is a X , then a pair is present to reduce all array elements to a X ; If no pair is present with gcd is a X ; Function to check if all array elements area X ; Function to calculate gcd of two numbers ; Driver Code
using System ; class GFG { public static bool findAns ( int [ ] A , int N , int X ) { if ( check ( A , X ) ) { return true ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int gcd = gcdFoo ( A [ i ] , A [ j ] ) ; if ( gcd != 1 ) { if ( gcd <= X ) { return true ; } } } } return false ; } public static bool check ( int [ ] A , int X ) { for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] > X ) { return false ; } } return true ; } static int gcdFoo ( int a , int b ) { if ( b == 0 ) return a ; return gcdFoo ( b , a % b ) ; } public static void Main ( String [ ] args ) { int X = 4 ; int [ ] A = { 2 , 1 , 5 , 3 , 6 } ; int N = 5 ; Console . WriteLine ( findAns ( A , N , X ) ) ; } }
Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | C # program for the above approach ; Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ] ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ] ; Return false ; Driver Code ; Function Call
using System ; class GFG { static string check ( int [ ] a , int [ ] b , int Na , int Nb , int k , int m ) { if ( Na < k < m ) return " No " ; Array . Sort ( a ) ; Array . Sort ( b ) ; if ( a [ k - 1 ] < b [ Nb - m ] ) { return " Yes " ; } return " No " ; } static public void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 3 , 4 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; int X = 2 , Y = 1 ; Console . WriteLine ( check ( arr1 , arr2 , N , M , X , Y ) ) ; } }
Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum | C # program for the above approach ; Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum ; Sort the array in increasing order ; Calculating the min Bitwise XOR between consecutive elements ; Return the final minimum Bitwise XOR ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
using System ; class GFG { static int splitArray ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int result = Int32 . MaxValue ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . Min ( result , arr [ i ] ^ arr [ i - 1 ] ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . Length ; Console . Write ( splitArray ( arr , N ) ) ; } }
Sort an array by left shifting digits of array elements | C # program for the above approach ; Function to check if an array is sorted in increasing order or not ; Traverse the array ; Function to sort the array by left shifting digits of array elements ; Stores previous array element ; Traverse the array [ ] arr ; Stores current element ; Stores current element in String format ; Left - shift digits of current element in all possible ways ; Left - shift digits of current element by idx ; If temp greater than or equal to prev and temp less than optEle ; Update arr [ i ] ; Update prev ; If arr is in increasing order ; Otherwise ; Driver Code
using System ; public class GFG { static bool isIncreasing ( int [ ] arr ) { for ( int i = 0 ; i < arr . Length - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } static int [ ] sortArr ( int [ ] arr ) { int prev = - 1 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int optEle = arr [ i ] ; String strEle = String . Join ( " " , arr [ i ] ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { String strEle2 = strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ; int temp = Int32 . Parse ( strEle2 ) ; if ( temp >= prev && temp < optEle ) optEle = temp ; } arr [ i ] = optEle ; prev = arr [ i ] ; } if ( isIncreasing ( arr ) ) return arr ; else { return new int [ ] { - 1 } ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 511 , 321 , 323 , 432 , 433 } ; int [ ] res = sortArr ( arr ) ; for ( int i = 0 ; i < res . Length ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } }
Maximize maximum possible subarray sum of an array by swapping with elements from another array | C # program for the above approach ; Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver Code ; Given arrays and respective lengths ; Calculate maximum subarray sum
using System ; using System . Collections . Generic ; class GFG { static void maxSum ( int [ ] arr , int [ ] brr , int N , int K ) { List < int > crr = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { crr . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < K ; i ++ ) { crr . Add ( brr [ i ] ) ; } crr . Sort ( ) ; crr . Reverse ( ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } Console . WriteLine ( sum ) ; } static void Main ( ) { int [ ] arr = { 7 , 2 , - 1 , 4 , 5 } ; int N = arr . Length ; int [ ] brr = { 1 , 2 , 3 , 2 } ; int K = brr . Length ; maxSum ( arr , brr , N , K ) ; } }
Count pairs with Bitwise XOR greater than both the elements of the pair | C # program for the above approach ; Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair ; Stores the count of pairs ; Generate all possible pairs ; Find the Bitwise XOR ; Find the maximum of two ; If xo < mx , increment count ; Print the value of count ; Driver Code ; Function Call
using System ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int xo = ( A [ i ] ^ A [ j ] ) ; int mx = Math . Max ( A [ i ] , A [ j ] ) ; if ( xo > mx ) { count ++ ; } } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Possible arrangement of persons waiting to sit in a hall | C # program for the above approach ; Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Driver code ; Given N ; Given arr [ ] ; Given string ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void findTheOrder ( int [ ] arr , string s , int N ) { List < int > ans = new List < int > ( ) ; Tuple < int , int > [ ] A = new Tuple < int , int > [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = new Tuple < int , int > ( arr [ i ] , i + 1 ) ; Array . Sort ( A ) ; List < Tuple < int , int > > q = new List < Tuple < int , int > > ( ) ; int index = 0 ; for ( int i = 0 ; i < 2 * N ; i ++ ) { if ( s [ i ] == '0' ) { ans . Add ( A [ index ] . Item2 ) ; q . Add ( A [ index ] ) ; q . Sort ( ) ; q . Reverse ( ) ; index ++ ; } else { ans . Add ( q [ 0 ] . Item2 ) ; q . RemoveAt ( 0 ) ; } } foreach ( int i in ans ) { Console . Write ( i + " ▁ " ) ; } } static void Main ( ) { int N = 3 ; int [ ] arr = { 2 , 1 , 3 } ; string s = "001011" ; findTheOrder ( arr , s , N ) ; } }
Difference between sum of K maximum even and odd array elements | C # program for the above approach ; Function to find the absolute difference between sum of first K maximum even and odd numbers ; Stores index from where odd number starts ; Segregate even and odd number ; If current element is even ; Sort in decreasing order even part ; Sort in decreasing order odd part ; Calculate sum of k maximum even number ; Calculate sum of k maximum odd number ; Print the absolute difference ; Driver Code ; Given array [ ] arr ; Size of array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void evenOddDiff ( int [ ] a , int n , int k ) { int j = - 1 ; List < int > even = new List < int > ( ) ; List < int > odd = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) { even . Add ( a [ i ] ) ; } else odd . Add ( a [ i ] ) ; } j ++ ; even . Sort ( ) ; even . Reverse ( ) ; odd . Sort ( ) ; odd . Reverse ( ) ; int evenSum = 0 , oddSum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { evenSum += even [ i ] ; } for ( int i = 0 ; i < k ; i ++ ) { oddSum += odd [ i ] ; } Console . Write ( Math . Abs ( evenSum - oddSum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 8 , 3 , 4 , 5 } ; int N = arr . Length ; int K = 2 ; evenOddDiff ( arr , N , K ) ; } }
Rearrange array to make product of prefix sum array non zero | C # program to implement the above approach ; Function to print array elements ; Function to rearrange array that satisfies the given condition ; Stores sum of elements of the given array ; Calculate totalSum ; If the totalSum is equal to 0 ; No possible way to rearrange array ; If totalSum exceeds 0 ; Rearrange the array in descending order ; Otherwise ; Rearrange the array in ascending order ; Driver Code
using System ; class GFG { static void printArr ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void rearrangeArr ( int [ ] arr , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } if ( totalSum == 0 ) { Console . Write ( " - 1" + " STRNEWLINE " ) ; } else if ( totalSum > 0 ) { Array . Sort ( arr ) ; arr = reverse ( arr ) ; printArr ( arr , N ) ; } else { Array . Sort ( arr ) ; printArr ( arr , N ) ; } } static int [ ] reverse ( int [ ] a ) { int i , n = a . Length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , - 2 , 3 } ; int N = arr . Length ; rearrangeArr ( arr , N ) ; } }
Count ways to distribute exactly one coin to each worker | C # program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Driver code ; Given two arrays ; Function Call
using System ; using System . Collections ; class GFG { static int MOD = 1000000007 ; static int solve ( ArrayList values , ArrayList salary ) { int ret = 1 ; int amt = 0 ; values . Sort ( ) ; salary . Sort ( ) ; while ( salary . Count > 0 ) { while ( values . Count > 0 && ( int ) values [ values . Count - 1 ] >= ( int ) salary [ salary . Count - 1 ] ) { amt ++ ; values . RemoveAt ( values . Count - 1 ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . RemoveAt ( salary . Count - 1 ) ; } return ret ; } public static void Main ( string [ ] args ) { ArrayList values = new ArrayList ( ) ; values . Add ( 1 ) ; values . Add ( 2 ) ; ArrayList salary = new ArrayList ( ) ; salary . Add ( 2 ) ; Console . Write ( solve ( values , salary ) ) ; } }
Count of index pairs with equal elements in an array | Set 2 | C # program for the above approach ; Function that counts the pair in the array [ ] arr ; Sort the array ; Initialize two pointers ; Add all valid pairs to answer ; Return the answer ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; Array . Sort ( arr ) ; int left = 0 , right = 1 ; while ( right < n ) { if ( arr [ left ] == arr [ right ] ) ans += right - left ; else left = right ; right ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 3 , 2 , 3 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }
Minimize Cost to sort a String in Increasing Order of Frequencies of Characters | C # program to implement above approach ; For a single character ; Stores count of repetitions of a character ; If repeating character ; Otherwise ; Store frequency ; Reset count ; Insert the last character block ; Sort the frequencies ; Stores the minimum cost of all operations ; Store the absolute difference of i - th frequencies of ordered and unordered sequences ; Return the minimum cost ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int sortString ( string S ) { List < int > sorted = new List < int > ( ) ; List < int > original = new List < int > ( ) ; bool insert = false ; if ( S . Length == 1 ) { Console . WriteLine ( 0 ) ; } int curr = 1 ; for ( int i = 0 ; i < ( S . Length - 1 ) ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) { curr += 1 ; insert = false ; } else { sorted . Add ( curr ) ; original . Add ( curr ) ; curr = 1 ; insert = true ; } } if ( ( S [ S . Length - 1 ] != S [ S . Length - 2 ] ) insert == false ) { sorted . Add ( curr ) ; original . Add ( curr ) ; } sorted . Sort ( ) ; int t_cost = 0 ; for ( int i = 0 ; i < sorted . Count ; i ++ ) { t_cost += Math . Abs ( sorted [ i ] - original [ i ] ) ; } return ( t_cost / 2 ) ; } static void Main ( ) { string S = " aabbcccdeffffggghhhhhii " ; Console . Write ( sortString ( S ) ) ; } }