2760. Longest Even Odd Subarray With Threshold

Difficulty:
Related Topics:
Similar Questions:

    Problem

    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:

    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:

    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:

    Solution (Java)

    class Solution {
        public int longestAlternatingSubarray(int[] nums, int th) {
            int n = nums.length,maxi = 0,i = 0,j = 0,flag = 0;
            while(j < n){
                if(flag == 0){
                   if(nums[j] % 2 == 0 && nums[j] <= th){
                        i = j;
                        maxi = Math.max(maxi,j - i + 1);
                        flag = 1;
                    }
                }
                else if(flag == 1){
                    int x = nums[j-1],y = nums[j],c = x + y;
                    if(c % 2 != 0 && nums[j] <= th)
                        maxi = Math.max(maxi,j - i + 1);
                    else{
                        flag = 0;
                        j--;
                    }
                }
                j++;
            }
            return maxi;
        }
    }
    

    Explain:

    nope.

    Complexity: