Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | C # program for the above approach ; Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Driver Code ; Given N ; Given [ ] arr ; Function Calling
using System ; class GFG { static void minCost ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( 0 + " STRNEWLINE " ) ; return ; } Array . Sort ( arr ) ; float raised = 1 / ( float ) ( n - 1 ) ; float temp = ( float ) Math . Pow ( arr [ n - 1 ] , raised ) ; int r = ( int ) ( temp ) + 1 ; int i , j , min_cost = int . MaxValue ; int common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { int curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += Math . Abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = Math . Min ( min_cost , curr_cost ) ; common_ratio = j ; } } Console . Write ( min_cost + " ▁ " ) ; Console . Write ( common_ratio + " ▁ " ) ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 1 , 11 , 4 , 27 , 15 , 33 } ; minCost ( arr , N ) ; } }