Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | C # program to implement the above approach ; Utility pair ; Function to print the smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of [ ] arr in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array [ ] arr ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print the element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print all the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print current pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void smallestNumberLeftInPQ ( int [ ] arr , int N ) { List < int > pq = new List < int > ( ) ; List < pair > pairsArr = new List < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Add ( arr [ i ] ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count > 1 ) { int X = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int Y = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; pq . Add ( ( X + Y + 1 ) / 2 ) ; pq . Sort ( ) ; pq . Reverse ( ) ; pairsArr . Add ( new pair ( X , Y ) ) ; } Console . Write ( " { " + pq [ 0 ] + " } , ▁ " ) ; int sz = pairsArr . Count ; for ( int i = 0 ; i < sz ; i ++ ) { if ( i == 0 ) { Console . Write ( " { ▁ " ) ; } Console . Write ( " ( " + pairsArr [ i ] . first + " , ▁ " + pairsArr [ i ] . second + " ) " ) ; if ( i != sz - 1 ) { Console . Write ( " , ▁ " ) ; } if ( i == sz - 1 ) { Console . Write ( " ▁ } " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int N = arr . Length ; smallestNumberLeftInPQ ( arr , N ) ; } }