Team Ai
Datasetpublic

FPEvalRepoPublic/LeetCodeMetaData

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes399downloads
longest-alternating-subarray.json32 linesDownload Raw Back to root
1{2  "id": 2870,3  "name": "longest-alternating-subarray",4  "difficulty": "Easy",5  "link": "https://leetcode.com/problems/longest-alternating-subarray/",6  "date": "2023-06-24",7  "task_description": "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 `[2, 3]`, `[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`",8  "test_case": [9    {10      "label": "Example 1",11      "input": "nums = [2,3,4,3,4]",12      "output": "4 "13    },14    {15      "label": "Example 2",16      "input": "nums = [4,5,6]",17      "output": "2 "18    }19  ],20  "constraints": [21    "m is greater than 1.",22    "s1 = s0 + 1.",23    "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.",24    "2 <= nums.length <= 100",25    "1 <= nums[i] <= 104"26  ],27  "python_template": "class Solution(object):\n    def alternatingSubarray(self, nums):\n        \"\"\"\n        :type nums: List[int]\n        :rtype: int\n        \"\"\"\n        ",28  "java_template": "class Solution {\n    public int alternatingSubarray(int[] nums) {\n        \n    }\n}",29  "metadata": {30    "func_name": "alternatingSubarray"31  }32}