Minimize remaining array element by removing pairs and replacing them with their average | 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
def findSmallestNumLeft ( arr , N ) : NEW_LINE INDENT PQ = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT PQ . append ( arr [ i ] ) NEW_LINE DEDENT PQ = sorted ( PQ ) NEW_LINE while ( len ( PQ ) > 1 ) : NEW_LINE INDENT top1 = PQ [ - 1 ] NEW_LINE del PQ [ - 1 ] NEW_LINE top2 = PQ [ - 1 ] NEW_LINE del PQ [ - 1 ] NEW_LINE PQ . append ( ( top1 + top2 + 1 ) // 2 ) NEW_LINE PQ = sorted ( PQ ) NEW_LINE DEDENT return PQ [ - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 30 , 16 , 40 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findSmallestNumLeft ( arr , N ) ) NEW_LINE DEDENT