Let's examine an example with the array [2, 0, 1, 0, 2, 1, 0] . The longest subarrays consisting of consecutive elements are [2, 0, 1] , [0, 2, 1] , or [0, 1, 2] . The Python code to tackle this issue may be structured as follows: def find_max_subarray ( array ) : length = len ( array ) max_len_so_far = - 1 for i in range ( length ) : min_value = array [ i ] max_value = array [ i ] elements = { array [ i ] } for j in range ( i + 1 , length ) : if array [ j ] in elements : break elements . add ( array [ j ] ) min_value = min ( min_value , array [ j ] ) max_value = max ( max_value , array [ j ] ) if max_value - min_value == j - i : max_len_so_far = max ( max_len_so_far , max_value - min_value + 1 ) return max_len_so_far