Leetcode Problem 2760. Longest Even Odd Subarray With Threshold

2760. Longest Even Odd Subarray With Threshold

Leetcode Solutions

Dynamic Programming Approach

Algorithm:

  1. Initialize max_length and current_length to 0.
  2. Iterate through the array using an index i.
  3. If nums[i] is even and nums[i] <= threshold, check if it can start or extend a subarray:
    • If i is 0 or nums[i-1] is odd, set current_length to 1.
    • Otherwise, increment current_length.
  4. If nums[i] is odd and nums[i] <= threshold, check if it can extend a subarray:
    • If nums[i-1] is even, increment current_length.
    • Otherwise, reset current_length.
  5. Update max_length with the maximum of max_length and current_length.
  6. If nums[i] > threshold, reset current_length.
  7. Return max_length as the result.
UML Thumbnail

Two-Pointer Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...