给定一个由N 个正整数组成的数组arr[] ,任务是找到将给定的整数数组变成2的幂序列所需的最小步骤 通过以下操作:
- 重新排序给定的数组。这不算是一步。
- 对于每一步,从数组中选择任何索引i并将arr[i]更改为arr[i] − 1或arr[i] + 1 。
A sequence is called power sequence of 2, if for every ith index (0 ≤i ≤ N − 1),
arr[i] = 2i , where N is length of the given array.
例子:
Input: arr[] = { 1, 8, 2, 10, 6 }
Output: 8
Explanation:
Reorder the array arr[] to { 1, 2, 6, 8, 10 }
Step 1: Decrement arr[2] to 5
Step 2: Decrement arr[2] to 4
Step 3 – 8: Increment arr[4] by 1. Final value of arr[4] becomes 16.
Therefore, arr[] = {1, 2, 4, 8, 16}
Hence, the minimum number of steps required to obtain the power sequence of 2 is 8.
Input: arr[] = { 1, 3, 4 }
Output: 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