Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
Last Updated :
14 Jan, 2022
Given an array consisting of N integers, the task is to find the integer K which requires the minimum number of moves to convert the given array to a sequence of powers of K, i.e. {K0, K1, K2, ……., KN – 1}. In each move, increase or decrease an array element by one.
Examples:
Input: arr[] = {1, 2, 3}
Output: 2
Explanation: By incrementing arr[2] by 1 modifies to {1, 2, 4} which is equal to {20, 21, 22}. Therefore, K = 2.
Input: arr[] = {1, 9, 27}
Output: 5
Explanation: Modifying array to {1, 5, 25} requires minimum number of moves. Therefore, K = 5.
Approach: The idea is to check for all the numbers starting from 1 till the power of a number crosses the maximum value i.e. 1010 (assumed). Follow the steps below to solve the problem:
- Check for all the numbers from 1 to x until the value of xN – 1 < max_element of array + move needed to convert in the power of 1.
- Count the moves needed to make an array in the power of that element.
- If the move needed is minimum than the previous value then update the value of K and min moves.
- Print the value of K.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll findMinCostInteger(vector<ll>& a)
{
int n = a.size();
ll sum = accumulate(a.begin(),
a.end(), 0);
ll K = 0, minmove = LLONG_MAX;
for ( int i = 1;; ++i) {
ll power = 1, count = 0;
for (ll j = 0; j < n; ++j) {
count += abs (a[j] - power);
if (j != n - 1)
power = power * (ll)i;
if (power >= 1e10)
break ;
}
if (power >= (1e10)
|| power > (ll)(sum - n)
+ a[n - 1])
break ;
if (minmove > count) {
minmove = count;
K = i;
}
}
cout << K;
}
int main()
{
vector<ll> a = { 1, 9, 27 };
findMinCostInteger(a);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMinCostInteger( int a[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += a[i];
int K = 0 , minmove = Integer.MAX_VALUE;
for ( int i = 1 ;; ++i)
{
int power = 1 , count = 0 ;
for ( int j = 0 ; j < n; ++j)
{
count += Math.abs(a[j] - power);
if (j != n - 1 )
power = power * i;
if (power >= 1e10)
break ;
}
if (power >= (1e10) ||
power > (sum - n) + a[n - 1 ])
break ;
if (minmove > count)
{
minmove = count;
K = i;
}
}
System.out.println(K);
}
public static void main(String args[])
{
int []a = { 1 , 9 , 27 };
findMinCostInteger(a, 3 );
}
}
|
Python3
import sys
def findMinCostInteger(a):
n = len (a)
sm = sum (a)
K = 0
minmove = sys.maxsize
i = 1
while ( 1 ):
power = 1
count = 0
for j in range (n):
count + = abs (a[j] - power)
if (j ! = n - 1 ):
power = power * i
if (power > = 1e10 ):
break
if (power > = ( 1e10 ) or
power > (sm - n) + a[n - 1 ]):
break
if (minmove > count):
minmove = count
K = i
i + = 1
print (K)
if __name__ = = '__main__' :
a = [ 1 , 9 , 27 ]
findMinCostInteger(a)
|
C#
using System;
class GFG{
static void findMinCostint( int []a,
int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += a[i];
int K = 0, minmove = int .MaxValue;
for ( int i = 1;; ++i)
{
int power = 1, count = 0;
for ( int j = 0; j < n; ++j)
{
count += Math.Abs(a[j] - power);
if (j != n - 1)
power = power * i;
if (power >= 1e10)
break ;
}
if (power >= (1e10) ||
power > (sum - n) +
a[n - 1])
break ;
if (minmove > count)
{
minmove = count;
K = i;
}
}
Console.WriteLine(K);
}
public static void Main(String []args)
{
int []a = {1, 9, 27};
findMinCostint(a, 3);
}
}
|
Javascript
<script>
function findMinCostLeteger(a, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += a[i];
let K = 0, minmove = Number.MAX_VALUE;
for (let i = 1;; ++i)
{
let power = 1, count = 0;
for (let j = 0; j < n; ++j)
{
count += Math.abs(a[j] - power);
if (j != n - 1)
power = power * i;
if (power >= 1e10)
break ;
}
if (power >= (1e10) ||
power > (sum - n) + a[n - 1])
break ;
if (minmove > count)
{
minmove = count;
K = i;
}
}
document.write(K);
}
let a = [ 1, 9, 27 ];
findMinCostLeteger(a, 3);
</script>
|
Time Complexity: O(N * max(x))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...