• + 2 comments

    This is adding from idx to idx + m. It's more efficient to keep a running sum and subtract the beginning of the window and add the next value:

    def solve(n, s, d, m):
        if m > n:
            return 0
        
        i = j = 0
        window_sum = 0
        
        while j < m:
            window_sum += s[j]
            j += 1
            
        count = 1 if window_sum == d else 0
        
        while j < n:
            window_sum -= s[i]
            window_sum += s[j]
            
            if window_sum == d:
                count += 1
            i += 1
            j += 1
            
        return count