Given N numbers(1, 2, 3, ….N) and a number S. The task is to print the minimum number of numbers that sum up to give S.

**Examples**:

Input: N = 5, S = 11

Output: 3

Three numbers (smaller than or equal to N) can be any of the given combinations.

(3, 4, 4)

(2, 4, 5)

(1, 5, 5)

(3, 3, 5)Input: N = 1, S = 10

Output: 10

**Approach**: Greedily we choose N as many times we can, and then if anything less than N is left we will choose that number which adds up to give S, hence the total number of numbers are **(S/N) + 1(if S%N>0)**.

Below is the implementation of the above approach.

## C++

`// C++ program to find the minimum numbers ` `// required to get to S ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum ` `// numbers required to get to S ` `int` `minimumNumbers(` `int` `n, ` `int` `s) ` `{ ` ` ` `if` `(s % n) ` ` ` `return` `s / n + 1; ` ` ` `else` ` ` `return` `s / n; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `s = 11; ` ` ` `cout << minimumNumbers(n, s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the minimum numbers ` `// required to get to S ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the minimum ` `// numbers required to get to S ` `static` `int` `minimumNumbers(` `int` `n, ` `int` `s) ` `{ ` ` ` `if` `((s % n)>` `0` `) ` ` ` `return` `s / n + ` `1` `; ` ` ` `else` ` ` `return` `s / n; ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `5` `; ` ` ` `int` `s = ` `11` `; ` ` ` `System.out.println(minimumNumbers(n, s)); ` ` ` `} ` `} ` `// This code is contributed by shs.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find the ` `# minimum numbers required to get to S ` ` ` `# Function to find the minimum ` `# numbers required to get to S ` `def` `minimumNumbers(n, s): ` ` ` ` ` `if` `(s ` `%` `n): ` ` ` `return` `s ` `/` `n ` `+` `1` `; ` ` ` `else` `: ` ` ` `return` `s ` `/` `n; ` ` ` `# Driver Code ` `n ` `=` `5` `; ` `s ` `=` `11` `; ` `print` `(` `int` `(minimumNumbers(n, s))); ` ` ` `# This code is contributed ` `# by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the minimum numbers ` `// required to get to S ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the minimum ` `// numbers required to get to S ` `static` `int` `minimumNumbers(` `int` `n, ` `int` `s) ` `{ ` ` ` `if` `((s % n)>0) ` ` ` `return` `s / n + 1; ` ` ` `else` ` ` `return` `s / n; ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `n = 5; ` ` ` `int` `s = 11; ` ` ` `Console.WriteLine(minimumNumbers(n, s)); ` ` ` `} ` `} ` `// This code is contributed by shs.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the minimum numbers ` `// required to get to S ` ` ` `// Function to find the minimum ` `// numbers required to get to S ` `function` `minimumNumbers(` `$n` `, ` `$s` `) ` `{ ` ` ` `if` `(` `$s` `% ` `$n` `) ` ` ` `return` `round` `(` `$s` `/ ` `$n` `+ 1); ` ` ` `else` ` ` `return` `round` `(` `$s` `/` `$n` `); ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `$s` `= 11; ` `echo` `minimumNumbers(` `$n` `, ` `$s` `); ` ` ` `// This code is contributed by shs.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print all Jumping Numbers smaller than or equal to a given value
- Euler's Totient function for all numbers smaller than or equal to n
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest number smaller than or equal to N divisible by K
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Count of Binary Digit numbers smaller than N
- An interesting solution to get all prime numbers smaller than n
- Cube Free Numbers smaller than n
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Sieve of Sundaram to print all primes smaller than n
- Maximize a number considering permutations with values smaller than limit
- Find largest number smaller than N with same set of digits
- Previous perfect square and cube number smaller than number N
- Find the largest number smaller than integer N with maximum number of set bits
- Maximize number of groups formed with size not smaller than its largest element
- Count of array elements which is smaller than both its adjacent elements
- Largest number less than N with digit sum greater than the digit sum of N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Sum of Semi-Prime Numbers less than or equal to N

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.