codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange array by interchanging positions of even and odd elements in the given array | C # program for the above approach ; Function to replace odd elements with even elements and vice versa ; Length of the array ; Traverse the given array ; If arr [ i ] is visited ; Find the next odd element ; Find next even element ; Mark them visited ; Swap them ; Print the readonly array ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void swapEvenOdd ( int [ ] arr ) { int n = arr . Length ; int o = - 1 , e = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) continue ; int r = - 1 ; if ( arr [ i ] % 2 == 0 ) { o ++ ; while ( arr [ o ] % 2 == 0 arr [ o ] < 0 ) o ++ ; r = o ; } else { e ++ ; while ( arr [ e ] % 2 == 1 arr [ e ] < 0 ) e ++ ; r = e ; } arr [ i ] *= - 1 ; arr [ r ] *= - 1 ; int tmp = arr [ i ] ; arr [ i ] = arr [ r ] ; arr [ r ] = tmp ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ( - 1 * arr [ i ] ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; swapEvenOdd ( arr ) ; } }
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | C # program to implement the above approach ; iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; else we could just break the loop as its not a palindrome type sequence ; we could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; we would check for the palindrome condition if yes we print True else False ; Driver code
using System ; class GFG { static string rev ( string str ) { int st = 0 ; int ed = str . Length - 1 ; char [ ] s = str . ToCharArray ( ) ; while ( st < ed ) { char temp = s [ st ] ; s [ st ] = s [ ed ] ; s [ ed ] = temp ; st ++ ; ed -- ; } return new string ( s ) ; } static bool check ( string a , string b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a [ i ] != b [ j ] ) break ; i += 1 ; j -= 1 ; string xa = a . Substring ( i , j + 1 - i ) ; string xb = b . Substring ( i , j + 1 - i ) ; char [ ] XA = xa . ToCharArray ( ) ; Array . Reverse ( XA ) ; char [ ] XB = xb . ToCharArray ( ) ; Array . Reverse ( XB ) ; if ( string . Compare ( xa , new string ( XA ) ) == 0 || string . Compare ( xb , new string ( XB ) ) == 0 ) return true ; } return false ; } static void Main ( ) { string a = " xbdef " ; string b = " cabex " ; if ( check ( a , b , a . Length ) || check ( b , a , a . Length ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }
Maximize length of subarray having equal elements by adding at most K | C # program for the above approach ; Function to find the maximum number of indices having equal elements after adding at most k numbers ; Sort the array in ascending order ; Make prefix sum array ; Initialize variables ; Update mid ; Check if any subarray can be obtained of length mid having equal elements ; Decrease max to mid ; Function to check if a subarray of length len consisting of equal elements can be obtained or not ; Sliding window ; Last element of the sliding window will be having the max size in the current window ; The current number of element in all indices of the current sliding window ; If the current number of the window , added to k exceeds totalNumbers ; Driver Code ; Function call
using System ; class GFG { public static int maxEqualIdx ( int [ ] arr , int k ) { Array . Sort ( arr ) ; int [ ] prefixSum = new int [ arr . Length + 1 ] ; prefixSum [ 1 ] = arr [ 0 ] ; for ( int i = 1 ; i < prefixSum . Length - 1 ; ++ i ) { prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] ; } int max = arr . Length ; int min = 1 ; int ans = 1 ; while ( min <= max ) { int mid = ( max + min ) / 2 ; if ( check ( prefixSum , mid , k , arr ) ) { ans = mid ; min = mid + 1 ; } else { max = mid - 1 ; } } return ans ; } public static bool check ( int [ ] pSum , int len , int k , int [ ] a ) { int i = 0 ; int j = len ; while ( j <= a . Length ) { int maxSize = a [ j - 1 ] ; int totalNumbers = maxSize * len ; int currNumbers = pSum [ j ] - pSum [ i ] ; if ( currNumbers + k >= totalNumbers ) { return true ; } else { i ++ ; j ++ ; } } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int k = 7 ; Console . WriteLine ( maxEqualIdx ( arr , k ) ) ; } }
Sum of product of all pairs of a Binary Array | C # program to implement the above approach ; Function to print the sum of product of all pairs of the given array ; Stores count of one in the given array ; Stores the size of the given array ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code
using System ; class GFG { static int productSum ( int [ ] arr ) { int cntOne = 0 ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 0 , 1 } ; Console . Write ( productSum ( arr ) ) ; } }
Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | C # program for the above approach ; Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; Find the number of digits ; Calculate sum of the digits ; Add the digits of num2 ; If the sum of the digits of N is less than or equal to K ; No number needs to be added ; Otherwise ; Calculate the sum of digits from least significant digit ; If sum exceeds K ; Increase previous digit by 1 ; Add zeros to the end ; Calculate difference between the result and N ; Return the result ; Driver Code
using System ; class GFG { static int minDigits ( int N , int K ) { int digits_num = ( int ) Math . Floor ( Math . Log ( N ) + 1 ) ; int temp_sum = 0 ; int temp = digits_num ; int result = 0 ; int X , var ; int sum = 0 ; int num2 = N ; while ( num2 != 0 ) { sum += num2 % 10 ; num2 /= 10 ; } if ( sum <= K ) { X = 0 ; } else { while ( temp > 0 ) { var = ( N / ( ( int ) Math . Pow ( 10 , temp - 1 ) ) ) ; . temp_sum += var % 10 ; if ( temp_sum >= K ) { var /= 10 ; var ++ ; result = var * ( int ) Math . Pow ( 10 , temp ) ; break ; } temp -- ; } X = result - N ; return X ; } return - 1 ; } public static void Main ( String [ ] args ) { int N = 11 ; int K = 1 ; Console . WriteLine ( minDigits ( N , K ) ) ; } }
Count of Ways to obtain given Sum from the given Array elements | C # program to implement the above approach ; Function to call dfs to calculate the number of ways ; Iterate till the length of array ; Initialize the memorization table ; Function to perform the DFS to calculate the number of ways ; Base case : Reached the end of array ; If current sum is obtained ; Otherwise ; If previously calculated subproblem occurred ; Check if the required sum can be obtained by adding current element or by subtracting the current index element ; Store the count of ways ; Driver Code
using System ; class GFG { static int findWays ( int [ ] nums , int S ) { int sum = 0 ; for ( int i = 0 ; i < nums . Length ; i ++ ) sum += nums [ i ] ; int [ , ] memo = new int [ nums . Length + 1 , 2 * sum + 1 ] ; for ( int i = 0 ; i < memo . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < memo . GetLength ( 1 ) ; j ++ ) { memo [ i , j ] = int . MinValue ; } } return dfs ( memo , nums , S , 0 , 0 , sum ) ; } static int dfs ( int [ , ] memo , int [ ] nums , int S , int curr_sum , int index , int sum ) { if ( index == nums . Length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } if ( memo [ index , curr_sum + sum ] != int . MinValue ) { return memo [ index , curr_sum + sum ] ; } int ans = dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum ) ; memo [ index , curr_sum + sum ] = ans ; return ans ; } public static void Main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = findWays ( arr , S ) ; Console . WriteLine ( answer ) ; } }
Longest subarray with sum not divisible by X | C # program to implement the above approach ; Function to print the longest subarray with sum of elements not divisible by X ; Pref [ ] stores the prefix sum Suff [ ] stores the suffix sum ; If array element is divisibile by x ; Increase count ; If all the array elements are divisible by x ; No subarray possible ; Reverse v to calculate the suffix sum ; Calculate the suffix sum ; Reverse to original form ; Reverse the suffix sum array ; Calculate the prefix sum ; Stores the starting index of required subarray ; Stores the ending index of required subarray ; If suffix sum till i - th index is not divisible by x ; Update the answer ; If prefix sum till i - th index is not divisible by x ; Update the answer ; Print the longest subarray ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void max_length ( int N , int x , int [ ] v ) { int i , a ; List < int > preff = new List < int > ( ) ; List < int > suff = new List < int > ( ) ; int ct = 0 ; for ( i = 0 ; i < N ; i ++ ) { a = v [ i ] ; if ( a % x == 0 ) { ct += 1 ; } } if ( ct == N ) { Console . Write ( - 1 + " STRNEWLINE " ) ; return ; } v = reverse ( v ) ; suff . Add ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { suff . Add ( v [ i ] + suff [ i - 1 ] ) ; } v = reverse ( v ) ; suff . Reverse ( ) ; preff . Add ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { preff . Add ( v [ i ] + preff [ i - 1 ] ) ; } int ans = 0 ; int lp = 0 ; int rp = N - 1 ; for ( i = 0 ; i < N ; i ++ ) { if ( suff [ i ] % x != 0 && ( ans < ( N - 1 ) ) ) { lp = i ; rp = N - 1 ; ans = Math . Max ( ans , N - i ) ; } if ( preff [ i ] % x != 0 && ( ans < ( i + 1 ) ) ) { lp = 0 ; rp = i ; ans = Math . Max ( ans , i + 1 ) ; } } for ( i = lp ; i <= rp ; i ++ ) { Console . Write ( v [ i ] + " ▁ " ) ; } } 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 x = 3 ; int [ ] v = { 1 , 3 , 2 , 6 } ; int N = v . Length ; max_length ( N , x , v ) ; } }
Number of times Maximum and minimum value updated during traversal of array | C # implementation to find the number of times minimum and maximum value updated during the traversal of the array ; Function to find the number of times minimum and maximum value updated during the traversal of the given array ; Increment i if new highest value occurs Increment j if new lowest value occurs ; Driver Code
using System ; class GFG { static void maxUpdated ( int [ ] arr ) { int h_score = arr [ 0 ] ; int l_score = arr [ 0 ] ; int i = 1 , j = 1 ; foreach ( int n in arr ) { if ( h_score < n ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } Console . Write ( " Number ▁ of ▁ times ▁ maximum ▁ value ▁ " ) ; Console . Write ( " updated ▁ = ▁ " + i + " STRNEWLINE " ) ; Console . Write ( " Number ▁ of ▁ times ▁ minimum ▁ value ▁ " ) ; Console . Write ( " updated ▁ = ▁ " + j ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 20 , 22 } ; maxUpdated ( arr ) ; } }
Find the farthest smaller number in the right side | C # implementation of the approach ; Function to find the farthest smaller number in the right side ; To store minimum element in the range i to n ; If current element in the suffix_min is less than a [ i ] then move right ; Print the required answer ; Driver code
using System ; class GFG { static void farthest_min ( int [ ] a , int n ) { int [ ] suffix_min = new int [ n ] ; suffix_min [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffix_min [ i ] = Math . Min ( suffix_min [ i + 1 ] , a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int low = i + 1 , high = n - 1 , ans = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( suffix_min [ mid ] < a [ i ] ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } Console . Write ( ans + " ▁ " ) ; } } public static void Main ( ) { int [ ] a = { 3 , 1 , 5 , 2 , 4 } ; int n = a . Length ; farthest_min ( a , n ) ; } }
Print numbers in descending order along with their frequencies | C # program to print the elements in descending along with their frequencies ; Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
using System ; class GFG { static void printElements ( int [ ] a , int n ) { Array . Sort ( a ) ; a = reverse ( a ) ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { Console . Write ( a [ i ] + " ▁ occurs ▁ " + cnt + " ▁ times STRNEWLINE " ) ; cnt = 1 ; } else cnt += 1 ; } Console . Write ( a [ n - 1 ] + " ▁ occurs ▁ " + cnt + " ▁ times STRNEWLINE " ) ; } 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 [ ] a = { 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 } ; int n = a . Length ; printElements ( a , n ) ; } }
Check if array can be sorted with one swap | C # program to check if an array can be sorted with at - most one swap ; Create a sorted copy of original array ; Check if 0 or 1 swap required to get the sorted array ; Driver Code
using System ; class GFG { static Boolean checkSorted ( int n , int [ ] arr ) { int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = arr [ i ] ; Array . Sort ( b , 0 , n ) ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != b [ i ] ) ct ++ ; if ( ct == 0 ct == 2 ) return true ; else return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . Length ; if ( checkSorted ( n , arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check whether ( i , j ) exists such that arr [ i ] != arr [ j ] and arr [ arr [ i ] ] is equal to arr [ arr [ j ] ] | C # implementation of the above approach ; Function that calculates marks . ; Checking 1 st condition i . e whether Arr [ i ] equal to Arr [ j ] or not ; Checking 2 nd condition i . e whether Arr [ Arr [ i ] ] equal to Arr [ Arr [ j ] ] or not . ; Driver code
using System ; class GFG { static bool checkIndices ( int [ ] Arr , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] != Arr [ j ] ) { if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) return true ; } } } return false ; } static public void Main ( ) { int [ ] Arr = { 3 , 2 , 1 , 1 , 4 } ; int N = Arr . Length ; if ( checkIndices ( Arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find two non | C # program to find two non - overlapping pairs having equal sum in an Array ; Declare a pair ; Function to find two non - overlapping pairs with same sum in an array ; First create an empty map key -> which is sum of a pair of elements in the array value -> list storing index of every pair having that sum ; Consider every pair ( A [ i ] , A [ j ] ) where ( j > i ) ; Calculate sum of current pair ; If sum is already present in the map ; Check every pair having desired sum ; If pairs don 't overlap, print  them and return ; Insert current pair into the map ; Driver Code
using System ; using System . Collections . Generic ; class Pair { public int x , y ; public Pair ( int x , int y ) { this . x = x ; this . y = y ; } } class GFG { public static void findPairs ( int [ ] A ) { Dictionary < int , List < Pair > > map = new Dictionary < int , List < Pair > > ( ) ; for ( int i = 0 ; i < A . Length - 1 ; i ++ ) { for ( int j = i + 1 ; j < A . Length ; j ++ ) { int sum = A [ i ] + A [ j ] ; if ( map . ContainsKey ( sum ) ) { foreach ( Pair pair in map [ sum ] ) { int x = pair . x ; int y = pair . y ; if ( ( x != i && x != j ) && ( y != i && y != j ) ) { Console . WriteLine ( " Pair ▁ First ( " + A [ i ] + " , ▁ " + A [ j ] + " ) " ) ; Console . WriteLine ( " Pair ▁ Second ▁ ( " + A [ x ] + " , ▁ " + A [ y ] + " ) " ) ; return ; } } } map [ sum ] = new List < Pair > ( ) ; map [ sum ] . Add ( new Pair ( i , j ) ) ; } } Console . Write ( " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 8 , 4 , 7 , 8 , 4 } ; findPairs ( A ) ; } }
Print all pairs with given sum | C # implementation of simple method to find print pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Consider all possible pairs and check their sums ; Driver Code
using System ; class GFG { static void printPairs ( int [ ] arr , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) Console . Write ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + " STRNEWLINE " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . Length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }
Ways to choose three points with distance between the most distant points <= L | C # program to count ways to choose triplets such that the distance between the farthest points <= L ; Returns the number of triplets with the distance between farthest points <= L ; sort the array ; find index of element greater than arr [ i ] + L ; find Number of elements between the ith index and indexGreater since the Numbers are sorted and the elements are distinct from the points btw these indices represent points within range ( a [ i ] + 1 and a [ i ] + L ) both inclusive ; if there are at least two elements in between i and indexGreater find the Number of ways to select two points out of these ; Driver Code ; set of n points on the X axis
using System ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Array . Sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int indexGreater = upper_bound ( arr , 0 , n , arr [ i ] + L ) ; int numberOfElements = indexGreater - ( i + 1 ) ; if ( numberOfElements >= 2 ) { ways += ( numberOfElements * ( numberOfElements - 1 ) / 2 ) ; } } return ways ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int L = 4 ; int ans = countTripletsLessThanL ( n , L , arr ) ; Console . WriteLine ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }
Making elements distinct in a sorted array by minimum increments | C # program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code
using System ; class GFG { static int minSum ( int [ ] arr , int n ) { int sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { int j = i ; while ( j < n && arr [ j ] <= arr [ j - 1 ] ) { arr [ j ] = arr [ j ] + 1 ; j ++ ; } } sum = sum + arr [ i ] ; } return sum ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }
Making elements distinct in a sorted array by minimum increments | Efficient C # program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Drivers code
using System ; class GFG { static int minSum ( int [ ] arr , int n ) { int sum = arr [ 0 ] , prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= prev ) { prev = prev + 1 ; sum = sum + prev ; } else { sum = sum + arr [ i ] ; prev = arr [ i ] ; } } return sum ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }
Randomized Binary Search Algorithm | C # program to implement recursive randomized algorithm . ; To generate random number between x and y ie . . [ x , y ] ; A recursive randomized binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code
using System ; class RandomizedBinarySearch { public static int getRandom ( int x , int y ) { Random r = new Random ( ) ; return ( x + ( int ) ( r . Next ( ) % ( y - x + 1 ) ) ) ; } public static int randomizedBinarySearch ( int [ ] arr , int low , int high , int key ) { if ( high >= low ) { int mid = getRandom ( low , high ) ; if ( arr [ mid ] == key ) return mid ; if ( arr [ mid ] > key ) return randomizedBinarySearch ( arr , low , mid - 1 , key ) ; return randomizedBinarySearch ( arr , mid + 1 , high , key ) ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . Length ; int key = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , key ) ; Console . WriteLine ( ( result == - 1 ) ? " Element ▁ is ▁ not ▁ present ▁ in ▁ array " : " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
Program to remove vowels from a String | C # program to remove vowels from a String ; Driver method to test the above function
using System ; using System . Text ; using System . Linq ; using System . Collections . Generic ; public class Test { static String remVowel ( String str ) { char [ ] vowels = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' , ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; List < char > al = vowels . OfType < char > ( ) . ToList ( ) ; ; StringBuilder sb = new StringBuilder ( str ) ; for ( int i = 0 ; i < sb . Length ; i ++ ) { if ( al . Contains ( sb [ i ] ) ) { sb . Replace ( sb [ i ] . ToString ( ) , " " ) ; i -- ; } } return sb . ToString ( ) ; } public static void Main ( ) { String str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " ; Console . Write ( remVowel ( str ) ) ; } }
Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first . ; If there are odd elements , we choose middle element ; If there are even elements , then we choose the average of middle two . ; After deciding the final value , find the result . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minCostToMakeElementEqual ( int [ ] a , int n ) { int y ; if ( n % 2 == 1 ) y = a [ n / 2 ] ; else y = ( a [ n / 2 ] + a [ ( n - 2 ) / 2 ] ) / 2 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) s += Math . Abs ( a [ i ] - y ) ; return s ; } static void Main ( ) { int [ ] a = { 1 , 100 , 101 } ; int n = a . Length ; Console . WriteLine ( minCostToMakeElementEqual ( a , n ) ) ; } }