Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an array of integers nums. Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing.
Example 1:
Input: nums = [1,4,3,3,2]
Output: 2
Explanation:
The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].
The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].
Hence, we return 2.
Example 2:
Input: nums = [3,3,3,3]
Output: 1
Explanation:
The strictly increasing subarrays of nums are [3], [3], [3], and [3].
The strictly decreasing subarrays of nums are [3], [3], [3], and [3].
Hence, we return 1.
Example 3:
Input: nums = [3,2,1]
Output: 3
Explanation:
The strictly increasing subarrays of nums are [3], [2], and [1].
The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].
Hence, we return 3.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50Problem summary: You are given an array of integers nums. Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,4,3,3,2]
[3,3,3,3]
[3,2,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3105: Longest Strictly Increasing or Strictly Decreasing Subarray
class Solution {
public int longestMonotonicSubarray(int[] nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3105: Longest Strictly Increasing or Strictly Decreasing Subarray
func longestMonotonicSubarray(nums []int) int {
ans := 1
t := 1
for i, x := range nums[1:] {
if nums[i] < x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
t = 1
for i, x := range nums[1:] {
if nums[i] > x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
return ans
}
# Accepted solution for LeetCode #3105: Longest Strictly Increasing or Strictly Decreasing Subarray
class Solution:
def longestMonotonicSubarray(self, nums: List[int]) -> int:
ans = t = 1
for i, x in enumerate(nums[1:]):
if nums[i] < x:
t += 1
ans = max(ans, t)
else:
t = 1
t = 1
for i, x in enumerate(nums[1:]):
if nums[i] > x:
t += 1
ans = max(ans, t)
else:
t = 1
return ans
// Accepted solution for LeetCode #3105: Longest Strictly Increasing or Strictly Decreasing Subarray
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3105: Longest Strictly Increasing or Strictly Decreasing Subarray
// class Solution {
// public int longestMonotonicSubarray(int[] nums) {
// int ans = 1;
// for (int i = 1, t = 1; i < nums.length; ++i) {
// if (nums[i - 1] < nums[i]) {
// ans = Math.max(ans, ++t);
// } else {
// t = 1;
// }
// }
// for (int i = 1, t = 1; i < nums.length; ++i) {
// if (nums[i - 1] > nums[i]) {
// ans = Math.max(ans, ++t);
// } else {
// t = 1;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3105: Longest Strictly Increasing or Strictly Decreasing Subarray
function longestMonotonicSubarray(nums: number[]): number {
const n = nums.length;
let ans = 1;
for (let i = 1, t1 = 1, t2 = 1; i < n; i++) {
t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1;
t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1;
ans = Math.max(ans, t1, t2);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.