Here's one approach:

   an0=: 2#0~:_2 +/\ ]
   an1=: ({.,(#~ an0)@}.@}:,{:)@(#~ an0)
   an2=: an1^:_
   an2 1 _1 2 _2{~100?.@#4
2 1 2 1 _2 _1 2 1 1 2 2 2 _1 2 _1 2 1 2 2 2 1 1 _2 _1 2 1 _2 _1 _2 _2
_2 _2 _1 _2 _1 _1 _2 _2 1 1 1 2 1 _2 1 _2 _1 _1 _1 2
   #an2 1 _1 2 _2{~100?.@#4
50

I have not convinced myself that there's no ambiguity in this problem,
and my approach seems like it has room for improvement, but I get the
same result you got.

Thanks,

-- 
Raul

On Fri, Dec 6, 2019 at 7:15 AM R.E. Boss <[email protected]> wrote:
>
> Given an array with zero or more annihilating pairs, i.e., two subsequent 
> numbers which add up to zero, the question is to clean up the array by 
> deleting all annihilating pairs such that no such pairs are left.
> I do have a solution that is both elegant and efficient (I believe), but I am 
> curious about other thoughts.
>
>    foo  2 1 1 _1 2 _2 _1
> 2
>
>    foo 1 _1 2 _2{~100?.@#4              NB. Notice (?.)
> 2 1 2 1 _2 _1 2 1 1 2 2 2 _1 2 _1 2 1 2 2 2 1 1 _2 _1 2 1 _2 _1 _2 _2 _2 _2 
> _1 _2 _1 _1 _2 _2 1 1 1 2 1 _2 1 _2 _1 _1 _1 2
>
>
> R.E. Boss
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to