Related Articles

# Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2

• Last Updated : 20 May, 2021

Given an array arr[] consisting of N integers, the following three operations can be performed on any element one at a time:

• Add one to the element.
• Subtract one from the element.
• Leave the element unchanged.

The task is to find the minimum cost required to convert it into a Geometric Progression and also find the common ratio
Note: Each addition and subtraction operation costs 1 unit.

Examples:

Input: N = 6, arr[] = {1, 11, 4, 27, 15, 33}
Output: 28 2
Explanation:
For r = 1, arr[] = {1, 4, 11, 15, 27, 33}
expected[] = {1, 1, 1, 1, 1, 1}
cost[] = {0, 3, 10, 14, 26, 32}
Total cost: ∑ cost = 85

For r = 2, arr[] = {1, 4, 11, 15, 27, 33}
expected[] = {1, 2, 4, 8, 16, 32}
cost[] = {0, 2, 7, 7, 11, 1}
Total cost: ∑ cost = 28
For r = 3, arr[] = {1, 4, 11, 15, 27, 33}
expected[] = {1, 3, 9, 27, 81, 243}
cost[] = {0, 1, 2, 12, 54, 210}
Total cost: ∑ cost = 279

Minimum cost = 28
Common ratio = 2

Input: N = 7, arr[] = {1, 2, 4, 8, 9, 6, 7}
Output: 30 1

Approach: The idea is to iterate over range of possible common ratios and check for the minimum operations required. Follow the steps below to solve the problem:

• Sort the array.
• Find the range of possible common ratios using the formula Ceil( arr[maximum_element] / (N – 1)).
• Calculate the number of operations required for all possible common ratios.
• Find the minimum operations required for any of the common ratios.

Below is the implementation of the above approach:

