```python
def max_len_sub(arr, n):
    if n == 0:
        return 0

    # A dictionary to store the length of the longest subsequence
    # ending with a specific number.
    # Key: number from the array
    # Value: length of the longest subsequence ending with that number
    lengths_map = {}
    
    # The overall maximum length found so far.
    max_len = 0

    for num in arr:
        # Find the length of the longest subsequence ending with a number
        # that is one less than the current number.
        prev_len = lengths_map.get(num - 1, 0)
        
        # Find the length of the longest subsequence ending with a number
        # that is one more than the current number.
        next_len = lengths_map.get(num + 1, 0)

        # The length of the subsequence ending with the current number is 1 plus
        # the maximum of the lengths of subsequences we can extend.
        current_len = 1 + max(prev_len, next_len)

        # Update the map with the new longest subsequence length for the current number.
        lengths_map[num] = current_len

        # Update the overall maximum length.
        max_len = max(max_len, current_len)
            
    return max_len
```