2760. Longest Even Odd Subarray With Threshold LeetCode Solution

In this guide, you will get 2760. Longest Even Odd Subarray With Threshold LeetCode Solution with the best time and space complexity. The solution to Longest Even Odd Subarray With Threshold 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 Even Odd Subarray With Threshold solution in C++
  4. Longest Even Odd Subarray With Threshold solution in Java
  5. Longest Even Odd Subarray With Threshold solution in Python
  6. Additional Resources
2760. Longest Even Odd Subarray With Threshold LeetCode Solution image

Problem Statement of Longest Even Odd Subarray With Threshold

You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions:

nums[l] % 2 == 0
For all indices i in the range [l, r – 1], nums[i] % 2 != nums[i + 1] % 2
For all indices i in the range [l, r], nums[i] <= threshold

Return an integer denoting the length of the longest such subarray.
Note: A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [3,2,5,4], threshold = 5
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Example 2:

See also  2114. Maximum Number of Words Found in Sentences LeetCode Solution

Input: nums = [1,2], threshold = 2
Output: 1
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.

Example 3:

Input: nums = [2,3,4,5], threshold = 4
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
It satisfies all the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.

Constraints:

1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= threshold <= 100

Complexity Analysis

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

2760. Longest Even Odd Subarray With Threshold LeetCode Solution in C++

class Solution {
 public:
  int longestAlternatingSubarray(vector<int>& nums, int threshold) {
    int ans = 0;
    int dp = 0;

    for (int i = 0; i < nums.size(); ++i) {
      if (nums[i] > threshold)
        dp = 0;
      else if (i > 0 && dp > 0 && isOddEven(nums[i - 1], nums[i]))
        // Increase the size of the subarray.
        ++dp;
      else
        // Start a new subarray if the start is valid.
        dp = nums[i] % 2 == 0 ? 1 : 0;
      ans = max(ans, dp);
    }

    return ans;
  }

 private:
  bool isOddEven(int a, int b) {
    return a % 2 != b % 2;
  }
};
/* code provided by PROGIEZ */

2760. Longest Even Odd Subarray With Threshold LeetCode Solution in Java

class Solution {
  public int longestAlternatingSubarray(int[] nums, int threshold) {
    int ans = 0;
    int dp = 0;

    for (int i = 0; i < nums.length; ++i) {
      if (nums[i] > threshold)
        dp = 0;
      else if (i > 0 && dp > 0 && isOddEven(nums[i - 1], nums[i]))
        // Increase the size of the subarray.
        ++dp;
      else
        // Start a new subarray if the start is valid.
        dp = nums[i] % 2 == 0 ? 1 : 0;
      ans = Math.max(ans, dp);
    }

    return ans;
  }

  private boolean isOddEven(int a, int b) {
    return a % 2 != b % 2;
  }
}
// code provided by PROGIEZ

2760. Longest Even Odd Subarray With Threshold LeetCode Solution in Python

class Solution:
  def longestAlternatingSubarray(self, nums: list[int], threshold: int) -> int:
    ans = 0
    dp = 0

    def isOddEven(a: int, b: int) -> bool:
      return a % 2 != b % 2

    for i, num in enumerate(nums):
      if num > threshold:
        dp = 0
      elif i > 0 and dp > 0 and isOddEven(nums[i - 1], num):
        # Increase the size of the subarray.
        dp += 1
      else:
        # Start a new subarray if the start is valid.
        dp = 1 if num % 2 == 0 else 0
      ans = max(ans, dp)

    return ans
# code by PROGIEZ

Additional Resources

See also  312. Burst Balloons LeetCode Solution

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