# Which m numbers in the range [1, k] have sum equal to n

Given the values of $n, k, m$, you have to check if there are $m$ distinct numbers in the range from 1 to $k$ have the sum $n$ or not ?

$n$ represents the required sum $k$ represents numbers range(1,2,3,... to k). $m$ is the number of elements in the range from 1 to $k$ that has the sum $n$.

Example: n = 12, k = 8, m = 3 ==> there are 3 numbers in the range from 1 to 8 that have the sum 12 which are 2,3,7.

The problem is taken from hackerrank and called bonetrousle.

• Welcome to Computer Science! What have you tried? Where did you get stuck? We do not want to just hand you the solution; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See here for tips on asking questions about exercise problems. If you are uncertain how to improve your question, why not ask around in Computer Science Chat?
– Raphael
Sep 24 '16 at 18:04
• Hi @Evil, don't worry it is a practice and I got to this stack to get algorithmic and scientific sense behind the problem. You can check the full code here github.com/PeterASteele/HackerRankProblems/blob/master/… Sep 24 '16 at 18:33
• I think no as the problem stated above may have more solutions like. 3 4 5 ==> sum = 12 and 2 3 7 ==> sum = 12 Sep 24 '16 at 18:52
• Unique was about the numbers to avoid situation where we can pick 12 = 4 + 4 + 4.
– Evil
Sep 24 '16 at 19:08
• yes the numbers should be unique. the answer 4 + 4 + 4 is incorrect. Sep 24 '16 at 19:24

1. What is the smallest number that can be represented as the sum of $m$ distinct integers from $1$ to $k$?
2. What is the largest number that can be represented as the sum of $m$ distinct integers from $1$ to $k$?