📌  相关文章
📜  将给定数组重新排列为2的幂序列所需的最少步骤

📅  最后修改于: 2021-05-04 16:53:42             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] 任务是找到使给定整数数组成为2的幂的序列所需的最小步骤 通过以下操作:

  • 重新排序给定的数组。它不算是一个步骤。
  • 对于每个步骤,从数组中选择任何索引i并将arr [i]更改为arr [i] − 1arr [i] +1

例子:

方法:为了解决给定问题,这个想法是按升序对数组进行排序,并为每个i排序后的数组的索引,计算ARR之间的绝对差[I]  2。的绝对之和为我们提供了所需要的答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
  
#include 
using namespace std;
  
// Function to calculate the minimum
// steps required to convert given
// array into a power sequence of 2
int minsteps(int arr[], int n)
{
  
    // Sort the array in
    // ascending order
    sort(arr, arr + n);
  
    int ans = 0;
  
    // Calculate the absolute difference
    // between arr[i] and 2^i for each index
    for (int i = 0; i < n; i++) {
        ans += abs(arr[i] - pow(2, i));
    }
  
    // Return the answer
    return ans;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 8, 2, 10, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minsteps(arr, n) << endl;
    return 0;
}


Java
// Java Program to implement
// the above approach
  
import java.util.*;
import java.lang.Math;
  
class GFG {
  
    // Function to calculate the minimum
    // steps required to convert given
    // array into a power sequence of 2
    static int minsteps(int arr[], int n)
    {
        // Sort the array in ascending order
        Arrays.sort(arr);
        int ans = 0;
  
        // Calculate the absolute difference
        // between arr[i] and 2^i for each index
        for (int i = 0; i < n; i++) {
            ans += Math.abs(arr[i] - Math.pow(2, i));
        }
        return ans;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 8, 2, 10, 6 };
        int n = arr.length;
        System.out.println(minsteps(arr, n));
    }
}


Python3
# Python 3 program for the above approach
  
# Function to calculate the minimum
# steps required to convert given
# array into a power sequence of 2
def minsteps(arr, n):
  
    # Sort the array in ascending order
    arr.sort()
    ans = 0
    for i in range(n):
        ans += abs(arr[i]-pow(2, i))
    return ans
  
  
# Driver Code
arr = [1, 8, 2, 10, 6]
n = len(arr)
print(minsteps(arr, n))


C#
// C# Program to the above approach
  
using System;
  
class GFG {
  
    // Function to calculate the minimum
    // steps required to convert given
    // array into a power sequence of 2
    static int minsteps(int[] arr, int n)
    {
  
        // Sort the array in ascending order
        Array.Sort(arr);
        int ans = 0;
  
        // Calculate the absolute difference
        // between arr[i] and 2^i for each index
        for (int i = 0; i < n; i++) {
            ans += Math.Abs(arr[i]
                            - (int)(Math.Pow(2, i)));
        }
        return ans;
    }
  
    // Driver Code
    public static void Main()
    {
  
        int[] arr = { 1, 8, 2, 10, 6 };
        int n = arr.Length;
        Console.WriteLine(minsteps(arr, n));
    }
}


输出:
8

时间复杂度: O(NlogN)
辅助空间: O(1)