2765. Longest Alternating Subarray LeetCode Solution

In this guide, you will get 2765. Longest Alternating Subarray LeetCode Solution with the best time and space complexity. The solution to Longest Alternating Subarray problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Alternating Subarray solution in C++
  4. Longest Alternating Subarray solution in Java
  5. Longest Alternating Subarray solution in Python
  6. Additional Resources
2765. Longest Alternating Subarray LeetCode Solution image

Problem Statement of 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 [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

See also  637. Average of Levels in Binary Tree LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2765. Longest Alternating Subarray LeetCode Solution in C++

class Solution {
 public:
  int alternatingSubarray(vector<int>& nums) {
    int ans = 1;
    int dp = 1;

    for (int i = 1; i < nums.size(); ++i) {
      const int targetDiff = dp % 2 == 0 ? -1 : 1;
      // Append nums[i] to the current alternating subarray.
      if (nums[i] - nums[i - 1] == targetDiff)
        ++dp;
      // Reset the alternating subarray to nums[i - 1..i].
      else if (nums[i] - nums[i - 1] == 1)
        dp = 2;
      // Reset the alternating subarray to nums[i].
      else
        dp = 1;
      ans = max(ans, dp);
    }

    return ans == 1 ? -1 : ans;
  }
};
/* code provided by PROGIEZ */

2765. Longest Alternating Subarray LeetCode Solution in Java

class Solution {
  public int alternatingSubarray(int[] nums) {
    int ans = 1;
    int dp = 1;

    for (int i = 1; i < nums.length; ++i) {
      final int targetDiff = dp % 2 == 0 ? -1 : 1;
      // Append nums[i] to the current alternating subarray.
      if (nums[i] - nums[i - 1] == targetDiff)
        ++dp;
      // Reset the alternating subarray to nums[i - 1..i].
      else if (nums[i] - nums[i - 1] == 1)
        dp = 2;
      // Reset the alternating subarray to nums[i].
      else
        dp = 1;
      ans = Math.max(ans, dp);
    }

    return ans == 1 ? -1 : ans;
  }
}
// code provided by PROGIEZ

2765. Longest Alternating Subarray LeetCode Solution in Python

class Solution:
  def alternatingSubarray(self, nums: list[int]) -> int:
    ans = 1
    dp = 1

    for i in range(1, len(nums)):
      targetDiff = -1 if dp % 2 == 0 else 1
      # Append nums[i] to the current alternating subarray.
      if nums[i] - nums[i - 1] == targetDiff:
        dp += 1
      # Reset the alternating subarray to nums[i - 1..i].
      elif nums[i] - nums[i - 1] == 1:
        dp = 2
      # Reset the alternating subarray to nums[i].
      else:
        dp = 1
      ans = max(ans, dp)

    return -1 if ans == 1 else ans
# code by PROGIEZ

Additional Resources

See also  393. UTF-8 Validation LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.