Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Minimum numbers (smaller than or equal to N) with sum S

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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;
}

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..

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

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..

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..
?>

Javascript




<script>
 
// JavaScript 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 parseInt(s / n) + 1;
    else
        return parseInt(s / n);
}
 
// Driver Code
    let n = 5;
    let s = 11;
    document.write(minimumNumbers(n, s));
 
</script>

Output: 

3

 

Time Complexity: O(1)

Auxiliary Space: O(1), since no extra space has been taken.


My Personal Notes arrow_drop_up
Last Updated : 27 Aug, 2022
Like Article
Save Article
Similar Reads
Related Tutorials