• + 0 comments

    2 line Python solution, 100%

    Could be even 1, but split into 2 for readability :)

    segs = [len(list(j)) for i,j in groupby(a, lambda x:x>k) if not i]
    return math.comb(len(a)+1,2) - sum(math.comb(i+1,2) for i in segs)
    

    Notes:

    • answer = count of all subarrays - count of subarrays where all numbers <=k.
    • count of all subarrays is just N+1 choose 2
    • segs (funny name ikr) array is the lengths of all subarrays where elements are <=k.