## C++

 `// C++ program for above approach` `#include ``using` `namespace` `std;` `// Function to find minimum cost``void` `minCost(``int` `arr[], ``int` `n)``{``    ``if` `(n == 1) {``        ``cout << 0 << endl;``        ``return``;``    ``}` `    ``// Sort the array``    ``sort(arr, arr + n);` `    ``// Maximum possible common ratios``    ``float` `raised = 1 / ``float``(n - 1);``    ``float` `temp = ``pow``(arr[n - 1], raised);``    ``int` `r = round(temp) + 1;` `    ``int` `i, j, min_cost = INT_MAX;``    ``int` `common_ratio = 1;` `    ``// Iterate over all possible common ratios``    ``for` `(j = 1; j <= r; j++) {``        ``int` `curr_cost = 0, prod = 1;` `        ``// Calculate operations required``        ``// for the current common ratio``        ``for` `(i = 0; i < n; i++) {` `            ``curr_cost += ``abs``(arr[i] - prod);``            ``prod *= j;``            ``if` `(curr_cost >= min_cost)``                ``break``;``        ``}` `        ``// Calculate minimum cost``        ``if` `(i == n) {``            ``min_cost = min(min_cost,``                           ``curr_cost);``            ``common_ratio = j;``        ``}``    ``}` `    ``cout << min_cost << ``' '``;``    ``cout << common_ratio << ``' '``;``}` `// Driver Code``int` `main()``{``    ``// Given N``    ``int` `N = 6;` `    ``// Given arr[]``    ``int` `arr[] = { 1, 11, 4, 27, 15, 33 };` `    ``// Function Calling``    ``minCost(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for``// the above approach``import` `java.util.*;``class` `GFG{` `// Function to find minimum cost``static` `void` `minCost(``int` `arr[],``                    ``int` `n)``{``  ``if` `(n == ``1``)``  ``{``    ``System.out.print(``0` `+ ``"\n"``);``    ``return``;``  ``}` `  ``// Sort the array``  ``Arrays.sort(arr);` `  ``// Maximum possible common ratios``  ``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 = Integer.MAX_VALUE;``  ``int` `common_ratio = ``1``;` `  ``// Iterate over all possible``  ``// common ratios``  ``for` `(j = ``1``; j <= r; j++)``  ``{``    ``int` `curr_cost = ``0``, prod = ``1``;` `    ``// Calculate operations required``    ``// for the current common ratio``    ``for` `(i = ``0``; i < n; i++)``    ``{``      ``curr_cost += Math.abs(arr[i] -``                            ``prod);``      ``prod *= j;``      ``if` `(curr_cost >= min_cost)``        ``break``;``    ``}` `    ``// Calculate minimum cost``    ``if` `(i == n)``    ``{``      ``min_cost = Math.min(min_cost,``                          ``curr_cost);``      ``common_ratio = j;``    ``}``  ``}` `  ``System.out.print(min_cost + ``" "``);``  ``System.out.print(common_ratio + ``" "``);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ``// Given N``  ``int` `N = ``6``;` `  ``// Given arr[]``  ``int` `arr[] = {``1``, ``11``, ``4``,``               ``27``, ``15``, ``33``};` `  ``// Function Calling``  ``minCost(arr, N);``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python3 program for above approach``import` `sys` `# Function to find minimum cost``def` `minCost(arr, n):``    ` `    ``if` `(n ``=``=` `1``):``        ``print``(``0``)``        ``return` `    ``# Sort the array``    ``arr ``=` `sorted``(arr)` `    ``# Maximum possible common ratios``    ``raised ``=` `1` `/` `(n ``-` `1``)``    ``temp ``=` `pow``(arr[n ``-` `1``], raised)``    ``r ``=` `round``(temp) ``+` `1` `    ``min_cost ``=` `sys.maxsize``    ``common_ratio ``=` `1` `    ``# Iterate over all possible``    ``# common ratios``    ``for` `j ``in` `range``(``1``, r ``+` `1``):``        ``curr_cost ``=` `0``        ``prod ``=` `1` `        ``# Calculate operations required``        ``# for the current common ratio``        ``i ``=` `0``        ``while` `i < n:``            ``curr_cost ``+``=` `abs``(arr[i] ``-` `prod)``            ``prod ``*``=` `j``            ` `            ``if` `(curr_cost >``=` `min_cost):``                ``break``            ` `            ``i ``+``=` `1` `        ``# Calculate minimum cost``        ``if` `(i ``=``=` `n):``            ``min_cost ``=` `min``(min_cost,``                          ``curr_cost)``            ``common_ratio ``=` `j` `    ``print``(min_cost, common_ratio)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given N``    ``N ``=` `6` `    ``# Given arr[]``    ``arr ``=` `[ ``1``, ``11``, ``4``, ``27``, ``15``, ``33` `]` `    ``# Function calling``    ``minCost(arr, N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for``// the above approach``using` `System;` `class` `GFG{` `// Function to find minimum cost``static` `void` `minCost(``int` `[]arr,``                    ``int` `n)``{``  ``if` `(n == 1)``  ``{``    ``Console.Write(0 + ``"\n"``);``    ``return``;``  ``}` `  ``// Sort the array``  ``Array.Sort(arr);` `  ``// Maximum possible common ratios``  ``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;` `  ``// Iterate over all possible``  ``// common ratios``  ``for` `(j = 1; j <= r; j++)``  ``{``    ``int` `curr_cost = 0, prod = 1;` `    ``// Calculate operations required``    ``// for the current common ratio``    ``for` `(i = 0; i < n; i++)``    ``{``      ``curr_cost += Math.Abs(arr[i] -``                            ``prod);``      ``prod *= j;``      ` `      ``if` `(curr_cost >= min_cost)``        ``break``;``    ``}` `    ``// Calculate minimum cost``    ``if` `(i == n)``    ``{``      ``min_cost = Math.Min(min_cost,``                          ``curr_cost);``      ``common_ratio = j;``    ``}``  ``}` `  ``Console.Write(min_cost + ``" "``);``  ``Console.Write(common_ratio + ``" "``);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ` `  ``// Given N``  ``int` `N = 6;` `  ``// Given []arr``  ``int` `[]arr = { 1, 11, 4,``                ``27, 15, 33 };` `  ``// Function Calling``  ``minCost(arr, N);``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``
Output:
`28 2`

Time Complexity: O(N * K), where K is the maximum possible common ratio.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up