Find the maximum sum of lengths of non-overlapping subarrays (contiguous elements) with k
as the maximum element of each subarray.
arr = [2, 1, 4, 9, 2, 3, 8, 3, 4]
and k = 4
should return 5
since [2, 1, 4]
has a length of 3
and [3, 4]
has a length of 2
, total 5
.
arr = [1, 2, 3, 2, 3, 4, 1]
and k = 4
should return 7
since [1, 2, 3, 2, 3, 4, 1]
has a length of 7
.