codeparrot/xlcost-text-to-code huggingface dataset:
Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | C # program for the above approach ; Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Driver Code ; Given array and K ; Function call
using System ; using System . Collections . Generic ; class GFG { static int make_cuts ( int [ ] arr , int n , int K ) { int ans = 0 ; List < int > poss = new List < int > ( ) ; int ce = 0 ; int co = 0 ; for ( int x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . Add ( Math . Abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } poss . Sort ( ) ; foreach ( int x in poss ) { if ( K >= x ) { ans ++ ; K -= x ; } else break ; } return ans ; } public static void Main ( String [ ] args ) { int N = 6 ; int K = 4 ; int [ ] arr = { 1 , 2 , 5 , 10 , 15 , 20 } ; Console . Write ( make_cuts ( arr , N , K ) ) ; } }
Cost of rearranging the array such that no element exceeds the sum of its adjacent elements | C # program to implement the above approach ; Function to check if given elements can be arranged such that sum of its neighbors is strictly greater ; Initialize the total cost ; Storing the original index of elements in a hashmap ; Sort the given array ; Check if a given condition is satisfies or not ; First number ; Add the cost to overall cost ; Last number ; Add the cost to overall cost ; Add the cost to overall cost ; Printing the cost ; Driver Code ; Given array ; Function call
using System ; using System . Collections . Generic ; class GFG { static void Arrange ( int [ ] arr , int n ) { int cost = 0 ; Dictionary < int , int > index = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { index . Add ( arr [ i ] , i ) ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] > arr [ i + 1 ] + arr [ n - 1 ] ) { Console . Write ( " - 1" ) ; return ; } else { cost += Math . Abs ( index [ arr [ i ] ] - i ) ; } } else if ( i == n - 1 ) { if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) { Console . Write ( " - 1" ) ; return ; } else { cost += Math . Abs ( index [ arr [ i ] ] - i ) ; } } else { if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) { Console . Write ( " - 1" ) ; return ; } else { cost += Math . Abs ( index [ arr [ i ] ] - i ) ; } } } Console . Write ( cost ) ; return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 1 , 3 } ; int N = arr . Length ; Arrange ( arr , N ) ; } }
XOR of all possible pairwise sum from two given Arrays | C # program to implement the above approach ; Function to calculate the sum of XOR of the sum of every pair ; Stores the XOR of sums of every pair ; Iterate to generate all possible pairs ; Update XOR ; Return the answer ; Driver Code
using System ; class GFG { static int XorSum ( int [ ] A , int [ ] B , int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { ans = ans ^ ( A [ i ] + B [ j ] ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 6 , 0 , 0 , 3 , 3 } ; int [ ] B = { 0 , 5 , 6 , 5 , 0 , 3 } ; int N = A . Length ; Console . WriteLine ( XorSum ( A , B , N ) ) ; } }
Maximum non | C # program for the above approach ; Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the readonly answer ; Count this character if freq 1 ; Return count of distinct characters ; Driver Code ; Given String ; Given k ; Function call
using System ; using System . Collections . Generic ; class GFG { static int maxDistinctChar ( char [ ] s , int n , int k ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( ( int ) s [ i ] ) ) { freq [ ( int ) s [ i ] ] = freq [ ( int ) s [ i ] ] + 1 ; } else { freq . Add ( ( int ) s [ i ] , 1 ) ; } } List < int > v = new List < int > ( ) ; foreach ( KeyValuePair < int , int > it in freq ) { v . Add ( it . Value ) ; } v . Sort ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { int mn = Math . Min ( v [ i ] - 1 , k ) ; v [ i ] = v [ i ] - mn ; k -= mn ; } if ( k > 0 ) { for ( int i = 0 ; i < v . Count ; i ++ ) { int mn = Math . Min ( v [ i ] , k ) ; v [ i ] = v [ i ] - mn ; k -= mn ; } } int res = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) if ( v [ i ] == 1 ) res ++ ; return res ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . Length ; int K = 1 ; Console . Write ( maxDistinctChar ( S . ToCharArray ( ) , N , K ) ) ; } }
Split N as the sum of K numbers satisfying the given conditions | C # program to implement the above approach ; List to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; Boolean array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > primes = new List < int > ( ) ; static void SieveOfEratosthenes ( ) { bool [ ] prime = new bool [ 10005 ] ; for ( int i = 0 ; i < 10005 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= 1000 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 1000 ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= 1000 ; p ++ ) if ( prime [ p ] ) primes . Add ( p ) ; } static void generate ( int n , int k ) { List < int > prod = new List < int > ( ) ; SieveOfEratosthenes ( ) ; int l = primes . Count ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i + 1 ; j < l ; j ++ ) { if ( primes [ i ] * primes [ j ] > 0 ) prod . Add ( primes [ i ] * primes [ j ] ) ; } } prod . Sort ( ) ; int sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) sum += prod [ i ] ; if ( sum > n ) Console . Write ( " - 1" ) ; else { for ( int i = 0 ; i < k - 1 ; i ++ ) { Console . Write ( prod [ i ] + " , ▁ " ) ; } Console . Write ( n - sum + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int n = 52 , k = 5 ; generate ( n , k ) ; } }
Maximum modified Array sum possible by choosing elements as per the given conditions | C # program for the above approach ; Function that finds the maximum sum of the array elements according to the given condition ; Sort the array ; Take the max value from the array ; Iterate from the end of the array ; Check for the number had come before or not ; Take the value which is not occured already ; Change max to new value ; Return the maximum sum ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static int findMaxValue ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = arr [ n - 1 ] ; int maxPossible = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( maxPossible > 0 ) { if ( arr [ i ] >= maxPossible ) { ans += ( maxPossible - 1 ) ; maxPossible = maxPossible - 1 ; } else { maxPossible = arr [ i ] ; ans += maxPossible ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 4 , 1 , 5 } ; int n = arr . Length ; Console . Write ( findMaxValue ( arr , n ) ) ; } }
Maximize the last Array element as per the given conditions | C # Program to implement the above approach ; Function to find the maximum possible value that can be placed at the last index ; Sort the array elements in ascending order ; If the first element is is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code
using System ; class GFG { public static int maximizeFinalElement ( int [ ] arr , int n ) { Array . Sort ( arr ) ; if ( arr [ 0 ] != 1 ) arr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 1 ) { arr [ i ] = arr [ i - 1 ] + 1 ; } } return arr [ n - 1 ] ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 3 , 1 , 3 , 4 } ; int max = maximizeFinalElement ( arr , n ) ; Console . WriteLine ( max ) ; } }
Find the point on X | C # program to implement the above approach ; Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
using System ; class GFG { static int findLeastDist ( int [ ] A , int N ) { Array . Sort ( A ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } public static void Main ( string [ ] args ) { int [ ] A = { 4 , 1 , 5 , 10 , 2 } ; int N = A . Length ; Console . Write ( " ( " + findLeastDist ( A , N ) + " , ▁ " + 0 + " ) " ) ; } }
Maximum sum of any submatrix of a Matrix which is sorted row | C # program for the above approach ; Function that finds the maximum Sub - Matrix Sum ; Number of rows in the matrix ; Number of columns in the matrix ; [ , ] dp matrix to store the results of each iteration ; Base Case - The largest element in the matrix ; To stores the readonly result ; Find the max sub matrix sum for the last row ; Check whether the current sub - array yields maximum sum ; Calculate the max sub matrix sum for the last column ; Check whether the current sub - array yields maximum sum ; Build the [ , ] dp matrix from bottom to the top row ; Update sum at each cell in [ , ] dp ; Update the maximum sum ; Return the maximum sum ; Driver Code ; Given matrix [ , ] mat ; Function Call
using System ; class GFG { static int maxSubMatSum ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int i , j ; int [ , ] dp = new int [ n , m ] ; dp [ n - 1 , m - 1 ] = mat [ n - 1 , m - 1 ] ; int res = dp [ n - 1 , m - 1 ] ; for ( i = m - 2 ; i >= 0 ; i -- ) { dp [ n - 1 , i ] = mat [ n - 1 , i ] + dp [ n - 1 , i + 1 ] ; res = Math . Max ( res , dp [ n - 1 , i ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { dp [ i , m - 1 ] = mat [ i , m - 1 ] + dp [ i + 1 , m - 1 ] ; res = Math . Max ( res , dp [ i , m - 1 ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = m - 2 ; j >= 0 ; j -- ) { dp [ i , j ] = mat [ i , j ] + dp [ i , j + 1 ] + dp [ i + 1 , j ] - dp [ i + 1 , j + 1 ] ; res = Math . Max ( res , dp [ i , j ] ) ; } } return res ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { - 6 , - 4 , - 1 } , { - 3 , 2 , 4 } , { 2 , 5 , 8 } } ; Console . Write ( maxSubMatSum ( mat ) ) ; } }
Arrange the numbers in the Array as per given inequalities | C # program for the above approach ; Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the readonly integer ; Return the answer ; Driver Code ; Given List of ints ; Given list of inequalities ; Function call ; Print the output
using System ; using System . Text ; class GFG { static String formAnInequality ( int [ ] integers , char [ ] inequalities ) { Array . Sort ( integers ) ; int lowerIndex = 0 ; int higherIndex = integers . Length - 1 ; StringBuilder sb = new StringBuilder ( ) ; foreach ( char ch in inequalities ) { if ( ch == ' < ' ) { sb . Append ( " ▁ " + integers [ lowerIndex ++ ] + " ▁ " + ch ) ; } else { sb . Append ( " ▁ " + integers [ higherIndex -- ] + " ▁ " + ch ) ; } } sb . Append ( " ▁ " + integers [ lowerIndex ] ) ; return sb . ToString ( ) ; } public static void Main ( String [ ] args ) { int [ ] integers = { 2 , 5 , 1 , 0 } ; char [ ] inequalities = { ' < ' , ' > ' , ' < ' } ; String output = formAnInequality ( integers , inequalities ) ; Console . WriteLine ( output ) ; } }
Check if two arrays can be made equal by reversing subarrays multiple times | C # implementation to check if two arrays can be made equal ; Function to check if array B can be made equal to array A ; Sort both the arrays ; Check if both the arrays are equal or not ; Driver code
using System ; class GFG { static bool canMadeEqual ( 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 = { 1 , 2 , 3 } ; int [ ] B = { 1 , 3 , 2 } ; int n = A . Length ; if ( canMadeEqual ( A , B , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Minimize difference between maximum and minimum of Array by at most K replacements | C # program of the approach ; Function to find minimum difference between the maximum and the minimum elements arr [ ] by at most K replacements ; Check if turns are more than or equal to n - 1 then simply return zero ; Sort the array ; Set difference as the maximum possible difference ; Iterate over the array to track the minimum difference in k turns ; Return the answer ; Driver Code ; Given array arr [ ] ; Given K replacements ; Function Call
using System ; class GFG { static int maxMinDifference ( int [ ] arr , int n , int k ) { if ( k >= n - 1 ) return 0 ; Array . Sort ( arr ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; for ( int i = k , j = n - 1 ; i >= 0 ; -- i , -- j ) { ans = Math . Min ( arr [ j ] - arr [ i ] , ans ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 4 , 6 , 11 , 15 } ; int N = arr . Length ; int K = 3 ; Console . Write ( maxMinDifference ( arr , N , K ) ) ; } }
Check if a decreasing Array can be sorted using Triple cyclic shift | C # program for the above approach ; If array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; Check if its possible to sort ; Number of swap is N / 2 always for this approach ; Printing index of the cyclic right shift ; Driver code
using System ; class GFG { static void sortarray ( int [ ] arr , int N ) { if ( N == 3 ) Console . WriteLine ( " NO " ) ; else if ( N % 4 == 0 N % 4 == 1 ) { Console . WriteLine ( " YES " ) ; Console . WriteLine ( N / 2 ) ; int k = 1 ; for ( int l = 0 ; l < ( N / 4 ) ; l ++ ) { Console . WriteLine ( k + " ▁ " + ( k + 1 ) + " ▁ " + N ) ; Console . WriteLine ( k + 1 + " ▁ " + N + " ▁ " + ( N - 1 ) ) ; k = k + 2 ; N = N - 2 ; } } else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int N = 5 ; int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; sortarray ( arr , N ) ; } }
Find least non | C # program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Create a visited array ; Find the first missing value ; Driver code
using System ; class GFG { static int MAX = ( int ) ( 1e5 + 5 ) ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_missing ( pair [ ] interval ) { int [ ] vis = new int [ MAX ] ; for ( int i = 0 ; i < interval . Length ; ++ i ) { int start = interval [ i ] . first ; int end = interval [ i ] . second ; vis [ start ] ++ ; vis [ end + 1 ] -- ; } for ( int i = 1 ; i < MAX ; i ++ ) { vis [ i ] += vis [ i - 1 ] ; if ( vis [ i ] == 0 ) { Console . Write ( i + " STRNEWLINE " ) ; return ; } } } public static void Main ( String [ ] args ) { pair [ ] interval = { new pair ( 0 , 14 ) , new pair ( 86 , 108 ) , new pair ( 22 , 30 ) , new pair ( 5 , 17 ) } ; find_missing ( interval ) ; } }
Find least non | C # program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; Check if any missing value exist ; Finally print the missing value ; Driver code
using System ; using System . Collections . Generic ; class GFG { class Pair : IComparable < Pair > { public int start , end ; public Pair ( int s , int e ) { start = s ; end = e ; } public int CompareTo ( Pair p ) { return this . start - p . start ; } } static void findMissing ( List < Pair > interval ) { interval . Sort ( ) ; int mx = 0 ; for ( int i = 0 ; i < interval . Count ; ++ i ) { if ( interval [ i ] . start > mx ) { Console . WriteLine ( mx ) ; return ; } else mx = Math . Max ( mx , interval [ i ] . end + 1 ) ; } Console . WriteLine ( mx ) ; } public static void Main ( String [ ] args ) { List < Pair > interval = new List < Pair > ( ) ; interval . Add ( new Pair ( 0 , 14 ) ) ; interval . Add ( new Pair ( 86 , 108 ) ) ; interval . Add ( new Pair ( 22 , 30 ) ) ; interval . Add ( new Pair ( 5 , 17 ) ) ; findMissing ( interval ) ; } }
Maximum bitwise OR value of subsequence of length K | C # program for the above approach ; Function to convert bit array to decimal number ; Return the final result ; Function to find the maximum Bitwise OR value of subsequence of length K ; Initialize bit array of size 32 with all value as 0 ; Iterate for each index of bit [ ] array from 31 to 0 , and check if the ith value of bit array is 0 ; Check for maximum contributing element ; Update the bit array if max_contributing element is found ; Decrement the value of K ; Return the result ; Driver Code ; Given array arr [ ] ; Length of subsequence ; Function call
using System ; class GFG { static int build_num ( int [ ] bit ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == 1 ) ans += ( 1 << i ) ; ans += 32 ; return ans ; } static int maximumOR ( int [ ] arr , int n , int k ) { int [ ] bit = new int [ 32 ] ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( bit [ i ] == 0 && k > 0 ) { int temp = build_num ( bit ) ; int temp1 = temp ; int val = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( temp1 < ( temp arr [ j ] ) ) { temp1 = temp | arr [ j ] ; val = arr [ j ] ; } } if ( val != - 1 ) { k -- ; for ( int j = 0 ; j < 32 ; j ++ ) { bit [ j ] ++ ; } } } } return build_num ( bit ) ; } public static void Main ( ) { int [ ] arr = { 5 , 9 , 7 , 19 } ; int k = 3 ; int n = arr . Length ; Console . Write ( maximumOR ( arr , n , k ) ) ; } }
Lexicographically smallest string after M operations | C # implementation to find the lexicographical smallest String after performing M operations ; Function to find the lexicographical smallest String after performing M operations ; Size of the given String ; Declare an array a ; For each i , a [ i ] contain number of operations to update s [ i ] to ' a ' ; Check if m >= ar [ i ] , then update s [ i ] to ' a ' decrement k by a [ i ] ; Form a cycle of 26 ; Update last element of String with the value s [ i ] + ( k % 26 ) ; Return the answer ; Driver code
using System ; class GFG { static void smallest_String ( char [ ] s , int m ) { int n = s . Length ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int distance = s [ i ] - ' a ' ; if ( distance == 0 ) a [ i ] = 0 ; else a [ i ] = 26 - distance ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m >= a [ i ] ) { s [ i ] = ' a ' ; m = m - a [ i ] ; } } m = m % 26 ; s [ n - 1 ] = ( char ) ( s [ n - 1 ] + m ) ; Console . Write ( String . Join ( " " , s ) ) ; } public static void Main ( String [ ] args ) { String str = " aazzx " ; int m = 6 ; smallest_String ( str . ToCharArray ( ) , m ) ; } }
Maximize jobs that can be completed under given constraint | C # Program to implement the above approach ; Function to find maxiumum number of jobs ; Min Heap ; Sort ranges by start day ; Stores the minimum and maximum day in the ranges ; Iterating from min_day to max_day ; Insert the end day of the jobs which can be completed on i - th day in a priority queue ; Pop all jobs whose end day is less than current day ; If queue is empty , no job can be completed on the i - th day ; Increment the count of jobs completed ; Pop the job with least end day ; Return the jobs on the last day ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int find_maximum_jobs ( int N , List < Tuple < int , int > > ranges ) { List < int > queue = new List < int > ( ) ; ranges . Sort ( ) ; int min_day = ranges [ 0 ] . Item1 ; int max_day = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_day = Math . Max ( max_day , ranges [ i ] . Item2 ) ; int index = 0 , count_jobs = 0 ; for ( int i = min_day ; i <= max_day ; i ++ ) { while ( index < ranges . Count && ranges [ index ] . Item1 <= i ) { queue . Add ( ranges [ index ] . Item2 ) ; index ++ ; } queue . Sort ( ) ; while ( queue . Count > 0 && queue [ 0 ] < i ) queue . RemoveAt ( 0 ) ; if ( queue . Count == 0 ) continue ; count_jobs ++ ; queue . RemoveAt ( 0 ) ; } return count_jobs ; } static void Main ( ) { int N = 5 ; List < Tuple < int , int > > ranges = new List < Tuple < int , int > > ( ) ; ranges . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; ranges . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; ranges . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; ranges . Add ( new Tuple < int , int > ( 2 , 3 ) ) ; ranges . Add ( new Tuple < int , int > ( 2 , 3 ) ) ; Console . Write ( find_maximum_jobs ( N , ranges ) ) ; } }
Minimize number of boxes by putting small box inside bigger one | C # implementation to minimize the number of the box by putting small box inside the bigger one ; Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; Check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code
using System ; class GFG { public static void minBox ( int [ ] arr , int n ) { int box = n ; Array . Sort ( arr ) ; int curr_box = 0 , next_box = 1 ; while ( curr_box < n && next_box < n ) { if ( arr [ curr_box ] < arr [ next_box ] ) { box -- ; curr_box ++ ; next_box ++ ; } else if ( arr [ curr_box ] == arr [ next_box ] ) next_box ++ ; } Console . WriteLine ( box ) ; } public static void Main ( String [ ] args ) { int [ ] size = { 1 , 2 , 3 } ; int n = size . Length ; minBox ( size , n ) ; } }