

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to count number of minimum coins needed to get sum k
Suppose we have two numbers n and k. We have unlimited number of coins worth values 1 to n. We want to take some values whose sum is k. We can select multiple same valued coins to get total sum k. We have to count the minimum number of coins needed to get the sum k.
So, if the input is like n = 6; k = 16, then the output will be 3, because (2 * 6) + 4.
Steps
To solve this, we will follow these steps −
c := (n + k - 1) / n return c
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; int solve(int n, int k){ int c=(n+k-1)/n; return c; } int main(){ int n = 6; int k = 16; cout << solve(n, k) << endl; }
Input
6, 16
Output
3
- Related Questions & Answers
- C++ program to count number of operations needed to reach n by paying coins
- C++ program to count minimum number of operations needed to make number n to 1
- Program to find number of coins needed to make the changes with given set of coins in Python
- C++ program to find minimum how many coins needed to buy binary string
- Program to find number of coins needed to make the changes in Python
- C++ program to count minimum number of binary digit numbers needed to represent n
- Program to find minimum element addition needed to get target sum in Python
- Program to count number of paths whose sum is k in python
- C/C++ Program for Greedy Algorithm to find Minimum number of Coins
- C++ program to count number of steps needed to make sum and the product different from zero
- Program to find minimum number of rocketships needed for rescue in Python
- Program to find maximum number of coins we can get using Python
- Program to count minimum deletions needed to make character frequencies unique in Python
- Program to find minimum largest sum of k sublists in C++
- Program to count number of ways we can distribute coins to workers in Python
Advertisements