Maximum Sum of Lengths of Non-Overlapping Subarrays with k as Max Element.

Find the maximum sum of lengths of non-overlapping subarrays (contiguous elements) with k as the maximum element of each subarray.

Examples:

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.

Contributed by Berkan Teber