Minimum operations required to make every element greater than or equal to K | C # implementation of above approach ; Function to calculate gcd of two numbers ; C # 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
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int FindMinOperation ( int [ ] a , int n , int k ) { List < int > Q = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) Q . Add ( a [ i ] ) ; Q . Sort ( ) ; int ans = 0 ; while ( true ) { if ( Q [ 0 ] >= k ) break ; if ( Q . Count < 2 ) return - 1 ; int x = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; int y = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; int z = ( x * y ) / gcd ( x , y ) ; Q . Add ( z ) ; Q . Sort ( ) ; ans ++ ; } return ans ; } static void Main ( ) { int [ ] a = { 3 , 5 , 7 , 6 , 8 } ; int k = 8 ; int n = a . Length ; Console . WriteLine ( FindMinOperation ( a , n , k ) ) ; } }