Optimize function that returns length of a subarray I did an exercise today on where the task is to write a function that returns the length of the longest subarray where the difference between the smallest and biggest value in the subarray is no more than 1. I had a solution that works but it's too slow to pass the tests that have big arrays. What is it about my solution that makes it very slow with huge arrays?
def longestSubarray(arr):
    longest = 0
    arr_len = len(arr)
    for i in range(arr_len):
        for j in range(i, arr_len):
            sub = arr[i: j+1]
            if max(sub) - min(sub) &gt; 1:
                continue
            length = len(sub)
            if length &gt; longest:
                longest = length
    return longest
Specific suggestions:

In many situations it would probably be useful to return sub rather than len(sub). This makes the code somewhat simpler and gives the caller more information. Depending on the context that may or may not be useful, but it simplifies the code, which makes it easier to focus on the core issue.
The code iterates over all sub-lists, calculating min and max of each of them. This is a lot of duplicate work. You can instead iterate only once over the list (with some backtracking):

Set the longest sub-list to the empty list.
Set the working sub-list to a list containing just the first item in the input list.
Set working sub-list min and max to the first item in the working sub-list.
Move along the input list, updating min and max as long as the difference between them ends up being less than one and adding the current item to the working sub-list.
Once you encounter a number which makes the difference between min and max more than one (or the end of the list, at which point you'd terminate after this step), check if the working sub-list is longer than the longest sub-list and update the latter if so.
Once you encounter a number which makes the difference between min and max more than one you can't simply start the new working sub-list from that list item, because valid sub-lists may overlap (for example, [1, 1, 2, 3] has valid sub-lists [1, 1, 2] and [2, 3]). At this point you need to set the new working sub-list to include all the items at the tail end of the previous working sub-list which make a valid sub-list with the new item (if any).
Go to 3 until you reach the end of the list.



Tool support

black can automatically format your code to be more idiomatic, for example adding spaces around infix mathematical operators like +.

flake8 can give you more hints to write idiomatic Python beyond black:
 [flake8]
 ignore = W503,E203


I would then recommend validating your type hints using a strict mypy configuration:
 [mypy]
 check_untyped_defs = true
 disallow_any_generics = true
 disallow_untyped_defs = true
 ignore_missing_imports = true
 no_implicit_optional = true
 warn_redundant_casts = true
 warn_return_any = true
 warn_unused_ignores = true

In your case it looks like the type of arr should be Iterable[int] and the output is int.