[Question]
  [
The difference list of a list of integers is the list differences of consecutive members.
For example the difference list of
```
1, 3, 2 ,4
```
is
```
2, -1, 2
```
Your task is to take as input a difference list and output what the difference list would look like if the original list were sorted.
For example the difference list
```
2, 1, -2, -1
```
Might represent a list
```
2 4 5 3 2
```
Which when sorted is
```
2 2 3 4 5
```
Which has a difference list of
```
0 1 1 1
```
---
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so answers will be scored in bytes with less bytes being better.
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
.¥{¥
```
[Try it online!](https://tio.run/##MzBNTDJM/f9f79DS6kNL//@PNtJRMNRR0AVSuoaxAA "05AB1E – Try It Online")
### Explanation
```
.¥{¥
.¥   # Undelta the input list
  {  # Sort it
   ¥ # And get the deltas
```
[Answer]
# [Python 3](https://docs.python.org/3/) with [Numpy](http://www.numpy.org/), ~~56~~ ~~54~~ 53 bytes
*2 bytes off thanks to [@Artyer](https://codegolf.stackexchange.com/users/48878/artyer) (Numpy's `sort` instead of standard `sorted`). 1 byte off thanks to [@notjagan](https://codegolf.stackexchange.com/users/63641/notjagan) (moving `0` into `cumsum`)*
```
lambda x:diff(sort(cumsum([0]+x)))
from numpy import*
```
The code defines an anonymous function that inputs a list or a Numpy array and outputs a Numpy array.
[Try it online!](https://tio.run/##Dco7DoAgDADQ3VN0bFUS0c3Ek6iDnzSSWCAIiZ4emd7y/BcvZ4fM05LvTfZzg3c8DTM@LkQ8kjxJcO7W5iWiioMTsEn8B0Z8GXX2wdiIjHPfgm5BFZReifIP "Python 3 – Try It Online")
[Answer]
# Mathematica, 40 bytes
```
Differences@Sort@Accumulate@Join[{1},#]&
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
0;+\ṢI
```
[Try it online!](https://tio.run/##y0rNyan8/9/AWjvm4c5Fnv8Ptx@d9HDnjP//o410FAx1FHSBlK5hLAA "Jelly – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 4 bytes
```
Ẋ-O∫
```
[Try it online!](https://tio.run/##yygtzv7//@GuLl3/Rx2r////H22kY6ija6SjaxgLAA "Husk – Try It Online")
### Explaination
```
      -- implicit input, e.g                               [2,1,-2,-1]
   ∫  -- cumulative sum                                    [0,2,3,1,0]
  O   -- sort                                              [0,0,1,2,3]
Ẋ     -- apply function to all adjacent pairs in the list  [(0,0),(0,1),(1,2),(2,3)]
 -    --   subtract                                        [0,1,1,1]
```
[Answer]
# [Pyth](https://pyth.readthedocs.io), 9 bytes
*-1 byte thanks to @EriktheOutgolfer*.
```
.+S+0sM._
```
**[Test Suite.](http://pyth.herokuapp.com/?code=.%2BS%2B0sM._&input=%5B2%2C+1%2C+-2%2C+-1%5D&test_suite=1&test_suite_input=%5B2%2C+-1%2C+2%5D%0A%5B2%2C+1%2C+-2%2C+-1%5D&debug=0)**
# [Pyth](https://pyth.readthedocs.io), 10 bytes
```
.+S.u+YNQ0
```
[Try it online!](http://pyth.herokuapp.com/?code=.%2BS.u%2BYNQ0&input=%5B2%2C+1%2C+-2%2C+-1%5D&debug=0) or [Try more test cases](http://pyth.herokuapp.com/?code=.%2BS.u%2BYNQ0&input=%5B2%2C+1%2C+-2%2C+-1%5D&test_suite=1&test_suite_input=%5B2%2C+-1%2C+2%5D%0A%5B2%2C+1%2C+-2%2C+-1%5D&debug=0).
[Answer]
## JavaScript (ES6), ~~57~~ 56 bytes
*Saved 1 byte thanks to [@ETHproductions](https://codegolf.stackexchange.com/users/42545/ethproductions)*
```
a=>a.map(n=>t-=n,p=t=0).sort((a,b)=>b-a).map(n=>p-(p=n))
```
### Demo
```
let f =
a=>a.map(n=>t-=n,p=t=0).sort((a,b)=>b-a).map(n=>p-(p=n))
console.log(JSON.stringify(f([2, 1, -2, -1])))
```
[Answer]
# Java 8, 123 bytes
The standard solution: cumulative sum input, sort, then diff. No substantial implementation tricks either.
```
l->{int s=l.length,d[]=new int[s+1],i=0;while(i<s)d[i+1]=d[i]+l[i++];for(java.util.Arrays.sort(d);i-->0;)l[i]=d[i+1]-d[i];}
```
Cast to `Consumer<int[]>`. Output is mutated input.
[Try It Online](https://tio.run/##ZVC7bsMwDJztr@BoI5aQdFUcoOjcKehkaFBtOWUqS4YkJwgCf7tL5bGkEwneHXnHozop5kZtj93vgsPofIQjzfgU0fB379UliPwf0E@2jegs/3A2TIP2Is/H6dtgC61RIcCnQgvXPBs9nlTUEKKKBD7pW7SxkTvosO/3tFp3Xzb1UC@G7a6EQqgNN9oe4k/VNbK2@gxJFFYbWWG9FucfNLrAbSi7BmlYU5ErQ/1Kit754jUGD3So6EqBjO3WoiTqTURalrRiXjJBju8xHoZPDjsYKEyxjx7toZGg/CGUKVt2C0GuoIanPQnXtwo2FTAqbDPTwuw1JFdtq8dYoC0TvL@EqAfupsjpWzYaWzwMR3c/mpiJOufz8gc)
## Ungolfed lambda
```
l -> {
    int
        s = l.length,
        d[] = new int[s + 1],
        i = 0
    ;
    while (i < s)
        d[i + 1] = d[i] + l[i++];
    for (java.util.Arrays.sort(d); i-- > 0; )
        l[i] = d[i + 1] - d[i];
}
```
## Acknowledgments
* -3 bytes thanks to *Olivier Grégoire*, master of unholy autoincrementation
* -1 byte thanks to *Nevay*
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 15 bytes
```
a₀ᶠ+ᵐ,0o{s₂↔-}ᶠ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/P/FRU8PDbQu0H26doGOQX138qKnpUdsU3Vqg2P//0UY6hjrxRjrxhrH/owA "Brachylog – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~31~~ 32 bytes
*-4 bytes thanks to @user2390246 for* `diffinv`
*+5 bytes from Jarko for `cat`*
```
cat(diff(sort(diffinv(scan()))))
```
Reads from stdin, writes to stdout. `diffinv` is an inverse of `diff` for a given starting value (0 by default). Since it's `diff`ed again, it doesn't matter what that value is.
As pointed out by Jarko Dubbeldam, I needed to properly output the result, at the cost of five bytes. Alas.
[Try it online!](https://tio.run/##K/r/PzmxRCMlMy1Nozi/CMLKzCvTKE5OzNPQBIH/RgqGCrpGCrqG/wE "R – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 83 bytes
```
l,r=input(),[1]
for i in l:r+=[r[-1]+i]
r.sort()
print[b-a for a,b in zip(r,r[1:])]
```
[Try it online!](https://tio.run/##FcsxCoAwDEbhvafIqJgKdRQ8SciggxiQtvzWQS9f6/SW7@WnHClOtZ6MxWK@S9ezBHV7AhlZpHPGsAjEBx1MHcYroSGXYbHI5lf66crbj1/LHRgSZu21VpmYApNvafsH "Python 2 – Try It Online")
*Horrible* solution.
[Answer]
# [Perl 6](https://perl6.org), 46 bytes
```
{[\+](0,|@_).sort.rotor(2=>-1).flat.map(*R-*)}
```
[Try it](https://tio.run/##K0gtyjH7n1upoJacn5KqYPu/OjpGO1bDQKfGIV5Trzi/qESvKL8kv0jDyNZO11BTLy0nsUQvN7FAQytIV0uz9n9xYqUCWKeRjoKhjoIukNI1/A8A "Perl 6 – Try It Online")
## Expanded:
```
{  # bare block lambda with implicit signature :(*@_)
  [\+](         # triangle reduce using &infix:«+»
    0,          # start with 0
    |@_         # Slip in the arguments from the outer block
  )             #                  (0, 2, 3, 1, 0)
  .sort         # sort the results (0,0,1,2,3)
  .rotor(2=>-1) # group in twos    ((0,0),(0,1),(1,2),(2,3))
  .flat         # flatten          (0,0,0,1,1,2,2,3)
  .map(*R-*)    # grab 2 values at a time, and subtract first from second
                # (0, 1, 1, 1)
}
```
[Answer]
# [Haskell](https://www.haskell.org/), 74 bytes
```
import Data.List
g=sort.scanl(+)0
h l|k<-g l=map(\(x,y)->x-y)$zip(tail$k)k
```
[Try it online!](https://tio.run/##DcixDsIgEADQWb7iBgaI0Fhn6eToH1iHi5py4aCkMLTGf6edXvI8lvBlbo1inpcKd6zYPahUMblyRFfemFid9UV44H@42QnYRcxqVKvZtB1Wu2n5o6wqEsugQ4tICRx8ZnHKC6UKEvzzaqA3YA9s/2o7 "Haskell – Try It Online")
Straightforward.
[Answer]
# TI-Basic (TI-84 Plus CE), 23 [bytes](http://tibasicdev.wikidot.com/one-byte-tokens)
```
Prompt X
augment({0},cumSum(LX→X
SortA(LX
ΔList(LX
```
Prompts for user input. The list must be input with a leading `{`, with numbers separated by `,`, and with an optional trailing `}`.
TI-Basic is a [tokenized language](http://tibasicdev.wikidot.com/one-byte-tokens); `ΔList(` and `cumSum(` are two-byte tokens, all other tokens used are one byte each.
Example run (with `NAME` as the program name and `{4,-2,7,-4,0}` as the input):
```
prgmNAME
X=?{4,-2,7,-4,0}
               {2 2 1 0 4}
```
Explanation:
```
Prompt X                  # 3 bytes, get list input, store in LX
augment({0},cumSum(LX→X   # 12 bytes, 
          # store the list ({0} prepended to the cumulative sum of LX) to LX
SortA(LX                  # 4 bytes, sort LX ascending
ΔList(LX                  # 4 bytes, implicitly print the difference list of LX
```
[Answer]
# [C++ (gcc)](https://gcc.gnu.org/), 136 bytes
As unnamed generic lambda, assuming input to be like `std::list` and returning via reference parameter.
```
[](auto&L){auto r=L.begin(),l=L.insert(r,0);while(r!=L.end())*r+++=*l++;for(L.sort(),l=r=--L.end();--l!=L.begin();*r---=*l);L.erase(l);}
```
[Try it online!](https://tio.run/##VY5BbsMgEEX3nGLiShUYiJoujfEJuEHbhYtxikQgAqxGsnx2F9KqVTczX2/@nxl9vfKz1vuD9dotk@ltSDma8TKgX@RsygNC45IDzHJ/ecNVPiqy3lGU6vhuztZjwlzR1icTM47siYjPD@sMjoeCjZ8wIW2klMrWUSrmELE6plC8NRgl5z8uwbk7/G0VbeSclxARxRDHZHCR2y4Qsj7DZaymFUHKU9fVZ/uCB1AgYX1mcGLAS@OnTSCYsSK1ldv352@dIt9BHZYMfQ@3WhoGjUD/B82rL2xD@xc "C++ (gcc) – Try It Online")
Ungolfed:
```
[](auto&L){
 auto r=L.begin(),
      l=L.insert(r,0); //adds a zero right in front
 while(r!=L.end())
   *r++ += *l++;       //sum left to right
 for(
  L.sort(),            //sorting invalidates the iterators
  l=r=--L.end();       //so, reinit
  --l!=L.begin();      //decrement l beforehand 
  *r-- -= *l           //diff right to left
 );
 L.erase(l);           //l==L.begin(), so this removes the temporary 0
}
```
[Answer]
# Pyth, 8 bytes
```
.+S+M.uP
```
[Demonstration](https://pyth.herokuapp.com/?code=.%2BS%2BM.uP&input=%5B2%2C+1%2C+-2%2C+-1%5D&debug=0)
```
.+S+M.uP
.+S+M.uPNQ    Implicit variables
     .u  Q    Apply the following function to the input repeatedly until it
              stops changing, then output the list of values, including the
              starting value.
       PN     Remove the last element. No-op if the list is empty.
   +M         Sum each list. This gives the cumulative sums in reverse order,
              including a 0 at the end for the empty list.
  S           Sort
.+            Deltas
```
[Answer]
# TI-Basic, 20 bytes
```
cumSum(augment({0},Ans->L1
SortA(L1
ΔList(L1
```
[Answer]
# [Perl 5](https://www.perl.org/), 87 + 1 (-a) = 88 bytes
```
$a[0]=1;push@a,$a[-1]+$_ for@F;@a=sort{$a<=>$b}@a;print$a[0]-$_,$"while($_=shift@a)&&@a
```
[Try it online!](https://tio.run/##HctBCsIwEEDRqwQZimIiidBVGpmVO08gJYzQkkBpQhJxIV7dsbj6vMXPU1l6ZqC7Hp2x@VkDktyozHgEL@ZU8GqRXE2lvYEGd4HHB8nmEtf23xR4CbtXiMu0B@9qiHNDOnQdEvNZCiOF2qLMN@UW01pZ3fqTNpoV/QA "Perl 5 – Try It Online")
[Answer]
# VB.NET (.NET 4.5), 109 bytes
```
Sub A(n)
Dim c=n.count-1
For i=1To c
n(i)+=n(i-1)
Next
n.Sort()
For i=c To 1 Step-1
n(i)-=n(i-1)
Next
End Sub
```
A function that expects a list as input and modifies it directly. The original parameter can then be used for output
1. Recreates an original list by adding forwards through the list (assumes an implicit 0 as the first element)
2. Sorts the original list
3. Gets the differences by going backwards (so I don't need to keep track of a different list) (the implicit first element of 0 means the first difference is the same as the smallest element)
[Try it online!](https://tio.run/##nVJNj5tADD3Dr7A4NKAWJHrPIeqXKjW9ZP/AMHiJ1cGOZsym2z@fekiqTXta9YJsP/Oe/TxPlBYX2sEl8i2jtrOwXGg@SdQEh@ekOHcfJAT0SsKp@4KMkXy5l3EJCPv@clgG2NUMLsE3SlrLI3xlxQlj05QfaQbK0K0EG8gKMAom3igEVHiWBYgDMcKIPriI8CgRgsgJnlwkNwRMK5N/FVNSx0pOERyPlpMlgX6hhaBHhORmhCxX@i13XhbWtje6nVfzYkWAElSr4ktHVX62qWjbw4PYJP/K0w@EYVElnqDqq1W7epAKVCY02VhyTc3brX3bvim/408tuTuYz3VzY/aZuYeD4mkd6H8E2r8EPlmP3edS5CPtHXHt4pSyi0mjEdV2oqIYbdGwXomu3v6p5RLj2cDrXW9wU1rDBvzR8YSrpbzMA8YEAwY520D3GPFpUfshQLcbx/p98xL3d3F7D@QFLNvVAXI1Pwd0/pintCMGKxXeXqME7M6RFGsD3kD1Dqrcznn14rb76sH1tV5@Aw)
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), ~~15~~ 14 bytes
-1 byte thanks to [ngn](https://codegolf.stackexchange.com/users/24908/ngn).
```
(¯2-/⍋⊃¨⊂)0,+\
```
`+\` cumulative sum
`0,` prepend a zero
`(`…`)` apply the following tacit function on that:
 `⊂` enclose (so we can pick multiple items)
 `⍋⊃¨` let each of the indices that would sort the argument pick from that
 `¯2-/` reversed pairwise difference
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R2wSNQ@uNdPUf9XY/6mo@tOJRV5OmgY52DFBOwUjBUAEoCcSGAA "APL (Dyalog Unicode) – Try It Online")
---
Original solution found by the Code Golf Hackathon participants at [the Dyalog '17 User Meeting](https://www.dyalog.com/user-meetings/dyalog17/workshops.htm#TP03):
```
¯2-/l[⍋l←+\0,⎕]
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKM97f@h9Ua6@jnRj3q7cx61TdCOMdB51Dc1FiT3XwEM0riMFAwVgMqA2BAA "APL (Dyalog Unicode) – Try It Online")
`⎕` prompt for input
`0,` prepend a zero
`+\` cumulative sum
`l←` store as `l`
`⍋` find the indices that will sort `l`
`l[`…`]` use that to index into `l`
`¯2-/` reversed pairwise difference
[Answer]
# [MATL](https://github.com/lmendo/MATL), 6 bytes
```
0hYsSd
```
[Try it online!](https://tio.run/##y00syfn/3yAjsjg45f//aCMFQwVdIwVdw1gA "MATL – Try It Online")
```
0       # push 0
 h      # horizontal concatenate with implicit input
  Ys    # cumulative sum
    S   # sort
     d  # diff (implicit output)
```
[Answer]
# J, 10 bytes
```
/:~&.(+/\)
```
# explanation
"sort under scan sum": In J, the Under conjunction `&.` applies the transformation to its right to the input, then applies the verb to its left (in this case sort `/:~`) and then does the reverse transformation. That is, J understands how to invert a scan sum, which is exactly what's needed here: the successive differences are the input that, when scan-summed, will produce that scan-sum.
[Try it online!](https://tio.run/##y/r/P81WT0Hfqk5NT0NbP0aTKzU5I18hTcFIwVAh3kgh3vD/fwA "J – Try It Online")
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
ÞRs¯0p
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQIiwiIiwiw55Sc8KvMHAiLCIiLCJbMiwxLC0yLC0xXSJd)
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), 7 bytes
```
1¤++⊣ȯọ
```
[Try it online!](https://tio.run/##S0/MTPz/3/DQEm3tR12LT6x/uLv3f@qjton/o40UDBV0jRR0DWMB "Gaia – Try It Online")
### Explanation
```
1¤+      Prepend 1
   +⊣    Cumulative sums
     ȯ   Sort
      ọ  Deltas
```
[Answer]
# [k](https://en.wikipedia.org/wiki/K_(programming_language)), 16 bytes
```
1_-':{x@<x}@+\0,
```
[Try it online!](https://tio.run/##y9bNz/7/P83KMF5X3aq6wsGmotZBO8ZA53@ag56VgVXCfyMFQwVdIwVdQwA "K (oK) – Try It Online")
```
              0, /prepend a 0
            +\   /cumulative sum
     {x@<x}@     /sort
1_-':            /differences list
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt/), 10 bytes
```
iT å+ n än
```
[Test it](http://ethproductions.github.io/japt/?v=1.4.5&code=aVQg5SsgbiDkbg==&input=Wy0yLCAxMDAsIC0yLCAtMV0=)
[Answer]
# [Röda](https://github.com/fergusq/roda), 42 bytes
```
{i=0{[0];[i]if i+=_}|sort|slide 2|[_2-_1]}
```
[Try it online!](https://tio.run/##DcuxDsIgFEbhWZ7iHzq0EZLCqOFJkNw2AslN1CZUJ@DZkeV808lH2HsRl4SbxaMXtmtxq7879pzAV0utnkf@1vPFIcJUR0aR9q03Id47fzBmZyS0hBoo7VGR5mU0x/B7xnmbSGKibRGt/wE "Röda – Try It Online")
This is similar to the [Perl 6 answer](https://codegolf.stackexchange.com/a/138590/66323). `.sort` is `|sort`, `.rotor(2=>-1).flat` is `|slide 2` and `.map(*R-*)` is `|[_2-_1]`.
Explanation:
```
{
  i=0 /* initialize variable i */
  /* the following block recreates the original list from differences: */
  {
    [0];       /* push 0 to the stream */
    [i]if i+=_ /* add every number in the stream to i and push i back */
  }|
  sort|    /* sort the numbers */
  slide 2| /* for values i1, i2, i3, ... in the stream
              push pairs i1, i2, i2, i3, ... */
  [_2-_1]  /* calculate difference of numbers in each pair in the stream */
}
```
The statement `[i]if i+=_` is equivalent to
```
for sfv do
  if i += sfv do
    push(i)
  done
done
```
The `+=` operator does not push values to the stream, so it is truthy. I could also have used some kind of block (eg. `{|j|i+=j;[i]}_`) to tie the addition and pushing statements together, but `if` is shorter.
[Answer]
## Julia 0.6.0 (34 bytes)
Pretty much a copy of what has been done in R and Python 3
`x->diff(sort(cumsum(vcat([0],x))))` 
[Answer]
# [Factor](https://factorcode.org/), 45 bytes
```
[ 0 prefix cum-sum natural-sort differences ]
```
[Try it online!](https://tio.run/##Jc1BCsIwEEbhfU/xXyDFdqkHEDduxJV0McSJBpO0zkxAEc8eKe4f3wvkbZZ2Ph2O@y2Un5WLZ8WDpXBCJrv3amRRLXqFzmKx3LAIm70XicWw67oPRgxwI9yAb9cu2KxFiC/4mp3WjEJWhZJbAVxjCCz/0dQ8pYS@/QA "Factor – Try It Online")
Add a 0 to the start of the input, cumulative sum, sort, differences.
]