Algorithms
Easy
2765
2870
Longest Alternating Subarray
longest-alternating-subarray
"""
You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if:

 * m is greater than 1.
 * s1 = s0 + 1.
 * The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m.

Return the maximum length of all alternating subarrays present in nums or -1 if no such subarray exists.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [2,3,4,3,4]
Output: 4
Explanation: 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.

Example 2:

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


Constraints:

 * 2 <= nums.length <= 100
 * 1 <= nums[i] <= 104
"""
class Solution:
    def alternatingSubarray(self, nums: List[int]) -> int:
        
You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if:

 * m is greater than 1.
 * s1 = s0 + 1.
 * The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m.

Return the maximum length of all alternating subarrays present in nums or -1 if no such subarray exists.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [2,3,4,3,4]
Output: 4
Explanation: 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.

Example 2:

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


Constraints:

 * 2 <= nums.length <= 100
 * 1 <= nums[i] <= 104


Please complete the code below to solve above prblem:
```python
class Solution:
    def alternatingSubarray(self, nums: List[int]) -> int:
        
```

my_solution = Solution()

test_input = { "nums": [2,3,4,3,4] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [4,5,6] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [31,32,31,32,33] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [21,9,5] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [42,43,44,43,44,43,44,45,46] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [13,14,15,14] }
assert my_solution.alternatingSubarray(**test_input) == 3

test_input = { "nums": [74,75,74,75,74,75,74,75] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [77,78,79,78,79,78,79,78,79,80] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [88,42,53] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [64,65,64,65,64,65,66,65,66,65] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [99,100,99,100] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [21,22] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [23,24,23,24,25,24,25,24,25] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [20,9,15,15] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [92,93,92,93,92] }
assert my_solution.alternatingSubarray(**test_input) == 5

test_input = { "nums": [24,25,26] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [51,52,53,52,53,52,53,54,53] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [65,66,65,66,67,68,69] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [29,2,5,24] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [26,27,26,27,28,27,28,27,28] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [21,22,21,22,21,22] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [94,95,94,95,94] }
assert my_solution.alternatingSubarray(**test_input) == 5

test_input = { "nums": [82,83,84,83,84,83,84,83] }
assert my_solution.alternatingSubarray(**test_input) == 7

test_input = { "nums": [14,30,29,49,3,23,44,21,26,52] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [4,5,4,5,6,5,6] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [62,63] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [24,25,26,25,26,25,26,25,26] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [55,56,55,56,55,56,55,56,57,56] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [52,77,42,21] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [80,81] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [50,51,50,51,50,51,50,51,50] }
assert my_solution.alternatingSubarray(**test_input) == 9

test_input = { "nums": [83,84,83] }
assert my_solution.alternatingSubarray(**test_input) == 3

test_input = { "nums": [17,18,17,18,19,18,19,20,19,20] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [5,14,8,12,5,4] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [35,36,35,36,35,36,35,36,35,36] }
assert my_solution.alternatingSubarray(**test_input) == 10

test_input = { "nums": [8,9,8,9,8,9] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [59,60] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [55,56,55] }
assert my_solution.alternatingSubarray(**test_input) == 3

test_input = { "nums": [47,46,65,37,24,54,39,70] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [45,46,45,46,45,46,45] }
assert my_solution.alternatingSubarray(**test_input) == 7

test_input = { "nums": [78,79,78,79,78,79,78,79,80,79] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [65,66,65,66,65,66,67,68] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [62,63,62,63,62,63] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [7,10,5,2,11,3,9,12,9,11] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [79,80,79,80,79,80] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [19,20,21,20,21,22] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [84,85,86,85,86,85,86,87,88,87] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [54,55] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [71,14,24,13,21,14,18,84,37,2] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [26,27,26] }
assert my_solution.alternatingSubarray(**test_input) == 3

test_input = { "nums": [53,54,53,54,53] }
assert my_solution.alternatingSubarray(**test_input) == 5

test_input = { "nums": [67,68,67,68,67,68,69,70,69,70] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [85,86,85,86,85,86,85,86,85,86] }
assert my_solution.alternatingSubarray(**test_input) == 10

test_input = { "nums": [22,16,27,22,44,10] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [27,28,27,28,27,28,27,28,29,28] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [54,55,54,55,54,55,54,55,56,57] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [24,25,26,27,28] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [55,56] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [13,6,6,8,12,7,1] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [40,41,40,41,40,41,40,41] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [10,11,10,11,10,11,12] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [58,59,58,59] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [1,15,44,74,56,41,48,71] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [89,90,89,90,89,90] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [4,5,4,5,4,5,6] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [50,51,52,53,52,53] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [44,45,46,45,46,45,46,47,48] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [16,3,25,12,2,19,1,26] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [17,18,19,20,19,20] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [91,92,93,92,93] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [28,29,28,29,28,29,30] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [88,89,88,89,88,89] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [1,1,1,1,1,1,1,1,1,1] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [32,33,32,33,32,33,34] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [61,62,61,62,63,62,63,64,63] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [6,7,6,7] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [20,21,20,21,20] }
assert my_solution.alternatingSubarray(**test_input) == 5

test_input = { "nums": [14,6,21] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [12,13,12,13,12,13,12,13,12] }
assert my_solution.alternatingSubarray(**test_input) == 9

test_input = { "nums": [33,34,33,34,33,34,33] }
assert my_solution.alternatingSubarray(**test_input) == 7

test_input = { "nums": [92,93,92] }
assert my_solution.alternatingSubarray(**test_input) == 3

test_input = { "nums": [93,94,95,94,95,96,95,96,97] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [8,4,27] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [30,31,32,31,32,33,34,33] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [26,27,26,27,26,27] }
assert my_solution.alternatingSubarray(**test_input) == 6

test_input = { "nums": [67,68,69] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [83,84,85,84,85] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [6,26,4,2] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [81,82,81,82,81,82,81,82,83] }
assert my_solution.alternatingSubarray(**test_input) == 8

test_input = { "nums": [58,59] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [82,83,82,83,82,83,82,83,82] }
assert my_solution.alternatingSubarray(**test_input) == 9

test_input = { "nums": [48,49] }
assert my_solution.alternatingSubarray(**test_input) == 2

test_input = { "nums": [8,6,2] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [69,70,69,70,69,70,69,70,69] }
assert my_solution.alternatingSubarray(**test_input) == 9

test_input = { "nums": [28,29,28,29] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [82,83,82,83,84] }
assert my_solution.alternatingSubarray(**test_input) == 4

test_input = { "nums": [97,98,97,98,97,98,97] }
assert my_solution.alternatingSubarray(**test_input) == 7

test_input = { "nums": [2,2,1] }
assert my_solution.alternatingSubarray(**test_input) == -1

test_input = { "nums": [84,85,84,85,84,85,84] }
assert my_solution.alternatingSubarray(**test_input) == 7

test_input = { "nums": [21,22,21,22] }
assert my_solution.alternatingSubarray(**test_input) == 4