Given an integer N, the task is to find the minimum number of coins required to create all the values in the range [1, N].
Examples:
Input: N = 5
Output: 3
The coins {1, 2, 4} can be used to generate
all the values in the range [1, 5].
1 = 1
2 = 2
3 = 1 + 2
4 = 4
5 = 1 + 4
Input: N = 10
Output: 4Approach: The problem is a variation of coin change problem, it can be solved with the help of binary numbers. In the above example, it can be seen that to create all the values between 1 to 10, denominator {1, 2, 4, 8} are required which can be rewritten as {20, 21, 22, 23}. The minimum number of coins to create all the values for a value N can be computed using the below algorithm.
// A list which contains the sum of all previous
// bit values including that bit value
list = [ 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023]
// range = N
for value in list:
if(value >= N):
print(list.index(value) + 1)
breakBelow is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int index(vector<int> vec, int value)
{
vector<int>::iterator it;
it = find(vec.begin(), vec.end(), value);
return (it - vec.begin());
}
int findCount(int N)
{
vector<int> list;
int sum = 0;
int i;
for (i = 0; i < 20; i++) {
sum += pow(2, i);
list.push_back(sum);
}
for (i = 0; i < 20; i++) {
if (list[i] >= N) {
return (index(list, list[i]) + 1);
}
}
}
int main()
{
int N = 10;
cout << findCount(N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int findCount(int N)
{
Vector list = new Vector();
int sum = 0;
int i;
for (i = 0; i < 20; i++) {
sum += Math.pow(2, i);
list.add(sum);
}
for (i = 0; i < 20; i++) {
if ((int)list.get(i) >= N)
return (list.indexOf(list.get(i)) + 1);
}
return 0;
}
public static void main(String[] args)
{
int N = 10;
System.out.println(findCount(N));
}
}
|
Python3
def findCount(N):
list = []
sum = 0
for i in range(0, 20):
sum += 2**i
list.append(sum)
for value in list:
if(value >= N):
return (list.index(value) + 1)
N = 10
print(findCount(N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findCount(int N)
{
List<int> list = new List<int>();
int sum = 0;
int i;
for (i = 0; i < 20; i++) {
sum += (int)Math.Pow(2, i);
list.Add(sum);
}
for (i = 0; i < 20; i++) {
if ((int)list[i] >= N)
return (i + 1);
}
return 0;
}
public static void Main(String[] args)
{
int N = 10;
Console.WriteLine(findCount(N));
}
}
|
Javascript
<script>
function findCount(N)
{
let list = [];
let sum = 0;
let i;
for (i = 0; i < 20; i++) {
sum += parseInt(Math.pow(2, i), 10);
list.push(sum);
}
for (i = 0; i < 20; i++) {
if (list[i] >= N)
return (i + 1);
}
return 0;
}
let N = 10;
document.write(findCount(N));
</script>
|
Time Complexity: O(n)
Efficient approach: The minimum number of coins required to create all the values in the range [1, N] will be log(N)/log(2) + 1.
Below is the implementation of the above approach:
C++14
#include<bits/stdc++.h>
using namespace std;
int findCount(int n)
{
return log(n)/log(2)+1;
}
int main()
{
int N = 10;
cout << findCount(N) << endl;
return 0;
}
|
Java
class GFG{
static int findCount(int n)
{
return (int)(Math.log(n) /
Math.log(2)) + 1;
}
public static void main(String[] args)
{
int N = 10;
System.out.println(findCount(N));
}
}
|
Python3
import math
def findCount(n):
return int(math.log(n, 2)) + 1
N = 10
print(findCount(N))
|
C#
using System;
class GFG{
static int findCount(int n)
{
return (int)(Math.Log(n) /
Math.Log(2)) + 1;
}
public static void Main()
{
int N = 10;
Console.Write(findCount(N));
}
}
|
Javascript
<script>
function findCount(n)
{
return parseInt(Math.log(n)/Math.log(2), 10)+1;
}
let N = 10;
document.write(findCount(N));
</script>
|
Time complexity : O(log(n))
Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Competitive Programming Live course.