0

Given a consecutive list of numbers of size $n$, and a total sum figure $t$, what is the simplest way of finding the minimum $min$ and maximum $max$ numbers of that consecutive list?

For example,

$t = 10$

$n = 4$

$1 + 2 + 3 + 4 = 10$

$min = 1, max = 4$

Is there a way to find out the min/max for any sum value?

Updated: Added size of list condition

Carl
  • 405

2 Answers2

2

If list size = even like in your case, the minimum is $\lfloor sum/listsize\rfloor - (listsize/2-1)$ and the maximum is $\lceil sum/listsize\rceil + (listsize/2-1)$.

If list size = odd, the minimum is $ sum/listsize- (listsize-1)/2$ and the maximum is $ sum/listsize + (listsize-1)/2$.

Vaneet
  • 1,493
  • How would this work for lists of various sizes? Say I want 10 numbers, or maybe 100 numbers? – Carl Jun 11 '16 at 00:18
  • Based on changed notations, listsize is $n$ and sum = $t$. If $n$ is even, the mean is fractional, there are $(n/2-1)$ below the floor and same above the ceiling, which gives those numbers in the result. For $n$ odd, mean gives the center number and there are $(n-1)/2$ numbers below and above that. Hope this helps see the result mentioned. So, it works for any $n$,$t$. – Vaneet Jun 11 '16 at 04:25
  • I've tried this, but it just doesn't seem to work. Given $t=1000$ and $n=20$. I get 41 and 59 as the mix/max. That sums to 950 and only has 18 consecutive numbers – Carl Jun 11 '16 at 04:52
  • You cannot get 20 consecutive numbers with sum of 1000 - since 20 is even, sum/20 has to have 0.5 fractional part. – Vaneet Jun 11 '16 at 04:55
  • In your case of 41 to 59 with sum 950 - try $n=19$, $t=950$, and you will get the needed answer. – Vaneet Jun 11 '16 at 04:57
  • That makes sense, thanks. I also just noticed that you can get negative min numbers. Any way to ensure this is positive numbers only? e.g. $t=1500$, $n=100$ – Carl Jun 12 '16 at 06:02
  • Can you first explain which 100 consecutive numbers sum to 1500 - by the same logic as before since $1500/100$ is integer and $100$ is even, this is not a valid choice. – Vaneet Jun 12 '16 at 06:21
  • ahh okay, I'm working approximately. The person I'm writing the code for, won't necessarily know what a "valid" choice is. 1500/100 gives me -34 and 64. – Carl Jun 12 '16 at 11:43
0

Let the first number be n then the sum of four consecutive numbers are

$$n+n+1+n+2+n+3=4n+6$$

Now $$4n+6=given sum$$

Find n and n+3

the number of numbers in the list must be given so that u can add those many numbers and then equate it to the given sum then find the min and max of these numbers.

Jasser
  • 1,976