Tap here to switch tabs
Problem
Submissions

Problem: Longest Alternating Subarray

med
30 min
Try to solve the Longest Alternating Subarray problem.

Statement

You are given a 00 indexed integer array nums. A subarray of nums of length m (where m>1m > 1) is considered alternating if:

  • nums[i + 1] - nums[i] = 1 for all even indices i where 0≤i<m−10 \leq i < m - 1

  • nums[i + 1] - nums[i] = -1 for all odd indices i where 0≤i<m−10 \leq i < m - 1

Return the maximum length of any alternating subarray in nums. If no alternating subarray exists, return −1-1.

Constraints:

  • 2≤2 \leq nums.length ≤100\leq 100

  • 1≤1 \leq nums[i] ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Longest Alternating Subarray

med
30 min
Try to solve the Longest Alternating Subarray problem.

Statement

You are given a 00 indexed integer array nums. A subarray of nums of length m (where m>1m > 1) is considered alternating if:

  • nums[i + 1] - nums[i] = 1 for all even indices i where 0≤i<m−10 \leq i < m - 1

  • nums[i + 1] - nums[i] = -1 for all odd indices i where 0≤i<m−10 \leq i < m - 1

Return the maximum length of any alternating subarray in nums. If no alternating subarray exists, return −1-1.

Constraints:

  • 2≤2 \leq nums.length ≤100\leq 100

  • 1≤1 \leq nums[i] ≤104\leq 10^4