Minimum operations required to make every element greater than or equal to K | Function to calculate gcd of two numbers ; function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE return b NEW_LINE return gcd ( b % a , a ) NEW_LINE DEDENT def FindMinOperation ( a , n , k ) : NEW_LINE INDENT Q = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE Q . append ( a [ i ] ) NEW_LINE Q . sort ( ) NEW_LINE ans = 0 NEW_LINE while ( True ) : NEW_LINE if ( Q [ 0 ] >= k ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( len ( Q ) < 2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = Q [ 0 ] NEW_LINE Q . pop ( 0 ) NEW_LINE y = Q [ 0 ] NEW_LINE Q . pop ( 0 ) NEW_LINE z = ( x * y ) // gcd ( x , y ) NEW_LINE Q . append ( z ) NEW_LINE Q . sort ( ) NEW_LINE ans += 1 NEW_LINE return ans NEW_LINE DEDENT a = [ 3 , 5 , 7 , 6 , 8 ] NEW_LINE k = 8 NEW_LINE n = len ( a ) NEW_LINE print ( FindMinOperation ( a , n , k ) ) NEW_LINE