给定一个由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我。的绝对差之和为我们提供了所需要的答案。
下面是上述方法的实现:
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)