codeparrot/xlcost-text-to-code huggingface dataset:
Kth highest XOR of diagonal elements from a Matrix | C # program to implement the above approach ; Function to find K - th maximum XOR of any diagonal in the matrix ; Number or rows ; Number of columns ; Store XOR of diagonals ; Traverse each diagonal ; Starting column of diagonal ; Count total elements in the diagonal ; Store XOR of current diagonal ; Push XOR of current diagonal ; Sort XOR values of diagonals ; Print the K - th Maximum XOR ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findXOR ( int [ , ] mat , int K ) { int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; List < int > digXOR = new List < int > ( ) ; for ( int l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { int s_col = Math . Max ( 0 , l - N ) ; int count = Math . Min ( l , Math . Min ( ( M - s_col ) , N ) ) ; int currXOR = 0 ; for ( int j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ Math . Min ( N , l ) - j - 1 , s_col + j ] ) ; } digXOR . Add ( currXOR ) ; } digXOR . Sort ( ) ; Console . Write ( digXOR [ N + M - 1 - K ] ) ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int K = 3 ; findXOR ( mat , K ) ; } }
Maximum sum of absolute differences between distinct pairs of a triplet from an array | C # program for the above approach ; Function to find the maximum sum of absolute differences between distinct pairs of triplet in array ; Stores the maximum sum ; Sort the array in ascending order ; Sum of differences between pairs of the triplet ; Print the sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maximumSum ( int [ ] arr , int N ) { int sum ; Array . Sort ( arr ) ; sum = ( arr [ N - 1 ] - arr [ 0 ] ) + ( arr [ N - 2 ] - arr [ 0 ] ) + ( arr [ N - 1 ] - arr [ N - 2 ] ) ; Console . Write ( sum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 2 } ; int N = arr . Length ; maximumSum ( arr , N ) ; } }
Maximum and minimum sum of Bitwise XOR of pairs from an array | C # program for the above approach ; Function to find the required sum ; Keeps the track of the visited array elements ; Stores the result ; Keeps count of visited elements ; Traverse the vector , V ; If n elements are visited , break out of the loop ; Store the pair ( i , j ) and their Bitwise XOR ; If i and j both are unvisited ; Add xorResult to res and mark i and j as visited ; Increment count by 2 ; Return the result ; Function to find the maximum and minimum possible sum of Bitwise XOR of all the pairs from the array ; Stores the XOR of all pairs ( i , j ) ; Store the XOR of all pairs ( i , j ) ; Update Bitwise XOR ; Sort the vector ; Initialize variables to store maximum and minimum possible sums ; Find the minimum sum possible ; Reverse the vector , v ; Find the maximum sum possible ; Print the result ; Driver code
using System ; using System . Collections . Generic ; class pair : IComparable < pair > { public int first , second , third ; public pair ( int first , int second , int third ) { this . first = first ; this . second = second ; this . third = third ; } public int CompareTo ( pair p ) { return this . second - p . first ; } } class GFG { static int findSum ( List < pair > v , int n ) { Dictionary < int , Boolean > um = new Dictionary < int , Boolean > ( ) ; int res = 0 ; int cnt = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( cnt == n ) break ; int x = v [ i ] . second ; int y = v [ i ] . third ; int xorResult = v [ i ] . first ; if ( ! um . ContainsKey ( x ) && ! um . ContainsKey ( y ) ) { res += xorResult ; um . Add ( x , true ) ; um . Add ( y , true ) ; cnt += 2 ; } } return res ; } static void findMaxMinSum ( int [ ] a , int n ) { List < pair > v = new List < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xorResult = a [ i ] ^ a [ j ] ; v . Add ( new pair ( xorResult , a [ i ] , a [ j ] ) ) ; } } v . Sort ( ) ; int maxi = 0 , mini = 0 ; mini = findSum ( v , n ) ; v . Reverse ( ) ; maxi = findSum ( v , n ) ; Console . Write ( mini + " ▁ " + maxi ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; findMaxMinSum ( arr , N ) ; } }
Lexicographically smallest permutation having maximum sum of differences between adjacent elements | C # program to implement the above approach ; Function to find the lexicographically smallest permutation of an array such that the sum of the difference between adjacent elements is maximum ; Stores the size of the array ; Sort the given array in increasing order ; Swap the first and last array elements ; Print the required permutation ; Driver Code
using System ; class GFG { static void maximumSumPermutation ( int [ ] arr ) { int N = arr . Length ; Array . Sort ( arr ) ; int temp = arr [ 0 ] ; arr [ 0 ] = arr [ N - 1 ] ; arr [ N - 1 ] = temp ; foreach ( int i in arr ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; maximumSumPermutation ( arr ) ; } }
Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | C # program for the above approach ; Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Print the answer ; Driver Code
using System ; public class GFG { static void findMaxNumbers ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int opr = ( int ) Math . Ceiling ( ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } Console . Write ( cnt ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 8 , 4 , 7 } ; int N = arr . Length ; int K = 5 ; findMaxNumbers ( arr , N , K ) ; } }
Mean of given array after removal of K percent of smallest and largest array elements | C # program for the above approach ; Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
using System ; class GFG { static void meanOfRemainingElements ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int kthPercent = ( N * K ) / 100 ; float sum = 0f ; for ( int i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; float mean = ( sum / ( N - 2 * kthPercent ) ) ; Console . WriteLine ( Math . Round ( mean , 5 ) ) ; } public static void Main ( ) { int [ ] arr = { 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 } ; int arr_size = arr . Length ; int K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ; } }
Make the array elements equal by performing given operations minimum number of times | C # Program for the above approach ; Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Driver Code ; Given array ; Size of the array
using System ; public class GFG { static void minOperation ( int [ ] a , int N ) { int totOps = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { totOps += Math . Abs ( a [ i ] - a [ i + 1 ] ) ; } int maxOps = Math . Max ( Math . Abs ( a [ 0 ] - a [ 1 ] ) , Math . Abs ( a [ N - 1 ] - a [ N - 2 ] ) ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxOps = Math . Max ( maxOps , Math . Abs ( a [ i ] - a [ i - 1 ] ) + Math . Abs ( a [ i ] - a [ i + 1 ] ) - Math . Abs ( a [ i - 1 ] - a [ i + 1 ] ) ) ; } Console . WriteLine ( totOps - maxOps ) ; } static public void Main ( ) { int [ ] arr = { 1 , - 1 , 0 , 1 , 1 } ; int N = arr . Length ; minOperation ( arr , N ) ; } }
Find the segment that overlaps with maximum number of segments | C # program for the above approach ; Function to find the segment which overlaps with maximum number of segments ; ' L ' & ' R ' co - ordinates of all segments are stored in lvalues & rvalues ; Assign co - ordinates ; Co - ordinate compression ; Stores the required segment ; Stores the current maximum number of intersections ; Find number of ' R ' coordinates which are less than the ' L ' value of the current segment ; Find number of ' L ' coordinates which are greater than the ' R ' value of the current segment ; Segments excluding ' lesser ' and ' greater ' gives the number of intersections ; Update the current maximum ; Print segment coordinates ; Driver Code ; Given segments ; Size of segments array
using System ; public class GFG { static void maxIntersection ( int [ , ] segments , int N ) { int [ ] rvalues = new int [ N ] ; int [ ] lvalues = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { lvalues [ i ] = segments [ i , 0 ] ; rvalues [ i ] = segments [ i , 1 ] ; } Array . Sort ( lvalues ) ; Array . Sort ( rvalues ) ; int [ ] answer = { - 1 , - 1 } ; int numIntersections = 0 ; for ( int i = 0 ; i < N ; ++ i ) { int lesser = lower_bound ( rvalues , 0 , segments . GetLength ( 0 ) , segments [ i , 0 ] ) ; int greater = Math . Max ( 0 , N - ( upper_bound ( lvalues , 0 , segments . GetLength ( 0 ) , segments [ i , 1 ] ) ) ) ; if ( ( N - lesser - greater ) >= numIntersections ) { answer = new int [ ] { segments [ i , 0 ] , segments [ i , 1 ] } ; numIntersections = ( N - lesser - greater ) ; } } Console . Write ( answer [ 0 ] + " ▁ " + answer [ 1 ] ) ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } 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 [ , ] segments = { { 1 , 4 } , { 2 , 3 } , { 3 , 6 } } ; int N = segments . GetLength ( 0 ) ; maxIntersection ( segments , N ) ; } }
Count swaps required to sort an array using Insertion Sort | C # program for the above approach ; Stores the sorted array elements ; Function to count the number of swaps required to merge two sorted subarray in a sorted form ; Stores the count of swaps ; Function to count the total number of swaps required to sort the array ; Stores the total count of swaps required ; Find the middle index splitting the two halves ; Count the number of swaps required to sort the left subarray ; Count the number of swaps required to sort the right subarray ; Count the number of swaps required to sort the two sorted subarrays ; Driver Code
using System ; class GFG { static int [ ] temp = new int [ 100000 ] ; static int merge ( int [ ] A , int left , int mid , int right ) { int swaps = 0 ; int i = left , j = mid , k = left ; while ( i < mid && j <= right ) { if ( A [ i ] <= A [ j ] ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } else { temp [ k ] = A [ j ] ; k ++ ; j ++ ; swaps += mid - i ; } } while ( i < mid ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } while ( j <= right ) { temp [ k ] = A [ j ] ; k ++ ; j ++ ; } while ( left <= right ) { A [ left ] = temp [ left ] ; left ++ ; } return swaps ; } static int mergeInsertionSwap ( int [ ] A , int left , int right ) { int swaps = 0 ; if ( left < right ) { int mid = left + ( right - left ) / 2 ; swaps += mergeInsertionSwap ( A , left , mid ) ; swaps += mergeInsertionSwap ( A , mid + 1 , right ) ; swaps += merge ( A , left , mid + 1 , right ) ; } return swaps ; } static public void Main ( ) { int [ ] A = { 2 , 1 , 3 , 1 , 2 } ; int N = A . Length ; Console . WriteLine ( mergeInsertionSwap ( A , 0 , N - 1 ) ) ; } }
Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | C # program to implement the above approach ; Function to maximize the count of elements required to be removed from arr [ ] such that the sum of arr [ ] is greater than or equal to sum of the array brr [ ] ; Sort the array arr [ ] ; Stores index of smallest element of arr [ ] ; Stores sum of elements of the array arr [ ] ; Traverse the array arr [ ] ; Update sumArr ; Stores sum of elements of the array brr [ ] ; Traverse the array brr [ ] ; Update sumArr ; Stores count of removed elements ; Repeatedly remove the smallest element of arr [ ] ; Update sumArr ; Remove the smallest element ; If the sum of remaining elements in arr [ ] >= sum of brr [ ] ; Update cntRemElem ; Driver Code
using System ; class GFG { static int maxCntRemovedfromArray ( int [ ] arr , int N , int [ ] brr , int M ) { Array . Sort ( arr ) ; int i = 0 ; int sumArr = 0 ; for ( i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } int sumBrr = 0 ; for ( i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } int cntRemElem = 0 ; while ( i < N && sumArr >= sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } static public void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 4 , 6 } ; int [ ] brr = new int [ ] { 7 } ; int N = arr . Length ; int M = brr . Length ; Console . WriteLine ( maxCntRemovedfromArray ( arr , N , brr , M ) ) ; } }
Find any two pairs ( a , b ) and ( c , d ) such that a d | C # implementation to sort the array of points by its distance from the given point ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; Driver code
using System ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPair ( pair [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int a = arr [ i ] . first , b = arr [ i ] . second ; for ( int j = i + 1 ; j < N ; j ++ ) { int c = arr [ j ] . first , d = arr [ j ] . second ; if ( a < c && b > d ) { Console . WriteLine ( " ( " + a + " ▁ " + b + " ) , ▁ ( " + c + " ▁ " + d + " ) " ) ; return ; } } } Console . WriteLine ( " NO ▁ SUCH ▁ PAIR ▁ EXIST " ) ; } public static void Main ( String [ ] args ) { pair [ ] arr = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findPair ( arr , 5 ) ; } }
Find any two pairs ( a , b ) and ( c , d ) such that a d | C # program for the above approach ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code
using System ; public class GFG { class pair : IComparable < pair > { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int CompareTo ( pair p ) { return this . first - p . first ; } } static void findpair ( pair [ ] arr , int N ) { Array . Sort ( arr ) ; for ( int i = 1 ; i < N ; i ++ ) { int b = arr [ i - 1 ] . second ; int d = arr [ i ] . second ; if ( b > d ) { Console . Write ( " ( " + arr [ i - 1 ] . first + " ▁ " + b + " ) , ▁ ( " + arr [ i ] . first + " ▁ " + d + " ) " ) ; return ; } } Console . Write ( " NO ▁ SUCH ▁ PAIR ▁ EXIST STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { pair [ ] arr = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findpair ( arr , 5 ) ; } }
Print indices in non | C # program to implement the above approach ; Function to print the order of array elements generating non - decreasing quotient after division by X ; Stores the quotient and the order ; Traverse the array ; Sort the vector ; Print the order ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printOrder ( int [ ] order , int N , int X ) { List < Tuple < int , int > > vect = new List < Tuple < int , int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( order [ i ] % X == 0 ) { vect . Add ( new Tuple < int , int > ( ( order [ i ] / X ) , i + 1 ) ) ; } else { vect . Add ( new Tuple < int , int > ( ( order [ i ] / X + 1 ) , i + 1 ) ) ; } } vect . Sort ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( vect [ i ] . Item2 + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( ) { int N = 3 , X = 3 ; int [ ] order = { 2 , 7 , 4 } ; printOrder ( order , N , X ) ; } }
Shuffle 2 n integers as a1 | C # program for the above approach ; Function to reverse the array from the position ' start ' to position ' end ' ; Stores mid of start and end ; Traverse the array in the range [ start , end ] ; Stores arr [ start + i ] ; Update arr [ start + i ] ; Update arr [ end - i ] ; Utility function to shuffle the given array in the of form { a1 , b1 , a2 , b2 , ... . an , bn } ; Stores the length of the array ; If length of the array is 2 ; Stores mid of the { start , end } ; Divide array into two halves of even length ; Update mid ; Calculate the mid - points of both halves of the array ; Reverse the subarray made from mid1 to mid2 ; Reverse the subarray made from mid1 to mid ; Reverse the subarray made from mid to mid2 ; Recursively calls for both the halves of the array ; Function to shuffle the given array in the form of { a1 , b1 , a2 , b2 , ... . an , bn } ; Function Call ; Print the modified array ; Driver Code ; Given array ; Size of the array ; Shuffles the given array to the required permutation
using System ; public class GFG { static void reverse ( int [ ] arr , int start , int end ) { int mid = ( end - start + 1 ) / 2 ; for ( int i = 0 ; i < mid ; i ++ ) { int temp = arr [ start + i ] ; arr [ start + i ] = arr [ end - i ] ; arr [ end - i ] = temp ; } return ; } static void shuffleArrayUtil ( int [ ] arr , int start , int end ) { int l = end - start + 1 ; if ( l == 2 ) return ; int mid = start + l / 2 ; if ( l % 4 > 0 ) { mid -= 1 ; } int mid1 = start + ( mid - start ) / 2 ; int mid2 = mid + ( end + 1 - mid ) / 2 ; reverse ( arr , mid1 , mid2 - 1 ) ; reverse ( arr , mid1 , mid - 1 ) ; reverse ( arr , mid , mid2 - 1 ) ; shuffleArrayUtil ( arr , start , mid - 1 ) ; shuffleArrayUtil ( arr , mid , end ) ; } static void shuffleArray ( int [ ] arr , int N , int start , int end ) { shuffleArrayUtil ( arr , start , end ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 2 , 4 , 6 } ; int N = arr . Length ; shuffleArray ( arr , N , 0 , N - 1 ) ; } }
Reduce sum of same | C # program for the above approach ; Function to check if elements of B [ ] can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A [ ] in ascending order ; Sort B [ ] in descending order ; Traverse the arrays A [ ] and B [ ] ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void rearrange ( int [ ] A , int [ ] B , int N , int X ) { bool flag = true ; Array . Sort ( A ) ; Array . Sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ N - 1 - i ] > X ) { flag = false ; break ; } } if ( flag == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 } ; int [ ] B = { 1 , 1 , 2 } ; int X = 4 ; int N = A . Length ; rearrange ( A , B , N , X ) ; } }
Minimum increments or decrements by D required to make all array elements equal | C # program for the above approach ; Function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Sort the array ; Traverse the array ; If difference between two consecutive elements are not divisible by D ; Store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Stores middle element of the array ; Traverse the array ; Update count ; Driver code ; Given N & D ; Given array arr [ ] ; Function Call
using System ; class GFG { static void numOperation ( int [ ] arr , int N , int D ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( ( arr [ i + 1 ] - arr [ i ] ) % D != 0 ) { Console . WriteLine ( " - 1" ) ; return ; } } int count = 0 ; int mid = arr [ N / 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { count += Math . Abs ( mid - arr [ i ] ) / D ; } Console . WriteLine ( count ) ; } static public void Main ( ) { int N = 4 , D = 2 ; int [ ] arr = new int [ ] { 2 , 4 , 6 , 8 } ; numOperation ( arr , N , D ) ; } }
Maximize sum of second minimums of each K length partitions of the array | C # program for the above approach ; Function to find the maximum sum of second smallest of each partition of size K ; Sort the array [ ] A in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Driver Code ; Given size of partitions ; Given array [ ] A ; Size of the given array ; Function Call
using System ; class GFG { static void findSum ( int [ ] A , int N , int K ) { Array . Sort ( A ) ; int sum = 0 ; for ( int i = N / K ; i < N ; i += K - 1 ) { sum += A [ i ] ; } Console . Write ( sum ) ; } public static void Main ( String [ ] args ) { int K = 4 ; int [ ] A = { 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 } ; int N = A . Length ; findSum ( A , N , K ) ; } }
Minimize remaining array element by removing pairs and replacing them with their average | C # program to implement the above approach ; Function to find the smallest element left in the array by removing pairs and inserting their average ; Stores array elements such that the largest element present at top of PQ ; Traverse the array ; Insert arr [ i ] into PQ ; Iterate over elements of PQ while count of elements in PQ greater than 1 ; Stores largest element of PQ ; Pop the largest element of PQ ; Stores largest element of PQ ; Pop the largest element of PQ ; Insert the ceil value of average of top1 and top2 ; Driver code
using System ; using System . Collections . Generic ; class GfG { static int findSmallestNumLeft ( int [ ] arr , int N ) { List < int > PQ = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . Add ( arr [ i ] ) ; } PQ . Sort ( ) ; PQ . Reverse ( ) ; while ( PQ . Count > 1 ) { int top1 = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; int top2 = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; PQ . Add ( ( top1 + top2 + 1 ) / 2 ) ; PQ . Sort ( ) ; PQ . Reverse ( ) ; } return PQ [ 0 ] ; } public static void Main ( ) { int [ ] arr = { 30 , 16 , 40 } ; int N = arr . Length ; Console . Write ( findSmallestNumLeft ( arr , N ) ) ; } }