Let A be an 'almost' non-decreasing array of non-negative integers, which is
(by definition) a concatenation of non-decreasing sub arrays.
E.g.
A=. 0 1 1 2 3 3 3 4 4 5 , 2 2 3 4 4 4 5 5 , 3 4 5 6 , 4
The comma's are superfluous but identify the non-decreasing sub arrays.
Let B be an array of real numbers, with the same length as A.
[B=. 20 ?.@#~ #A
14 16 8 6 5 8 6 16 16 19 13 12 3 1 9 12 17 0 9 5 17 7 9
Required:
array C, by taking array B and add each element of B, corresponding with
element x in A, to all the next elements of B which correspond to elements y
in A with x<y and such that for all z in A placed between x and y, you have
x<z.
I do have a solution, but it is essentially rank 0.
Can anyone come up with an elegant solution of larger rank?
The answer for the example above is
A , B ,: C
0 1 1 2 3 3 3 4 4 5 2 2 3 4 4 4 5 5 3 4 5 6 4
14 16 8 6 5 8 6 16 16 19 13 12 3 1 9 12 17 0 9 5 17 7 9
14 30 22 28 33 36 34 50 50 69 35 34 37 38 46 49 66 49 43 48 65 72 52
R.E. Boss
Spoiler alert
C=.([: +/ B {~ ] i: i.@>:@{:)\ A
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm