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 ) ) ; } }