Skip to content

Practice Longest Alternating Subarray

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2765
LeetCode Problem

Longest Alternating Subarray

Problem Statement

Find a contiguous subarray of length at least two whose consecutive differences are +1, -1, +1, -1, and so on, starting with +1. Return the maximum such length, or -1 if none exists.

Source: Longest Alternating Subarray on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,3,4,3,4]
Output
4

# Example 2

Input
nums = [4,5,6]
Output
2

Constraints

2 <= nums.length <= 100
1 <= nums[i] <= 10^4

Related LeetCode Problems

Practice more problems similar to Longest Alternating Subarray.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.