Minimum steps required to rearrange given array to a power sequence of 2
Last Updated :
05 May, 2021
Given an array arr[] consisting of N positive integers, the task is to find the minimum steps required to make the given array of integers into a sequence of powers of 2 by the following operations:
- Reorder the given array. It doesn't count as a step.
- For each step, select any index i from the array and change arr[i] to arr[i] ? 1 or 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.
Examples:
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
Approach: To solve the given problem, the idea is to sort the array in ascending order and for every ith index of the sorted array, calculate the absolute difference between arr[i] and 2i. The sum of the absolute differences gives us the required answer.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript program to implement
// the above approach
// Function to calculate the minimum
// steps required to convert given
// array into a power sequence of 2
function minsteps(arr, n)
{
// Sort the array in
// ascending order
arr.sort((a,b)=>a-b)
var ans = 0;
// Calculate the absolute difference
// between arr[i] and 2^i for each index
for (var i = 0; i < n; i++) {
ans += Math.abs(arr[i] - Math.pow(2, i));
}
// Return the answer
return ans;
}
// Driver Code
var arr = [ 1, 8, 2, 10, 6 ];
var n = arr.length;
document.write( minsteps(arr, n));
// This code is contributed by noob2000.
</script>
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Similar Reads
Minimum steps required to reduce all array elements to 1 based on given steps Given an array arr[] of size N. The task is to find the minimum steps required to reduce all array elements to 1. In each step, perform the following given operation: Choose any starting index, say i, and jump to the (arr[i] + i)th index, reducing ith as well as (arr[i] + i)th index by 1, follow thi
8 min read
Number of subarrays required to be rearranged to sort the given array Given an array arr[] consisting of the first N natural numbers, the task is to find the minimum number of subarrays required to be rearranged such that the resultant array is sorted. Examples: Input: arr[] = {2, 1, 4, 3, 5}Output: 1Explanation:Operation 1: Choose the subarray {arr[0], arr[3]}, i.e.
6 min read
Minimize given operations required to sort the stream array Given an array stream of n integers in the form of a stream, the task is to find the minimum number of operations that are required to sort the stream (increasing order) following the below steps: In one operation you can pick up the first element and then put that element into the xth index. All th
6 min read
Minimum increments or decrements required to convert a sorted array into a power sequence Given a sorted array arr[] consisting of N positive integers, the task is to minimize the total number of increments or decrements of each array element required to convert the given array into a power sequence of any arbitrary integer X. A sequence is called a power sequence of any integer X, if an
8 min read
Minimum number of steps required to place all 1s at a single index Given a binary array A[] of size N, where all 1s can be moved to its adjacent position, the task is to print an array res[] of size N, where res[i] contains the minimum number of steps required to move all the 1s at the ith index. Examples: Input: A[] = {1, 0, 1, 0}Output: {2, 2, 2, 4}Explanation: F
9 min read
Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2] Given a sequence of integers of even length 'n', the task is to find the minimum number of operations required to convert the sequence to follow the rule a[i]=a[i+2] where 'i' is the index. The operation here is to replace any element of the sequence with any element. Examples: Input : n=4 ; Array :
11 min read