Minimum increments or decrements required to convert a sorted array into a power sequence

Minimum increments or decrements required to convert a sorted array into a power sequence
Given a sorted array arr[] consisting of N positive integers, the task is to minimize the total number of increments or decrements of each array element required to convert the given array into a power sequence of any arbitrary integer X.

A sequence is called a power sequence of any integer X, if and only if for every ith element (0 ≤ i < N), arr[i] = Xi, where N is length of the given array. Examples: Input: arr[] = {1, 3, 4}Output: 1Explanation: Decreasing arr[1] by 1 modifies array to {1, 2, 4}, which is a power sequence of 2. Therefore, the total number of increments or decrements required is 1. Input: arr[] = {1, 5, 7}Output: 6Explanation:Operation 1: Decreasing arr[1] by 1 modifies array to {1, 4, 7}Operation 2: Decreasing arr[1] by 1 modifies array to {1, 3, 7}Operation 3: Increasing arr[2] by 1 modifies array to {1, 3, 8}Operation 4: Increasing arr[2] by 1 modifies array to {1, 3, 9}, which is the power sequence of 3. Therefore, the total number of increments or decrements required is 4. Approach: The given problem can be solved based on the following observations: As the given array needs to be converted into a power sequence for any arbitrary integer X, then the mathematical relations can be written as: where, 0   =>

Therefore, the idea is to iterate for all possible values of X starting from 1, and check if the following equation satisfies or not: If found to be true, then find all possible values of and return the minimum among all the values obtained.
Follow the steps below to solve the given problem:
Initialize a variable, say ans as (the sum of array elements – N), that stores the minimum number of increments or decrements required to make the array a power sequence.
Iterate a loop, starting from 1, using the variable X and perform the following steps:
Initialize two variables, say currCost as 0 and currPower as 1, that stores the sum of the expression and power of the integer X.
Iterate over the range [0, N – 1] and update the value of currCost as currCost + abs(arr[i] – currPower) and the value of currPower as X * currPower.
If the expression is not satisfied, then break out of the loop. Otherwise, update the value of ans to the minimum of ans and currCost.

After completing the above steps, print the value of ans as the required minimum number of operations.
Below is the implementation of the above approach:

C++

  
#include
using namespace std;
  

int minOperations(int a[], int n)
{
    
    int ans = accumulate(a, a + n, 0) – n;
  
    
    
    for (int x = 1;; x++) {
  
        int curPow = 1, curCost = 0;
  
        
        for (int i = 0; i < n; i++) {             curCost += abs(a[i] - curPow);             curPow *= x;         }                     if (curPow / x > ans + a[n – 1])
            break;
  
        
        
        ans = min(ans, curCost);
    }
  
    
    
    return ans;
}
  

int main()
{
    int arr[] = { 1, 5, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout