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

Given a sequence of N elements, only three operations can be performed on any element at most one time. The operations are:

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

Perform any one of the operations on all elements in the array. The task is to find the minimum number of operations(addition and subtraction) that can be performed on the sequence, in order to convert it into a Geometric Progression. If it is not possible to generate a GP by performing the above operations, print -1.

**Examples**:

Input: a[] = {1, 1, 4, 7, 15, 33}

Output: The minimum number of operations are 4.

Steps:

- Keep a
_{1}unchanged- Add one to a
_{2}.- Keep a
_{3}unchanged- Subtract one from a
_{4}.- Subtract one from a
_{5}.- Add one to a
_{6}.The resultant sequence is {1, 2, 4, 8, 16, 32}

Input: a[] = {20, 15, 20, 15}

Output: -1

**Approach** The key observation to be made here is that any Geometric Progression is uniquely determined by only its first two elements (Since the ratio between each of the next pairs has to be the same as the ratio between this pair, consisting of the first two elements). Since only **3*3** permutations are possible. The possible combination of operations are (+1, +1), (+1, 0), (+1, -1), (-1, +1), (-1, 0), (-1, -1), (0, +1), (0, 0) and (0, -1). Using brute force all these **9** permutations and checking if they form a GP in linear time will give us the answer. The minimum of the operations which result in combinations which are in GP will be the answer.

Below is the implementation of the above approach:

## C++

