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: 4
Approach: 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)
break
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #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()); } // Function to return the count // of minimum coins required int findCount(int N) { // To store the required sequence vector<int> list; int sum = 0; int i; // Creating list of the sum of all // previous bit values including // that bit value 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); } } } // Driver Code int main() { int N = 10; cout << findCount(N) << endl; return 0; } // This code is contributed by kanugargng |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the count // of minimum coins required static int findCount(int N) { Vector list = new Vector(); int sum = 0; int i; // Creating list of the sum of all // previous bit values including // that bit value 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; } // Driver Code public static void main (String[] args) { int N = 10; System.out.println(findCount(N)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the count # of minimum coins required def findCount(N): # To store the required sequence list = [] sum = 0 # Creating list of the sum of all # previous bit values including # that bit value for i in range(0, 20): sum += 2**i list.append(sum) for value in list: if(value>N): return (list.index(value) + 1) # Driver code N = 10print(findCount(N)) |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to return the count // of minimum coins required static int findCount(int N) { List<int> list = new List<int>(); int sum = 0; int i; // Creating list of the sum of all // previous bit values including // that bit value 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; } // Driver Code public static void Main (String[] args) { int N = 10; Console.WriteLine(findCount(N)); } } // This code is contributed by PrinciRaj1992 |
4
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:
// CPP progarm to find minimum number of coins #include<bits/stdc++.h> using namespace std; // Function to find minimum number of coins int findCount(int n) { return log(n)/log(2)+1; } // Driver code int main() { int N = 10; cout << findCount(N) << endl; return 0; } |
4
Time complexity : 0(log(n))
Recommended Posts:
- Find minimum number of coins that make a given value
- Number of subarrays with maximum values in given range
- Range Queries to count the number of even parity values with updates
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Buy minimum items without change and given coins
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Minimum number of towers required such that every house is in the range of at least one tower
- Number of paths with exactly k coins
- Generate array with minimum sum which can be deleted in P steps
- Queries for elements having values within the range A to B using MO's Algorithm
- Generate two BSTs from the given array such that maximum height among them is minimum
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Queries for counts of array elements with values in given range
- Construct a Matrix of size NxN with values in range [1, N^2] as per given conditions
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed
- Count of non decreasing arrays of length N formed with values in range L to R
- Dynamic Disjoint Set Data Structure for large range values
- Queries for count of array elements with values in given range with updates
- Minimum and Maximum values of an expression with * and +
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


