Problem statement

https://binarysearch.com/problems/Longest-Consecutive-Sublist/

Solution

Starting from each place i keep minumum, maximum and sum of values in window. Then we can check in O(1) for each window if it is good or not.

Complexity

It is O(n^2) for time and O(n) for space.

Code

class Solution:
    def solve(self, nums):
        if not nums: return 0
        n, ans = len(nums), 1
        for i in range(n):
            mn = mx = sm = nums[i]
            for j in range(i + 1, n):
                mn = min(mn, nums[j])
                mx = max(mx, nums[j])
                sm += nums[j]
                if (mx + mn)*(mx - mn + 1) == 2*sm:
                    ans = max(ans, j - i + 1)
        return ans