`// C++ program to find minimum number ` `// of operations to convert a given ` `// sequence to an Geometric Progression ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the GP series ` `void` `construct(` `int` `n, pair<` `double` `, ` `double` `> ans_pair) ` `{ ` ` ` `// Check for possibility ` ` ` `if` `(ans_pair.first == -1) { ` ` ` `cout << ` `"Not possible"` `; ` ` ` `return` `; ` ` ` `} ` ` ` `double` `a1 = ans_pair.first; ` ` ` `double` `a2 = ans_pair.second; ` ` ` `double` `r = a2 / a1; ` ` ` ` ` `cout << ` `"The resultant sequence is:\n"` `; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `double` `ai = a1 * ` `pow` `(r, i - 1); ` ` ` `cout << ai << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Function for getting the Arithmetic Progression ` `void` `findMinimumOperations(` `double` `* a, ` `int` `n) ` `{ ` ` ` `int` `ans = INT_MAX; ` ` ` `// The array c describes all the given set of ` ` ` `// possible operations. ` ` ` `int` `c[] = { -1, 0, 1 }; ` ` ` `// Size of c ` ` ` `int` `possiblities = 3; ` ` ` ` ` `// candidate answer ` ` ` `int` `pos1 = -1, pos2 = -1; ` ` ` ` ` `// loop through all the permutations of the first two ` ` ` `// elements. ` ` ` `for` `(` `int` `i = 0; i < possiblities; i++) { ` ` ` `for` `(` `int` `j = 0; j < possiblities; j++) { ` ` ` ` ` `// a1 and a2 are the candidate first two elements ` ` ` `// of the possible GP. ` ` ` `double` `a1 = a[1] + c[i]; ` ` ` `double` `a2 = a[2] + c[j]; ` ` ` ` ` `// temp stores the current answer, including the ` ` ` `// modification of the first two elements. ` ` ` `int` `temp = ` `abs` `(a1 - a[1]) + ` `abs` `(a2 - a[2]); ` ` ` ` ` `if` `(a1 == 0 || a2 == 0) ` ` ` `continue` `; ` ` ` ` ` `// common ratio of the possible GP ` ` ` `double` `r = a2 / a1; ` ` ` ` ` `// To check if the chosen set is valid, and id yes ` ` ` `// find the number of operations it takes. ` ` ` `for` `(` `int` `pos = 3; pos <= n; pos++) { ` ` ` ` ` `// ai is value of a[i] according to the assumed ` ` ` `// first two elements a1, a2 ` ` ` `// ith element of an GP = a1*((a2-a1)^(i-1)) ` ` ` `double` `ai = a1 * ` `pow` `(r, pos - 1); ` ` ` ` ` `// Check for the "proposed" element to be only ` ` ` `// differing by one ` ` ` `if` `(a[pos] == ai) { ` ` ` `continue` `; ` ` ` `} ` ` ` `else` `if` `(a[pos] + 1 == ai || a[pos] - 1 == ai) { ` ` ` `temp++; ` ` ` `} ` ` ` `else` `{ ` ` ` `temp = INT_MAX; ` `// set the temporary ans ` ` ` `break` `; ` `// to infinity and break ` ` ` `} ` ` ` `} ` ` ` ` ` `// update answer ` ` ` `if` `(temp < ans) { ` ` ` `ans = temp; ` ` ` `pos1 = a1; ` ` ` `pos2 = a2; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `if` `(ans == -1) { ` ` ` `cout << ` `"-1"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `cout << ` `"Minimum Number of Operations are "` `<< ans << ` `"\n"` `; ` ` ` `pair<` `double` `, ` `double` `> ans_pair = { pos1, pos2 }; ` ` ` ` ` `// Calling function to print the sequence ` ` ` `construct(n, ans_pair); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// array is 1-indexed, with a[0] = 0 ` ` ` `// for the sake of simplicity ` ` ` `double` `a[] = { 0, 7, 20, 49, 125 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `// Function to print the minimum operations ` ` ` `// and the sequence of elements ` ` ` `findMinimumOperations(a, n - 1); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find minimum number ` `# of operations to convert a given ` `# sequence to an Geometric Progression ` `from` `sys ` `import` `maxsize as INT_MAX ` ` ` `# Function to print the GP series ` `def` `construct(n: ` `int` `, ans_pair: ` `tuple` `): ` ` ` ` ` `# Check for possibility ` ` ` `if` `ans_pair[` `0` `] ` `=` `=` `-` `1` `: ` ` ` `print` `(` `"Not possible"` `) ` ` ` `return` ` ` ` ` `a1 ` `=` `ans_pair[` `0` `] ` ` ` `a2 ` `=` `ans_pair[` `1` `] ` ` ` `r ` `=` `a2 ` `/` `a1 ` ` ` ` ` `print` `(` `"The resultant sequence is"` `) ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `ai ` `=` `a1 ` `*` `pow` `(r, i ` `-` `1` `) ` ` ` `print` `(` `int` `(ai), end` `=` `" "` `) ` ` ` `# Function for getting the Arithmetic Progression ` `def` `findMinimumOperations(a: ` `list` `, n: ` `int` `): ` ` ` `ans ` `=` `INT_MAX ` ` ` ` ` `# The array c describes all the given set of ` ` ` `# possible operations. ` ` ` `c ` `=` `[` `-` `1` `, ` `0` `, ` `1` `] ` ` ` ` ` `# Size of c ` ` ` `possibilities ` `=` `3` ` ` ` ` `# candidate answer ` ` ` `pos1 ` `=` `-` `1` ` ` `pos2 ` `=` `-` `1` ` ` ` ` `# loop through all the permutations of the first two ` ` ` `# elements. ` ` ` `for` `i ` `in` `range` `(possibilities): ` ` ` `for` `j ` `in` `range` `(possibilities): ` ` ` ` ` `# a1 and a2 are the candidate first two elements ` ` ` `# of the possible GP. ` ` ` `a1 ` `=` `a[` `1` `] ` `+` `c[i] ` ` ` `a2 ` `=` `a[` `2` `] ` `+` `c[j] ` ` ` ` ` `# temp stores the current answer, including the ` ` ` `# modification of the first two elements. ` ` ` `temp ` `=` `abs` `(a1 ` `-` `a[` `1` `]) ` `+` `abs` `(a2 ` `-` `a[` `2` `]) ` ` ` ` ` `if` `a1 ` `=` `=` `0` `or` `a2 ` `=` `=` `0` `: ` ` ` `continue` ` ` ` ` `# common ratio of the possible GP ` ` ` `r ` `=` `a2 ` `/` `a1 ` ` ` ` ` `# To check if the chosen set is valid, and id yes ` ` ` `# find the number of operations it takes. ` ` ` `for` `pos ` `in` `range` `(` `3` `, n ` `+` `1` `): ` ` ` ` ` `# ai is value of a[i] according to the assumed ` ` ` `# first two elements a1, a2 ` ` ` `# ith element of an GP = a1*((a2-a1)^(i-1)) ` ` ` `ai ` `=` `a1 ` `*` `pow` `(r, pos ` `-` `1` `) ` ` ` ` ` `# Check for the "proposed" element to be only ` ` ` `# differing by one ` ` ` `if` `a[pos] ` `=` `=` `ai: ` ` ` `continue` ` ` `elif` `a[pos] ` `+` `1` `=` `=` `ai ` `or` `a[pos] ` `-` `1` `=` `=` `ai: ` ` ` `temp ` `+` `=` `1` ` ` `else` `: ` ` ` `temp ` `=` `INT_MAX ` `# set the temporary ans ` ` ` `break` `# to infinity and break ` ` ` ` ` `# update answer ` ` ` `if` `temp < ans: ` ` ` `ans ` `=` `temp ` ` ` `pos1 ` `=` `a1 ` ` ` `pos2 ` `=` `a2 ` ` ` `if` `ans ` `=` `=` `-` `1` `: ` ` ` `print` `(` `"-1"` `) ` ` ` `return` ` ` ` ` `print` `(` `"Minimum number of Operations are"` `, ans) ` ` ` `ans_pair ` `=` `(pos1, pos2) ` ` ` ` ` `# Calling function to print the sequence ` ` ` `construct(n, ans_pair) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# array is 1-indexed, with a[0] = 0 ` ` ` `# for the sake of simplicity ` ` ` `a ` `=` `[` `0` `, ` `7` `, ` `20` `, ` `49` `, ` `125` `] ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `# Function to print the minimum operations ` ` ` `# and the sequence of elements ` ` ` `findMinimumOperations(a, n ` `-` `1` `) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

Minimum Number of Operations are 2 The resultant sequence is: 8 20 50 125

**Time Complexity** : O(9*N)

## Recommended Posts:

- Number of GP (Geometric Progression) subsequences of size 3
- Removing a number from array to make it Geometric Progression
- Minimum prime number operations to convert A to B
- Geometric Progression
- Program to print GP (Geometric Progression)
- Program for N-th term of Geometric Progression series
- Minimum cost to convert str1 to str2 with the given operations
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Sum of Arithmetic Geometric Sequence
- Minimum number of operations required to reduce N to 1
- Minimum number of operations on an array to make all elements 0
- Minimum number of operations required to delete all elements of the array
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of given operation required to convert n to m
- Minimum number of operations to move all uppercase characters before all lower case characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.