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

📅  最后修改于: 2021-09-07 02:17:05             🧑  作者: Mango

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

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

例子:

方法:解决给定的问题,思路是对数组进行升序排序,对于排序后的数组的每个i索引,计算arr[i]之间的绝对差  2 i 。绝对差异的总和为我们提供了所需的答案。

下面是上述方法的实现:

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));
    }
}


Javascript


输出:
8

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live