- 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

# Minimum numbers which is smaller than or equal to N and with sum S in C++

## Problem statement

Given N numbers from 1 to N and a number S. The task is to print the minimum number of numbers that sum up to give S

## Example

If n = 7 and s = 10 then minimum 2 numbers are required

(9, 1) (8, 2) (7, 3) (6, 4)

## Algorithm

Answer can be calculated using below formula (S/N) + 1 if { S %N > 0}

## Example

#include <bits/stdc++.h> using namespace std; int getMinNumbers(int n, int s) { return s % n ? s / n + 1 : s / 2; } int main() { int n = 7; int s = 10; cout << "Required minimum numbers = " << getMinNumbers(n, s) << endl; return 0; }

When you compile and execute above program. It generates following output

## Output

Required minimum numbers = 2

- Related Questions & Answers
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Euler’s Totient function for all numbers smaller than or equal to n in java
- Largest number smaller than or equal to N divisible by K in C++
- Print all Jumping Numbers smaller than or equal to a given value in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Find all factorial numbers less than or equal to n in C++
- Print all prime numbers less than or equal to N in C++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Count of Binary Digit numbers smaller than N in C++
- An interesting solution to get all prime numbers smaller than n?
- First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++
- Minimum number of power terms with sum equal to n using C++.
- Find maximum product of digits among numbers less than or equal to N in C++
- Count elements smaller than or equal to x in a sorted matrix in C++
- Print triplets with sum less than or equal to k in C Program

Advertisements