- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 Articles
- 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
- C++ program to count minimum number of binary digit numbers needed to represent n
- C++ program to find minimum how many coins needed to buy binary string
- Program to find minimum element addition needed to get target sum 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 number of coins needed to make the changes with given set of coins in Python
- Program to find number of coins needed to make the changes in Python
- Program to find minimum largest sum of k sublists in C++
- Program to count number of paths whose sum is k in python
- C++ program to find minimum number of steps needed to move from start to end
- C++ program to find minimum how many operations needed to make number 0
- C++ program to count expected number of operations needed for all node removal
- C++ program to find minimum number of punches are needed to make way to reach target

Advertisements