codeparrot/xlcost-text-to-code huggingface dataset:
Product of all Subsequences of size K except the minimum and maximum Elements | C # program to find product of all Subsequences of size K except the minimum and maximum Elements ; 2D array to store value of combinations nCr ; Function to pre - calculate value of all combinations nCr ; Function to calculate product of all subsequences except the minimum and maximum elements ; Sorting array so that it becomes easy to calculate the number of times an element will come in first or last place ; An element will occur ' powa ' times in total of which ' powla ' times it will be last element and ' powfa ' times it will be first element ; In total it will come powe = powa - powla - powfa times ; Multiplying a [ i ] powe times using Fermat Little Theorem under MODulo MOD for fast exponentiation ; Driver Code ; pre - calculation of all combinations
using System ; class GFG { static int MOD = 1000000007 ; static int max = 101 ; static long [ , ] C = new long [ max , max ] ; static long power ( long x , long y ) { long res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % MOD ; } y = y >> 1 ; x = ( x * x ) % MOD ; } return res % MOD ; } static void combi ( int n , int k ) { int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = ( C [ i - 1 , j - 1 ] % MOD + C [ i - 1 , j ] % MOD ) % MOD ; } } } static long product ( long [ ] a , int n , int k ) { long ans = 1 ; Array . Sort ( a ) ; long powa = C [ n - 1 , k - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { long powla = C [ i , k - 1 ] ; long powfa = C [ n - i - 1 , k - 1 ] ; long powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD ; long mul = power ( a [ i ] , powe ) % MOD ; ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD ; } return ans % MOD ; } static public void Main ( ) { combi ( 100 , 100 ) ; long [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 3 ; long ans = product ( arr , n , k ) ; Console . WriteLine ( ans ) ; } }
Maximum number of segments that can contain the given points | C # implementation of the approach ; Function to return the maximum number of segments ; Sort both the vectors ; Initially pointing to the first element of b [ ] ; Try to find a match in b [ ] ; The segment ends before b [ j ] ; The point lies within the segment ; The segment starts after b [ j ] ; Return the required count ; Driver code
using System ; class GFG { static int countPoints ( int n , int m , int [ ] a , int [ ] b , int x , int y ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int j = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( a [ i ] + y < b [ j ] ) break ; if ( b [ j ] >= a [ i ] - x && b [ j ] <= a [ i ] + y ) { count ++ ; j ++ ; break ; } else j ++ ; } } return count ; } public static void Main ( ) { int x = 1 , y = 4 ; int [ ] a = { 1 , 5 } ; int n = a . Length ; int [ ] b = { 1 , 1 , 2 } ; int m = a . Length ; Console . WriteLine ( countPoints ( n , m , a , b , x , y ) ) ; } }
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | C # program to merge K sorted arrays ; Merge and sort k arrays ; Put the elements in sorted array . ; Sort the output array ; Driver code ; Input 2D - array ; Number of arrays ; Output array ; Print merged array
using System ; class GFG { static int N = 4 ; static void merge_and_sort ( int [ ] output , int [ , ] arr , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { output [ i * n + j ] = arr [ i , j ] ; } } Array . Sort ( output ) ; } static void Main ( ) { int [ , ] arr = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int n = N ; int k = arr . GetLength ( 0 ) ; int [ ] output = new int [ n * k ] ; merge_and_sort ( output , arr , n , k ) ; for ( int i = 0 ; i < n * k ; i ++ ) Console . Write ( output [ i ] + " ▁ " ) ; } }
Minimum operations of given type to make all elements of a matrix equal | C # implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; will not work for negative elements , so . . adding this ; adding this to handle negative elements too . ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; changed here as in case of even elements there will be 2 medians ; Return minimum operations required ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minOperations ( int n , int m , int k , List < List < int > > matrix ) { List < int > arr = new List < int > ( ) ; int mod ; if ( matrix [ 0 ] [ 0 ] < 0 ) { mod = k - ( Math . Abs ( matrix [ 0 ] [ 0 ] ) % k ) ; } else { mod = matrix [ 0 ] [ 0 ] % k ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr . Add ( matrix [ i ] [ j ] ) ; int val = matrix [ i ] [ j ] ; if ( val < 0 ) { int res = k - ( Math . Abs ( val ) % k ) ; if ( res != mod ) { return - 1 ; } } else { int foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return - 1 ; } } } } arr . Sort ( ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . Abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . Abs ( arr [ i ] - median2 ) / k ; minOperations = Math . Min ( minOperations , minOperations2 ) ; } return minOperations ; } static void Main ( ) { List < List < int > > matrix = new List < List < int > > { new List < int > { 2 , 4 , 6 } , new List < int > { 8 , 10 , 12 } , new List < int > { 14 , 16 , 18 } , new List < int > { 20 , 22 , 24 } , } ; int n = matrix . Count ; int m = matrix [ 0 ] . Count ; int k = 2 ; Console . Write ( minOperations ( n , m , k , matrix ) ) ; } }
Smallest subarray containing minimum and maximum values | C # implementation of above approach ; Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code
using System ; using System . Linq ; public class GFG { static int minSubarray ( int [ ] A , int n ) { int minValue = A . Min ( ) ; int maxValue = A . Max ( ) ; int pos_min = - 1 , pos_max = - 1 , ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != - 1 && pos_min != - 1 ) ans = Math . Min ( ans , Math . Abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } static public void Main ( ) { int [ ] A = { 1 , 5 , 9 , 7 , 1 , 9 , 4 } ; int n = A . Length ; Console . WriteLine ( minSubarray ( A , n ) ) ; } }
Minimum number of consecutive sequences that can be formed in an array | C # program find the minimum number of consecutive sequences in an array ; Driver program ; function call to print required answer
using System ; class GFG { static int countSequences ( int [ ] arr , int n ) { int count = 1 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; return count ; } static public void Main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 3 , 5 , 10 } ; int n = arr . Length ; Console . WriteLine ( countSequences ( arr , n ) ) ; } }
Minimum number of increment / decrement operations such that array contains all elements from 1 to N | C # implementation of the above approach ; Function to find the minimum operations ; Sort the given array ; Count operations by assigning a [ i ] = i + 1 ; Driver Code
using System ; class GFG { static long minimumMoves ( int [ ] a , int n ) { long operations = 0 ; Array . Sort ( a ) ; for ( int i = 0 ; i < n ; i ++ ) operations += ( long ) Math . Abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } static public void Main ( ) { int [ ] arr = { 5 , 3 , 2 } ; int n = arr . Length ; Console . WriteLine ( minimumMoves ( arr , n ) ) ; } }
Print a case where the given sorting algorithm fails | C # program to find a case where the given algorithm fails ; Function to print a case where the given sorting algorithm fails ; only case where it fails ; Driver Code
using System ; class GFG { static void printCase ( int n ) { if ( n <= 2 ) { Console . Write ( - 1 ) ; return ; } for ( int i = n ; i >= 1 ; i -- ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 3 ; printCase ( n ) ; } }
Find the missing elements from 1 to M in given N ranges | C # program to find missing elements from given Ranges ; Function to find missing elements from given Ranges ; First of all sort all the given ranges ; Store ans in a different vector ; prev is use to store end of last range ; j is used as a counter for ranges ; For last segment ; Finally print all answer ; Driver code ; Store ranges in vector of pair
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { Pair first = ( Pair ) a ; Pair second = ( Pair ) b ; if ( first . first == second . first ) { return first . second - second . second ; } return first . first - second . first ; } } public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findMissingNumber ( ArrayList ranges , int m ) { IComparer myComparer = new sortHelper ( ) ; ranges . Sort ( myComparer ) ; ArrayList ans = new ArrayList ( ) ; int prev = 0 ; for ( int j = 0 ; j < ranges . Count ; j ++ ) { int start = ( ( Pair ) ranges [ j ] ) . first ; int end = ( ( Pair ) ranges [ j ] ) . second ; for ( int i = prev + 1 ; i < start ; i ++ ) ans . Add ( i ) ; prev = end ; } for ( int i = prev + 1 ; i <= m ; i ++ ) ans . Add ( i ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { if ( ( int ) ans [ i ] <= m ) Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int M = 6 ; ArrayList ranges = new ArrayList ( ) ; ranges . Add ( new Pair ( 1 , 2 ) ) ; ranges . Add ( new Pair ( 4 , 5 ) ) ; findMissingNumber ( ranges , M ) ; } }
Check whether it is possible to make both arrays equal by modifying a single element | C # implementation of the above approach ; Function to check if both sequences can be made equal ; Sorting both the arrays ; Flag to tell if there are more than one mismatch ; To stores the index of mismatched element ; If there is more than one mismatch then return False ; If there is no mismatch or the difference between the mismatching elements is <= k then return true ; Driver code
using System ; class GFG { static bool check ( int n , int k , int [ ] a , int [ ] b ) { Array . Sort ( a ) ; Array . Sort ( b ) ; bool fl = false ; int ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { if ( fl == true ) { return false ; } fl = true ; ind = i ; } } if ( ind == - 1 | Math . Abs ( a [ ind ] - b [ ind ] ) <= k ) { return true ; } return false ; } public static void Main ( ) { int n = 2 , k = 4 ; int [ ] a = { 1 , 5 } ; int [ ] b = { 1 , 1 } ; if ( check ( n , k , a , b ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Sum of width ( max and min diff ) of all Subsequences | C # implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver Code
using System ; class GFG { static int MOD = 1000000007 ; static int SubseqWidths ( int [ ] A , int n ) { Array . Sort ( A ) ; int [ ] pow2 = new int [ n ] ; pow2 [ 0 ] = 1 ; for ( int i = 1 ; i < n ; ++ i ) pow2 [ i ] = ( pow2 [ i - 1 ] * 2 ) % MOD ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) ans = ( ans + ( pow2 [ i ] - pow2 [ n - 1 - i ] ) * A [ i ] ) % MOD ; return ans ; } static void Main ( ) { int [ ] A = new int [ ] { 5 , 6 , 4 , 3 , 8 } ; int n = A . Length ; Console . WriteLine ( SubseqWidths ( A , n ) ) ; } }
Covering maximum array elements with given value | C # implementation of the approach ; Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; Driver function
using System ; using System . Linq ; class GFG { static int maxArrayCover ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int cc = 0 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += a [ i ] ; if ( s > x ) { break ; } cc += 1 ; } if ( a . Sum ( ) == x ) { return n ; } else { if ( cc == n ) { return n - 1 ; } else { return cc ; } } } public static void Main ( ) { int n = 3 ; int x = 70 ; int [ ] a = new int [ ] { 10 , 20 , 30 } ; Console . WriteLine ( maxArrayCover ( a , n , x ) ) ; } }
Quotient | C # program to implement Quotient - Remainder Sort ; max_element finds maximum element in an array ; min_element finds minimum element in an array ; Creating a ROW * COL matrix of all zeros ; Driver Code
using System ; using System . Linq ; class GFG { static void QRsort ( int [ ] arr , int size ) { int MAX = arr . Max ( ) ; int MIN = arr . Min ( ) ; Console . WriteLine ( " Maximum ▁ Element ▁ found ▁ is ▁ : ▁ " + MAX ) ; Console . WriteLine ( " Minimum ▁ Element ▁ found ▁ is ▁ : ▁ " + MIN ) ; int COL = MIN ; int ROW = MAX / MIN + 1 ; int [ , ] matrix = new int [ ROW , COL ] ; for ( int i = 0 ; i < size ; i ++ ) { int quotient = arr [ i ] / MIN ; int remainder = arr [ i ] % MIN ; matrix [ quotient , remainder ] = arr [ i ] ; } int k = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( matrix [ i , j ] != 0 ) { arr [ k ++ ] = matrix [ i , j ] ; } } } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 7 , 4 , 8 , 2 , 6 } ; int size = arr . Length ; Console . WriteLine ( " Initial ▁ Array ▁ : ▁ " ) ; printArray ( arr , size ) ; QRsort ( arr , size ) ; Console . WriteLine ( " Array ▁ after ▁ sorting ▁ : ▁ " ) ; printArray ( arr , size ) ; } }
Check if a Linked List is Pairwise Sorted | C # program to check if linked list is pairwise sorted ; A linked list node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; The constructed linked list is : 10.15 . 9.9 .1 .5
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node start ; static Boolean isPairWiseSorted ( Node head ) { Boolean flag = true ; Node temp = head ; while ( temp != null && temp . next != null ) { if ( temp . data > temp . next . data ) { flag = false ; break ; } temp = temp . next . next ; } return flag ; } static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; start = head_ref ; } public static void Main ( String [ ] args ) { start = null ; push ( start , 5 ) ; push ( start , 1 ) ; push ( start , 9 ) ; push ( start , 9 ) ; push ( start , 15 ) ; push ( start , 10 ) ; if ( isPairWiseSorted ( start ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Count number of triplets with product equal to given number | Set 2 | C # implementation of above approach ; Function to count such triplets ; Sort the array ; three pointer technique ; Calculate the product of a triplet ; Check if that product is greater than m , decrement mid ; Check if that product is smaller than m , increment start ; Check if that product is equal to m , decrement mid , increment start and increment the count of pairs ; Driver code
using System ; class GFG { static int countTriplets ( int [ ] arr , int n , int m ) { int count = 0 ; Array . Sort ( arr ) ; int end , start , mid ; for ( end = n - 1 ; end >= 2 ; end -- ) { start = 0 ; mid = end - 1 ; while ( start < mid ) { long prod = arr [ end ] * arr [ start ] * arr [ mid ] ; if ( prod > m ) mid -- ; else if ( prod < m ) start ++ ; else if ( prod == m ) { count ++ ; mid -- ; start ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . Length ; int m = 1 ; Console . WriteLine ( countTriplets ( arr , n , m ) ) ; } }
Equally divide into two sets such that one set has maximum distinct elements | C # program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Insert all the resources in the set There will be unique resources in the set ; return minimum of distinct resources and n / 2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int distribution ( int [ ] arr , int n ) { HashSet < int > resources = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) resources . Add ( arr [ i ] ) ; return Math . Min ( resources . Count , n / 2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = arr . Length ; Console . Write ( distribution ( arr , n ) + " STRNEWLINE " ) ; } }
Sort 3 numbers | C # program to sort an array of size 3 ; Insert arr [ 1 ] ; Insert arr [ 2 ] ; Driver Code
using System ; class GFG { static void sort3 ( int [ ] arr , int [ ] temp ) { if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } if ( arr [ 2 ] < arr [ 1 ] ) { temp [ 0 ] = arr [ 1 ] ; arr [ 1 ] = arr [ 2 ] ; arr [ 2 ] = temp [ 0 ] ; if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } } } public static void Main ( String [ ] args ) { int [ ] a = new int [ ] { 10 , 12 , 5 } ; int [ ] temp1 = new int [ 10 ] ; sort3 ( a , temp1 ) ; for ( int i = 0 ; i < 3 ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }
Merge Sort with O ( 1 ) extra space merge and O ( n lg n ) time [ Unsigned Integers Only ] | C # program to sort an array using merge sort such that merge operation takes O ( 1 ) extra space . ; Obtaining actual values ; Recursive merge sort with extra parameter , naxele ; This functions finds max element and calls recursive merge sort . ; Driver code
using System ; using System . Linq ; class GFG { static void merge ( int [ ] arr , int beg , int mid , int end , int maxele ) { int i = beg ; int j = mid + 1 ; int k = beg ; while ( i <= mid && j <= end ) { if ( arr [ i ] % maxele <= arr [ j ] % maxele ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } else { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } } while ( i <= mid ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } while ( j <= end ) { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } for ( i = beg ; i <= end ; i ++ ) arr [ i ] = arr [ i ] / maxele ; } static void mergeSortRec ( int [ ] arr , int beg , int end , int maxele ) { if ( beg < end ) { int mid = ( beg + end ) / 2 ; mergeSortRec ( arr , beg , mid , maxele ) ; mergeSortRec ( arr , mid + 1 , end , maxele ) ; merge ( arr , beg , mid , end , maxele ) ; } } static void mergeSort ( int [ ] arr , int n ) { int maxele = arr . Max ( ) + 1 ; mergeSortRec ( arr , 0 , n - 1 , maxele ) ; } public static void Main ( ) { int [ ] arr = { 999 , 612 , 589 , 856 , 56 , 945 , 243 } ; int n = arr . Length ; mergeSort ( arr , n ) ; Console . WriteLine ( " Sorted ▁ array ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Print triplets with sum less than k | C # program to print triplets with sum smaller than a given value ; Sort input array ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there are total k - j third elements . ; Driver Code
using System ; class GFG { static void printTriplets ( int [ ] arr , int n , int sum ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { for ( int x = j + 1 ; x <= k ; x ++ ) Console . WriteLine ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ x ] ) ; j ++ ; } } } } public static void Main ( ) { int [ ] arr = { 5 , 1 , 3 , 4 , 7 } ; int n = arr . Length ; int sum = 12 ; printTriplets ( arr , n , sum ) ; } }
Sort string of characters using Stack | C # program to sort string of characters using stack ; Function to print the characters in sorted order ; Primary stack ; Secondary stack ; Append first character ; Iterate for all character in string ; i - th character ASCII ; stack 's top element ASCII  ; If greater or equal to top element then push to stack ; If smaller , then push all element to the temporary stack ; Push all greater elements ; Push operation ; Push till the stack is not - empty ; Push the i - th character ; Push the tempstack back to stack ; Print the stack in reverse order ; Driver Code
using System ; using System . Collections ; class GFG { static void printSorted ( string s , int l ) { Stack stack = new Stack ( ) ; Stack tempstack = new Stack ( ) ; stack . Push ( s [ 0 ] ) ; for ( int i = 1 ; i < l ; i ++ ) { int a = s [ i ] ; int b = ( int ) ( ( char ) stack . Peek ( ) ) ; if ( ( a - b ) >= 1 || ( a == b ) ) stack . Push ( s [ i ] ) ; else if ( ( b - a ) >= 1 ) { while ( ( b - a ) >= 1 ) { tempstack . Push ( stack . Peek ( ) ) ; stack . Pop ( ) ; if ( stack . Count > 0 ) b = ( int ) ( ( char ) stack . Peek ( ) ) ; else break ; } stack . Push ( s [ i ] ) ; while ( tempstack . Count > 0 ) { stack . Push ( tempstack . Peek ( ) ) ; tempstack . Pop ( ) ; } } } string answer = " " ; while ( stack . Count > 0 ) { answer = stack . Peek ( ) + answer ; stack . Pop ( ) ; } Console . WriteLine ( answer ) ; } static void Main ( ) { string s = " geeksforgeeks " ; int l = s . Length ; printSorted ( s , l ) ; } }
Check whether an array can be fit into another array rearranging the elements in the array | C # Program to check whether an array can be fit into another array with given condition . ; Returns true if the array A can be fit into array B , otherwise false ; Sort both the arrays ; Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B ; Driver Code
using System ; class GFG { static bool checkFittingArrays ( int [ ] A , int [ ] B , int N ) { Array . Sort ( A ) ; Array . Sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] > B [ i ] ) return false ; return true ; } public static void Main ( ) { int [ ] A = { 7 , 5 , 3 , 2 } ; int [ ] B = { 5 , 4 , 8 , 7 } ; int N = A . Length ; if ( checkFittingArrays ( A , B , N ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Maximise the number of toys that can be purchased with amount K | C # Program to maximize the number of toys with K amount ; This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment count ; Driver Code
using System ; class GFG { static int maximum_toys ( int [ ] cost , int N , int K ) { int count = 0 , sum = 0 ; Array . Sort ( cost ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( sum + cost [ i ] <= K ) { sum = sum + cost [ i ] ; count ++ ; } } return count ; } public static void Main ( ) { int K = 50 ; int [ ] cost = { 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 } ; int N = cost . Length ; Console . Write ( maximum_toys ( cost , N , K ) ) ; } }