Minimum cost to form a number X by adding up powers of 2
Last Updated :
15 Jun, 2022
Given an array arr[] of N integers and an integer X. Element arr[i] in array denotes the cost to use 2i. The task is to find the minimum cost to choose the numbers which add up to X.
Examples:
Input: arr[] = { 20, 50, 60, 90 }, X = 7
Output: 120
22 + 21 + 20 = 4 + 2 + 1 = 7 with cost = 60 + 50 + 20 = 130
But we can use 22 + 3 * 20 = 4 + 3 * 1 = 7 with cost = 60 + 3 * 20 = 120 which is minimum possible.
Input: arr[] = { 10, 5, 50 }, X = 4
Output: 10
Approach: The problem can be solved using basic Dynamic Programming. The fact that every number can be formed using powers of 2 has been used here. We can initially calculate the minimal cost required to form powers of 2 themselves. The recurrence will be as follows:
a[i] = min(a[i], 2 * a[i – 1])
Once the array is re-computed, we can simply keep adding the cost according to the set bits in the number X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumCost( int a[], int n, int x)
{
for ( int i = 1; i < n; i++) {
a[i] = min(a[i], 2 * a[i - 1]);
}
int ind = 0;
int sum = 0;
while (x) {
if (x & 1)
sum += a[ind];
ind++;
x = x >> 1;
}
return sum;
}
int main()
{
int a[] = { 20, 50, 60, 90 };
int x = 7;
int n = sizeof (a) / sizeof (a[0]);
cout << MinimumCost(a, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MinimumCost( int a[], int n, int x)
{
for ( int i = 1 ; i < n; i++)
{
a[i] = Math.min(a[i], 2 * a[i - 1 ]);
}
int ind = 0 ;
int sum = 0 ;
while (x > 0 )
{
if (x != 0 )
sum += a[ind];
ind++;
x = x >> 1 ;
}
return sum;
}
public static void main (String[] args)
{
int a[] = { 20 , 50 , 60 , 90 };
int x = 7 ;
int n =a.length;
System.out.println (MinimumCost(a, n, x));
}
}
|
Python3
def MinimumCost(a, n, x):
for i in range ( 1 , n, 1 ):
a[i] = min (a[i], 2 * a[i - 1 ])
ind = 0
sum = 0
while (x):
if (x & 1 ):
sum + = a[ind]
ind + = 1
x = x >> 1
return sum
if __name__ = = '__main__' :
a = [ 20 , 50 , 60 , 90 ]
x = 7
n = len (a)
print (MinimumCost(a, n, x))
|
C#
using System;
class GFG
{
public static int MinimumCost( int []a, int n, int x)
{
for ( int i = 1; i < n; i++)
{
a[i] = Math.Min(a[i], 2 * a[i - 1]);
}
int ind = 0;
int sum = 0;
while (x > 0)
{
if (x != 0 )
sum += a[ind];
ind++;
x = x >> 1;
}
return sum;
}
public static void Main ()
{
int []a = { 20, 50, 60, 90 };
int x = 7;
int n =a.Length;
Console.WriteLine(MinimumCost(a, n, x));
}
}
|
PHP
<?php
function MinimumCost( $a , $n , $x )
{
for ( $i = 1; $i < $n ; $i ++)
{
$a [ $i ] = min( $a [ $i ], 2 * $a [ $i - 1]);
}
$ind = 0;
$sum = 0;
while ( $x )
{
if ( $x & 1)
$sum += $a [ $ind ];
$ind ++;
$x = $x >> 1;
}
return $sum ;
}
$a = array ( 20, 50, 60, 90 );
$x = 7;
$n = sizeof( $a ) / sizeof( $a [0]);
echo MinimumCost( $a , $n , $x );
?>
|
Javascript
<script>
function MinimumCost(a , n , x) {
for (i = 1; i < n; i++) {
a[i] = Math.min(a[i], 2 * a[i - 1]);
}
var ind = 0;
var sum = 0;
while (x > 0) {
if (x != 0)
sum += a[ind];
ind++;
x = x >> 1;
}
return sum;
}
var a = [ 20, 50, 60, 90 ];
var x = 7;
var n = a.length;
document.write(MinimumCost(a, n, x));
</script>
|
Time Complexity: O(N + log(x)), as we are using a loop to traverse N times and log(x) times as in every traversal we are right shifting x by 1 bit which will be equivalent to log(x).
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...