📜  找到需要最小递增或递减数组元素的K以获取K的幂递增序列

📅  最后修改于: 2021-05-14 08:07:14             🧑  作者: Mango

给定一个包含N个整数的数组,任务是找到整数K ,该整数需要最少的移动次数才能将给定数组转换为K的幂序列,即{K 0 ,K 1 ,K 2 ,……。 ,K N – 1 } 。在每一步中,将数组元素增加或减少一个。

例子:

方法:想法是检查从1开始的所有数字,直到一个数字的幂超过最大值,即10 10 (假定)。请按照以下步骤解决问题:

  1. 检查从1x的所有数字,直到x N – 1 的值+转换为1的幂所需的move为止
  2. 计算在该元素的力量下形成数组所需的移动。
  3. 如果所需的移动量小于先前的值,则更新K最小移动量的值。
  4. 打印K的值。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
#define ll long long
 
// Function to find the value of K such
// that it takes minimum number of moves
// to convert the array in its power
ll findMinCostInteger(vector& a)
{
 
    // Size of the array
    int n = a.size();
 
    // Finding tha sum of the array
    ll sum = accumulate(a.begin(),
                        a.end(), 0);
 
    ll K = 0, minmove = LLONG_MAX;
 
    // Find K for which the count
    // of moves will become minimum
    for (int i = 1;; ++i) {
 
        ll power = 1, count = 0;
 
        for (ll j = 0; j < n; ++j) {
 
            count += abs(a[j] - power);
            if (j != n - 1)
                power = power * (ll)i;
 
            // If exceeds maximum value
            if (power >= 1e10)
                break;
        }
 
        if (power >= (1e10)
            || power > (ll)(sum - n)
                           + a[n - 1])
            break;
 
        // Update minimum moves
        if (minmove > count) {
            minmove = count;
            K = i;
        }
    }
 
    // Print K corresponds to minimum
    // number of moves
    cout << K;
}
 
// Driver Code
int main()
{
 
    // Given vector
    vector a = { 1, 9, 27 };
 
    // Function Call
    findMinCostInteger(a);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
   
// Function to find the value of K such
// that it takes minimum number of moves
// to convert the array in its power
static void findMinCostInteger(int a[], int n)
{
     
    // Finding tha sum of the array
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += a[i];
 
    int K = 0, minmove = Integer.MAX_VALUE;
 
    // Find K for which the count
    // of moves will become minimum
    for(int i = 1;; ++i)
    {
        int power = 1, count = 0;
         
        for(int j = 0; j < n; ++j)
        {
            count += Math.abs(a[j] - power);
             
            if (j != n - 1)
                power = power * i;
             
            // If exceeds maximum value
            if (power >= 1e10)
                break;
        }
         
        if (power >= (1e10) ||
            power > (sum - n) + a[n - 1])
            break;
         
        // Update minimum moves
        if (minmove > count)
        {
            minmove = count;
            K = i;
        }
    }
     
    // Print K corresponds to minimum
    // number of moves
    System.out.println(K);
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given vector
    int []a = { 1, 9, 27 };
 
    // Function Call
    findMinCostInteger(a, 3);
}
}
 
// This code is contributed by bgangwar59


Python3
# Python3 program for the above approach
import sys
 
# Function to find the value of K such
# that it takes minimum number of moves
# to convert the array in its power
def findMinCostInteger(a):
     
    # Size of the array
    n = len(a)
 
    # Finding tha sm of the array
    sm = sum(a)
 
    K = 0
    minmove = sys.maxsize
 
    # Find K for which the count
    # of moves will become minimum
    i = 1
    while(1):
        power = 1
        count = 0
        for j in range(n):
            count += abs(a[j] - power)
             
            if (j != n - 1):
                power = power * i
 
            # If exceeds maximum value
            if (power >= 1e10):
                break
 
        if (power >= (1e10) or
            power > (sm - n) + a[n - 1]):
            break
 
        # Update minimum moves
        if (minmove > count):
            minmove = count
            K = i
             
        i += 1
 
    # Print K corresponds to minimum
    # number of moves
    print(K)
 
# Driver Code
if __name__ == '__main__':
     
    # Given vector
    a =  [ 1, 9, 27 ]
 
    # Function Call
    findMinCostInteger(a)
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the
// above approach
using System;
class GFG{
   
// Function to find the value
// of K such that it takes
// minimum number of moves
// to convert the array in
// its power
static void findMinCostint(int []a,
                           int n)
{   
  // Finding tha sum of the array
  int sum = 0;
   
  for(int i = 0; i < n; i++)
    sum += a[i];
 
  int K = 0, minmove = int.MaxValue;
 
  // Find K for which the count
  // of moves will become minimum
  for(int i = 1;; ++i)
  {
    int power = 1, count = 0;
 
    for(int j = 0; j < n; ++j)
    {
      count += Math.Abs(a[j] - power);
 
      if (j != n - 1)
        power = power * i;
 
      // If exceeds maximum value
      if (power >= 1e10)
        break;
    }
 
    if (power >= (1e10) ||
        power > (sum - n) +
                 a[n - 1])
      break;
 
    // Update minimum moves
    if (minmove > count)
    {
      minmove = count;
      K = i;
    }
  }
 
  // Print K corresponds to
  // minimum number of moves
  Console.WriteLine(K);
}
 
// Driver Code
public static void Main(String []args)
{   
  // Given vector
  int []a = {1, 9, 27};
 
  // Function Call
  findMinCostint(a, 3);
}
}
 
// This code is contributed by gauravrajput1


输出
5

时间复杂度: O(N * max(x))
辅助空间: O(1)