Back to November Back to Year 2026
EASY 12 Nov 2026 View on LeetCode

3105. Longest Strictly Increasing or Strictly Decreasing Subarray

</> Solution

/**
 * Problem: LeetCode 3105 - Longest Strictly Increasing or Strictly Decreasing Subarray
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public int longestMonotonicSubarray(int[] nums) {
        int inc = 1, dec = 1, ans = 1;

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                inc++;
                dec = 1;
            } else if (nums[i] < nums[i - 1]) {
                dec++;
                inc = 1;
            } else {
                inc = 1;
                dec = 1;
            }

            ans = Math.max(ans, Math.max(inc, dec));
        }

        return ans;
    }
}

TIME COMPLEXITY

O(n)

SPACE COMPLEXITY

O(1)

TOPICS

Array Simulation String