DEV Community 👩‍💻👨‍💻

Discussion on: Daily Challenge #283 - Simple Missing Sum

Collapse
bubbler4 profile image
Bubbler

APL (using Dyalog APL):

solve←{⊃(⍳1++/⍵)~⊃(+,,)/⍵}

(Use the bookmarklet on this post to see the code with APL font and syntax highlighting.)

Not very fast (O(2^n) where n is the length of the input array), but definitely gets the job done, concisely. Demo is here. Note that an array of ones (e.g. [1, 1, 1, 1]) is a corner case, because the answer is one higher than the sum of the input numbers.

Explanation:

solve←{⊃(⍳1++/⍵)~⊃(+,,)/⍵}
{...}     ⍝ Define a function
⊃(...)/⍵  ⍝ Reduce the input by...
+,,       ⍝  concatenation of both sides and
          ⍝  pairwise sum
~         ⍝ Remove the numbers in the above from
(⍳1++/⍵)  ⍝ Generate 1..(sum of ⍵ + 1)
⊃         ⍝ Take the first (smallest) number