<jupyter_start><jupyter_text>Length of largest contiguous arrayPython Data Structures ArraysGiven an array of distinct integers, write code to find length of the longest subarray which contains numbers that can be arranged in a continuous sequence.For example:```Input:  arr[] = {10, 12, 11};Output: 3Input:  arr[] = {14, 12, 11, 20};Output: 2Input:  arr[] = {1, 10, 23, 11, 13, 14, 15, 12, 98}Output: 5```<jupyter_code>def test_is_contiguous_array(f):
    assert f([]) == False
    assert f([1]) == True
    assert f([2,4,3]) == True
    assert f([1,3,4,2]) == True
    assert f([1,3,5,2,4]) == True
    assert f([4,2,1,8]) == False
    assert f([1,8,2,4,3]) == False
    print('test_is_contiguous_array passed')

    
def is_contiguous_array(arr):
    """
    Contiguous array: all its numbers can be arranged in a continuous sequence.
    Assume all numbers in array are distinct.
    Property of a contiguous array: len() = max - min + 1
    """
    if not arr:
        return False
    else:
        return len(arr) == max(arr) - min(arr) + 1


test_is_contiguous_array(is_contiguous_array)


def test_find_longest_contiguous_subarray(f):
    assert f([]) == []
    assert f([1]) == [1]
    assert f([2,4,3]) == [2,4,3]
    assert f([1,3,4,2]) == [1,3,4,2]
    assert f([1,3,5,2,4]) == [1,3,5,2,4]
    assert f([4,2,1,8]) == [2,1]
    assert f([1,8,2,4,3]) == [2,4,3]
    assert f([1,2,6,5]) == [1,2]
    print('test_find_longest_contiguous_subarray passed')
    
    
def find_longest_contiguous_subarray(arr):
    """
    Algorithm:
    - enumerate all subarrays: n2
    - find subarray len, min, and max: n
    Assume arr has unique integers, no duplicates. 
    
    [2,4,3]     -> [2,4,3]
    [1,3,4,2]   -> [1,3,4,2]
    [1,3,5,2,4] -> [1,3,5,2,4]
    [4,2,1,8]   -> [2,1]
    [1,8,2,4,3] -> [2,4,3]
    [1,2,6,5]   -> [1,2]         # return first one found
    """
    longest_subarr = [arr[0]] if arr else []
    for i in range(len(arr)):
        for j in range(i,len(arr)):
            subarray = arr[i:j+1] # subarray of at least length 2
            if is_contiguous_array(subarray):
                if len(subarray) > len(longest_subarr):
                    longest_subarr = subarray
    return longest_subarr


test_find_longest_contiguous_subarray(find_longest_contiguous_subarray)<jupyter_output>test_find_longest_contiguous_subarray passed
{'fraction_non_alphanumeric': 0.1592545531554426, 'fraction_numerical': 0.06437950021177467, 'mean_word_length': 3.1805309734513276, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '330407', 'license': 'Unlicense', 'n_tokens_mistral': 966, 'n_tokens_neox': 910, 'n_words': 218, 'path': 'interviewq_exercises/q138_python_length_largest_contiguous_array.ipynb', 'repo_name': 'gentimouton/dives', 'size': '2361'}