codeparrot/xlcost-text-to-code huggingface dataset:
Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void print ( List < int > a , int n ) { for ( int i = 0 ; i <= n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void sort ( List < int > a , int n ) { for ( int i = n ; i >= 0 ; i -- ) for ( int j = n ; j > n - i ; j -- ) if ( a [ j ] > a [ j - 1 ] ) { int tempswap = a [ j ] ; a [ j ] = a [ j - 1 ] ; a [ j - 1 ] = tempswap ; } print ( a , n ) ; } static void Main ( ) { int n = 6 ; List < int > a = new List < int > ( ) ; a . Add ( 2 ) ; a . Add ( 4 ) ; a . Add ( 3 ) ; a . Add ( 2 ) ; a . Add ( 4 ) ; a . Add ( 5 ) ; a . Add ( 3 ) ; sort ( a , n ) ; } }
Subtraction in the Array | C # implementation of the approach ; Function to perform the given operation on arr [ ] ; Skip elements which are 0 ; Pick smallest non - zero element ; If all the elements of arr [ ] are 0 ; Driver code
using System ; class GFG { static void operations ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 , sum = 0 ; while ( k -- > 0 ) { while ( i < n && arr [ i ] - sum == 0 ) i ++ ; if ( i < n && arr [ i ] - sum > 0 ) { Console . Write ( arr [ i ] - sum + " ▁ " ) ; sum = arr [ i ] ; } else Console . WriteLine ( "0" ) ; } } public static void Main ( String [ ] args ) { int k = 5 ; int [ ] arr = { 3 , 6 , 4 , 2 } ; int n = arr . Length ; operations ( arr , n , k ) ; } }
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Simple C # program to sort an array of 0 s 1 s and 2 s . ; Variables to maintain the count of 0 ' s , ▁ ▁ 1' s and 2 's in the array ; Putting the 0 's in the  array in starting. ; Putting the 1 ' s ▁ in ▁ the ▁ ▁ array ▁ after ▁ the ▁ 0' s . ; Putting the 2 ' s ▁ in ▁ the ▁ ▁ array ▁ after ▁ the ▁ 1' s ; Prints the array ; Driver function
using System ; public class GfG { public static void sort012 ( int [ ] arr , int n ) { int count0 = 0 , count1 = 0 ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; if ( arr [ i ] == 1 ) count1 ++ ; if ( arr [ i ] == 2 ) count2 ++ ; } for ( int i = 0 ; i < count0 ; i ++ ) arr [ i ] = 0 ; for ( int i = count0 ; i < ( count0 + count1 ) ; i ++ ) arr [ i ] = 1 ; for ( int i = ( count0 + count1 ) ; i < n ; i ++ ) arr [ i ] = 2 ; printArray ( arr , n ) ; } public static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = 12 ; sort012 ( arr , n ) ; } }
Alternate Lower Upper String Sort | C # program for unusual string sorting ; Function for alternate sorting of string ; Count occurrences of individual lower case and upper case characters ; Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant . ; Driver function
using System ; public class GFG { private static int MAX = 100 ; static String alternateSort ( String s1 ) { int n = s1 . Length ; int l = 0 , j = 0 , k = 0 ; char [ ] s = s1 . ToCharArray ( ) ; int [ ] lCount = new int [ MAX ] ; int [ ] uCount = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( char . IsUpper ( s [ i ] ) ) uCount [ s [ i ] - ' A ' ] ++ ; else lCount [ s [ i ] - ' a ' ] ++ ; } while ( k < n ) { while ( l < MAX && uCount [ l ] == 0 ) l ++ ; if ( l < MAX ) { s [ k ++ ] = ( char ) ( ' A ' + l ) ; uCount [ l ] -- ; } while ( j < MAX && lCount [ j ] == 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = ( char ) ( ' a ' + j ) ; lCount [ j ] -- ; } } return ( new String ( s ) ) ; } public static void Main ( ) { String str = " bAwutndekWEdkd " ; Console . Write ( alternateSort ( str ) ) ; } }
Sum of Manhattan distances between all pairs of points | C # Program to find sum of Manhattan distance between all the pairs of given points ; Return the sum of distance between all the pair of points . ; for each point , finding distance to rest of the point ; Driven Program
using System ; class GFG { static int distancesum ( int [ ] x , int [ ] y , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) sum += ( Math . Abs ( x [ i ] - x [ j ] ) + Math . Abs ( y [ i ] - y [ j ] ) ) ; return sum ; } public static void Main ( ) { int [ ] x = { - 1 , 1 , 3 , 2 } ; int [ ] y = { 5 , 6 , 5 , 3 } ; int n = x . Length ; Console . WriteLine ( distancesum ( x , y , n ) ) ; } }
Sum of Manhattan distances between all pairs of points | C # Program to find sum of Manhattan distances between all the pairs of given points ; Return the sum of distance of one axis . ; sorting the array . ; for each point , finding the distance . ; Driven Program
using System ; class GFG { static int distancesum ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int res = 0 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += ( arr [ i ] * i - sum ) ; sum += arr [ i ] ; } return res ; } static int totaldistancesum ( int [ ] x , int [ ] y , int n ) { return distancesum ( x , n ) + distancesum ( y , n ) ; } public static void Main ( ) { int [ ] x = { - 1 , 1 , 3 , 2 } ; int [ ] y = { 5 , 6 , 5 , 3 } ; int n = x . Length ; Console . WriteLine ( totaldistancesum ( x , y , n ) ) ; } }
Sort an array with swapping only with a special element is allowed | C # program to sort an array by moving one space around . ; n is total number of elements . index is index of 999 or space . k is number of elements yet to be sorted . ; print the sorted array when loop reaches the base case ; else if k > 0 and space is at 0 th index swap each number with space and store index at second last index ; if space is neither start of array nor last element of array and element before it greater than / the element next to space ; first swap space and element next to space in case of { 3 , 999 , 2 } make it { 3 , 2 , 999 } ; than swap space and greater element convert { 3 , 2 , 999 } to { 999 , 2 , 3 } ; Wrapper over sortRec . ; Find index of space ( or 999 ) ; Invalid input ; Driver code
using System ; class GFG { static void sortRec ( int [ ] arr , int index , int k , int n ) { if ( k == 0 ) { for ( int i = 1 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( 999 ) ; return ; } else if ( k > 0 && index == 0 ) { index = n - 2 ; for ( int i = 1 ; i <= index ; i ++ ) { arr [ i - 1 ] = arr [ i ] ; } arr [ index ] = 999 ; } if ( index - 1 >= 0 && index + 1 < n && arr [ index - 1 ] > arr [ index + 1 ] ) { swap ( arr , index , index + 1 ) ; swap ( arr , index - 1 , index + 1 ) ; } else swap ( arr , index , index - 1 ) ; sortRec ( arr , index - 1 , k - 1 , n ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void sort ( int [ ] arr , int n ) { int index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 999 ) { index = i ; break ; } } if ( index == - 1 ) return ; sortRec ( arr , index , n , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 999 , 1 } ; int n = arr . Length ; sort ( arr , n ) ; } }
Find array with k number of merge sort calls | C # program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code
using System ; class GFG { static void unsort ( int l , int r , int [ ] a , int k ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; int temp = a [ mid - 1 ] ; a [ mid - 1 ] = a [ mid ] ; a [ mid ] = temp ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } static void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { Console . WriteLine ( " NO ▁ SOLUTION " ) ; return ; } int [ ] a = new int [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; } }
Program to sort string in descending order | C # program to sort a string of characters in descending order ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , ; so for location of character in count array we wil do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver code
using System ; class GFG { static int MAX_CHAR = 26 ; static void sortString ( String str ) { int [ ] charCount = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { charCount [ str [ i ] - ' a ' ] ++ ; } for ( int i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < charCount [ i ] ; j ++ ) { Console . Write ( ( char ) ( ' a ' + i ) ) ; } } } public static void Main ( String [ ] args ) { String s = " alkasingh " ; sortString ( s ) ; } }
Median after K additional integers | C # program to find median of an array when we are allowed to add additional K integers to it . ; Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; Driver code
using System ; class GFG { static void printMedian ( int [ ] arr , int n , int K ) { Array . Sort ( arr ) ; Console . Write ( arr [ ( n + K ) / 2 ] ) ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 2 , 8 } ; int k = 3 ; int n = arr . Length ; printMedian ( arr , n , k ) ; } }
A sorting algorithm that slightly improves on selection sort | C # program to implement min max selection sort . ; shifting the min . ; Shifting the max . The equal condition happens if we shifted the max to arr [ min_i ] in the previous swap . ; Driver code
using System ; class GFG { static void minMaxSelectionSort ( int [ ] arr , int n ) { for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { int min = arr [ i ] , max = arr [ i ] ; int min_i = i , max_i = i ; for ( int k = i ; k <= j ; k ++ ) { if ( arr [ k ] > max ) { max = arr [ k ] ; max_i = k ; } else if ( arr [ k ] < min ) { min = arr [ k ] ; min_i = k ; } } swap ( arr , i , min_i ) ; if ( arr [ min_i ] == max ) swap ( arr , j , min_i ) ; else swap ( arr , j , max_i ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 23 , 78 , 45 , 8 , 32 , 56 , 1 } ; int n = arr . Length ; minMaxSelectionSort ( arr , n ) ; Console . Write ( " Sorted ▁ array : STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } }
Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Find maximum element of both array ; Increment one two avoid collision of 0 and maximum element of array in modulo operation ; Recover back original element to compare ; Update element by adding multiplication with new number ; Update element by adding multiplication with new number ; Process those elements which are left in array a ; Process those elements which are left in array a ; Finally update elements by dividing with maximum element ; Finally update elements by dividing with maximum element ; Driver code ; Length of a ; length of b ; Function Call
using System ; public class GFG { static void mergeArray ( int [ ] a , int [ ] b , int n , int m ) { int mx = 0 ; for ( int I = 0 ; I < n ; I ++ ) { mx = Math . Max ( mx , a [ I ] ) ; } for ( int I = 0 ; I < m ; I ++ ) { mx = Math . Max ( mx , b [ I ] ) ; } mx ++ ; int i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { int e1 = a [ i ] % mx ; int e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { int el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { int el = b [ j ] % mx ; if ( k < n ) b [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( int In = 0 ; In < n ; In ++ ) a [ In ] = a [ In ] / mx ; for ( int In = 0 ; In < m ; In ++ ) b [ In ] = b [ In ] / mx ; return ; } static public void Main ( ) { int [ ] a = { 3 , 5 , 6 , 8 , 12 } ; int [ ] b = { 1 , 4 , 9 , 13 } ; int n = a . Length ; int m = b . Length ; mergeArray ( a , b , n , m ) ; Console . Write ( " First ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; Console . Write ( " Second ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) Console . Write ( b [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } }
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | C # program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Driver code
using System ; class GFG { static void arrange ( int [ ] arr , int n , int x ) { for ( int i = 1 ; i < n ; i ++ ) { int diff = Math . Abs ( arr [ i ] - x ) ; int j = i - 1 ; if ( Math . Abs ( arr [ j ] - x ) > diff ) { int temp = arr [ i ] ; while ( j >= 0 && Math . Abs ( arr [ j ] - x ) > diff ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } } static void print ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 10 , 5 , 3 , 9 , 2 } ; int n = arr . Length ; int x = 7 ; arrange ( arr , n , x ) ; print ( arr , n ) ; } }
Check if a grid can become row | C # program to check if we can make a grid of character sorted using adjacent swaps . ; v [ ] is vector of strings . len is length of strings in every row . ; Driver code ; int len = 5 ; Length of strings
using System ; class GfG { static Boolean check ( String [ ] v , int len ) { int n = v . Length ; char [ ] tempArray ; for ( int i = 0 ; i < n ; i ++ ) { tempArray = v [ i ] . ToCharArray ( ) ; Array . Sort ( tempArray ) ; v [ i ] = new String ( tempArray ) ; } for ( int i = 0 ; i < len - 1 ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( v [ i ] [ j ] > v [ i + 1 ] [ j ] ) return false ; return true ; } public static void Main ( String [ ] args ) { String [ ] v = { " ebcda " , " ihgfj " , " klmno " , " pqrst " , " yvwxu " } ; if ( check ( v , len ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Sort first half in ascending and second half in descending order | 1 | C # program to print first half in ascending order and the second half in descending order ; function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; Driver code
using System ; class GFG { static void printOrder ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; for ( int j = n - 1 ; j >= n / 2 ; j -- ) Console . Write ( arr [ j ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 } ; int n = arr . Length ; printOrder ( arr , n ) ; } }
Find the Sub | C # program to find subarray with sum closest to 0 ; Function to find the subarray ; Pick a starting point ; Consider current starting point as a subarray and update minimum sum and subarray indexes ; Try all subarrays starting with i ; update minimum sum and subarray indexes ; Return starting and ending indexes ; Drivers code
using System ; class GFG { public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Pair findSubArray ( int [ ] arr , int n ) { int start = 0 , end = 0 , min_sum = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = arr [ i ] ; if ( min_sum > Math . Abs ( curr_sum ) ) { min_sum = Math . Abs ( curr_sum ) ; start = i ; end = i ; } for ( int j = i + 1 ; j < n ; j ++ ) { curr_sum = curr_sum + arr [ j ] ; if ( min_sum > Math . Abs ( curr_sum ) ) { min_sum = Math . Abs ( curr_sum ) ; start = i ; end = j ; } } } Pair p = new Pair ( start , end ) ; return p ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , - 5 , 4 , - 6 , - 3 } ; int n = arr . Length ; Pair point = findSubArray ( arr , n ) ; Console . WriteLine ( " Subarray ▁ starting ▁ from ▁ " + point . first + " ▁ to ▁ " + point . second ) ; } }
Find shortest unique prefix for every word in a given list | Set 2 ( Using Sorting ) | C # program to print shortest unique prefixes for every word . ; create an array to store the results ; sort the array of strings ; compare the first string with its only right neighbor ; Store the unique prefix of a [ 1 ] from its left neighbor ; compute common prefix of a [ i ] unique from its right neighbor ; compare the new prefix with previous prefix ; store the prefix of a [ i + 1 ] unique from its left neighbour ; compute the unique prefix for the last string in sorted array ; Driver code
using System ; class GFG { public String [ ] uniquePrefix ( String [ ] a ) { int size = a . Length ; String [ ] res = new String [ size ] ; Array . Sort ( a ) ; int j = 0 ; while ( j < Math . Min ( a [ 0 ] . Length - 1 , a [ 1 ] . Length - 1 ) ) { if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) j ++ ; else break ; } int ind = 0 ; res [ ind ++ ] = a [ 0 ] . Substring ( 0 , j + 1 ) ; String temp_prefix = a [ 1 ] . Substring ( 0 , j + 1 ) ; for ( int i = 1 ; i < size - 1 ; i ++ ) { j = 0 ; while ( j < Math . Min ( a [ i ] . Length - 1 , a [ i + 1 ] . Length - 1 ) ) { if ( a [ i ] [ j ] == a [ i + 1 ] [ j ] ) j ++ ; else break ; } String new_prefix = a [ i ] . Substring ( 0 , j + 1 ) ; if ( temp_prefix . Length > new_prefix . Length ) res [ ind ++ ] = temp_prefix ; else res [ ind ++ ] = new_prefix ; temp_prefix = a [ i + 1 ] . Substring ( 0 , j + 1 ) ; } j = 0 ; String sec_last = a [ size - 2 ] ; String last = a [ size - 1 ] ; while ( j < Math . Min ( sec_last . Length - 1 , last . Length - 1 ) ) { if ( sec_last [ j ] == last [ j ] ) j ++ ; else break ; } res [ ind ] = last . Substring ( 0 , j + 1 ) ; return res ; } public static void Main ( String [ ] args ) { GFG gfg = new GFG ( ) ; String [ ] input = { " zebra " , " dog " , " duck " , " dove " } ; String [ ] output = gfg . uniquePrefix ( input ) ; Console . WriteLine ( " The ▁ shortest ▁ unique ▁ prefixes " + " ▁ in ▁ sorted ▁ order ▁ are ▁ : " ) ; for ( int i = 0 ; i < output . Length ; i ++ ) Console . Write ( output [ i ] + " ▁ " ) ; } }
Find the minimum and maximum amount to buy all N candies | C # implementation to find the minimum and maximum amount ; Function to find the minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver code ; Function call
using System ; public class GFG { static int findMinimum ( int [ ] arr , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += arr [ i ] ; n = n - k ; } return res ; } static int findMaximum ( int [ ] arr , int n , int k ) { int res = 0 , index = 0 ; for ( int i = n - 1 ; i >= index ; i -- ) { res += arr [ i ] ; index += k ; } return res ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int n = arr . Length ; int k = 2 ; Array . Sort ( arr ) ; Console . WriteLine ( findMinimum ( arr , n , k ) + " ▁ " + findMaximum ( arr , n , k ) ) ; } }
Print array of strings in sorted order without copying one string into another | C # implementation to print an array of strings in sorted order without copying one string into another ; function to print strings in sorted order ; Initially the index of the strings are assigned to the ' index [ ] ' ; selection sort technique is applied ; with the help of ' index [ ] ' strings are being compared ; index of the smallest string is placed at the ith index of ' index [ ] ' ; printing strings in sorted order ; Driver program to test above
using System ; public class GFG { static void printInSortedOrder ( String [ ] arr , int n ) { int [ ] index = new int [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) { index [ i ] = i ; } for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . CompareTo ( arr [ index [ j ] ] ) > 0 ) { min = j ; } } if ( min != i ) { int temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ index [ i ] ] + " ▁ " ) ; } } static public void Main ( ) { String [ ] arr = { " geeks " , " quiz " , " geeks " , " for " } ; int n = 4 ; printInSortedOrder ( arr , n ) ; } }
Find maximum height pyramid from the given array of objects | C # program to find maximum height pyramid from the given object width . ; Returns maximum number of pyramidcal levels n boxes of given widths . ; Sort objects in increasing order of widths ; Total width of previous level and total number of objects in previous level ; Number of object in current level . ; Width of current level . ; Picking the object . So increase current width and number of object . ; If current width and number of object are greater than previous . ; Update previous width , number of object on previous level . ; Reset width of current level , number of object on current level . ; Increment number of level . ; Driver Program
using System ; public class GFG { static int maxLevel ( int [ ] boxes , int n ) { Array . Sort ( boxes ) ; int prev_width = boxes [ 0 ] ; int prev_count = 1 ; int curr_count = 0 ; int curr_width = 0 ; for ( int i = 1 ; i < n ; i ++ ) { curr_width += boxes [ i ] ; curr_count += 1 ; if ( curr_width > prev_width && curr_count > prev_count ) { prev_width = curr_width ; prev_count = curr_count ; curr_count = 0 ; curr_width = 0 ; ans ++ ; } } return ans ; } static public void Main ( ) { int [ ] boxes = { 10 , 20 , 30 , 50 , 60 , 70 } ; int n = boxes . Length ; Console . WriteLine ( maxLevel ( boxes , n ) ) ; } }