<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A subarray <code>s</code> of length <code>m</code> is called <strong>alternating</strong> if:</p>

<ul>
	<li><code>m</code> is greater than <code>1</code>.</li>
	<li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>.</li>
	<li>The <strong>0-indexed</strong> subarray <code>s</code> looks like <code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>. In other words, <code>s<sub>1</sub> - s<sub>0</sub> = 1</code>, <code>s<sub>2</sub> - s<sub>1</sub> = -1</code>, <code>s<sub>3</sub> - s<sub>2</sub> = 1</code>, <code>s<sub>4</sub> - s<sub>3</sub> = -1</code>, and so on up to <code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>.</li>
</ul>

<p>Return <em>the maximum length of all <strong>alternating</strong> subarrays present in </em><code>nums</code> <em>or </em><code>-1</code><em> if no such subarray exists</em><em>.</em></p>

<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,4,3,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,5,6]
<strong>Output:</strong> 2
<strong>Explanation:</strong> [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 100</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

from typing import List

class Solution:
    def alternatingSubarray(self, nums: List[int]) -> int:
        """
        Plan:
        1. Initialize variables to keep track of the maximum length of alternating subarray and the current length.
        2. Iterate through the array and check if the current element and the next element form an alternating pattern.
        3. If they do, increment the current length.
        4. If they don't, reset the current length.
        5. Update the maximum length if the current length is greater.
        6. Return the maximum length if it is greater than 1, otherwise return -1.
        """
        
        max_length = 0
        current_length = 1
        
        for i in range(1, len(nums)):
            if (nums[i] - nums[i - 1] == 1 and current_length % 2 == 1) or (nums[i - 1] - nums[i] == 1 and current_length % 2 == 0):
                current_length += 1
            else:
                current_length = 2 if nums[i] - nums[i - 1] == 1 else 1
            
            max_length = max(max_length, current_length)
        
        return max_length if max_length > 1 else -1

# Test cases
sol = Solution()
print(sol.alternatingSubarray([2, 3, 4, 3, 4]))  # Output: 4
print(sol.alternatingSubarray([4, 5, 6]))  # Output: 2
print(sol.alternatingSubarray([1, 2, 1, 2, 1]))  # Output: 5
print(sol.alternatingSubarray([1, 3, 5, 7]))  # Output: -1
87