Find the maximum j – i such that arr[j] > arr[i] (Distance maximising problem)
Output: 6 (j = 7, i = 1)
q)list: 34 8 10 3 2 80 30 33 1
q)func:{{first[y _ x],/: (y+1) _ x}[x] each y}
q)func[list;til count list]
(34 8;34 10;34 3;34 2;34 80;34 30;34 33;34 1)
(8 10;8 3;8 2;8 80;8 30;8 33;8 1)
(10 3;10 2;10 80;10 30;10 33;10 1)
(3 2;3 80;3 30;3 33;3 1)
(2 80;2 30;2 33;2 1)
(80 30;80 33;80 1)
(30 33;30 1)
,33 1
()
q)asc r where {(x 1)>(x 0)} each r:raze func[list;til count list]
2 30
2 33
2 80
3 30
3 33
3 80
8 10
8 30
8 33
8 80
10 30
10 33
10 80
30 33
34 80
I am struck here to find the final result, please suggest on this.