LeetCode 3105 | Longest Strictly Increasing/Decreasing Subarray | Java | Easy | Array | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 3105 β Longest Strictly Increasing or Strictly Decreasing Subarray using Single Pass Array Traversal in Java.
In this problem, we are given an integer array nums and asked to return the maximum length of any contiguous subarray that is either strictly increasing or strictly decreasing. An increasing run requires every adjacent element to be strictly greater than the previous element, while a decreasing run requires every adjacent element to be strictly less than the previous element. Single-element subarrays always have a valid length of 1.
We implement an optimal single-pass linear scan that maintains two active streak counters, inc and dec, ending at the current index. On each step from index 1 to n - 1, if nums[i] is greater than nums[i - 1], we increment inc and reset dec to 1; if nums[i] is less than nums[i - 1], we increment dec and reset inc to 1; and when adjacent values are equal, we reset both counters to 1. Updating the global maximum at every iteration solves all cases in O(N) time and strictly O(1) constant auxiliary space.
π₯ Problem Highlights
β Single-pass dual streak tracking: monitors both increasing and decreasing runs simultaneously
β Strict monotonicity enforcement: resets both counters to 1 whenever adjacent elements are equal
β Seamless direction transitions: switching from increasing to decreasing retains length 1 at the pivot
β Edge case safety: initializing inc, dec, and ans to 1 handles single-element arrays cleanly
β Zero extra allocations: requires no dynamic arrays, prefix tables, or recursion stack
β Optimal complexity: O(N) linear time complexity and strictly O(1) constant auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro
00:32 Problem Understanding
01:51 Observation
02:56 Approach
04:08 Code Explanation
06:08 Code Run
07:13 Time Complexity
07:47 Space Complexity
08:12 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/longest-strictly-increasing-or-strictly-decreasing-subarray
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Array Single Pass Traversal
Monotonic Streak Tracking
Subarray Boundary Handling
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
In LeetCode 3105, what should happen to both inc and dec streak counters when nums[i] equals nums[i - 1]?
1οΈβ£ Both inc and dec are reset to 1 because equal adjacent elements break strict monotonicity
2οΈβ£ Only inc is incremented while dec stays unchanged
3οΈβ£ Both inc and dec are incremented by 1 together
4οΈβ£ Both inc and dec are set to 0
#LeetCode3105 #LeetCode #LongestMonotonicSubarray #Java #JavaDSA #Array #SinglePass #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 3105 β Longest Strictly Increasing or Strictly Decreasing Subarray using Single Pass Array Traversal in Java.
In this problem, we are given an integer array nums and asked to return the maximum length of any contiguous subarray that is either strictly increasing or strictly decreasing. An increasing run requires every adjacent element to be strictly greater than the previous element, while a decreasing run requires every adjacent element to be strictly less than the previous element. Single-element subarrays always have a valid length of 1.
We implement an optimal single-pass linear scan that maintains two active streak counters, inc and dec, ending at the current index. On each step from index 1 to n - 1, if nums[i] is greater than nums[i - 1], we increment inc and reset dec to 1; if nums[i] is less than nums[i - 1], we increment dec and reset inc to 1; and when adjacent values are equal, we reset both counters to 1. Updating the global maximum at every iteration solves all cases in O(N) time and strictly O(1) constant auxiliary space.
π₯ Problem Highlights
β Single-pass dual streak tracking: monitors both increasing and decreasing runs simultaneously
β Strict monotonicity enforcement: resets both counters to 1 whenever adjacent elements are equal
β Seamless direction transitions: switching from increasing to decreasing retains length 1 at the pivot
β Edge case safety: initializing inc, dec, and ans to 1 handles single-element arrays cleanly
β Zero extra allocations: requires no dynamic arrays, prefix tables, or recursion stack
β Optimal complexity: O(N) linear time complexity and strictly O(1) constant auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro
00:32 Problem Understanding
01:51 Observation
02:56 Approach
04:08 Code Explanation
06:08 Code Run
07:13 Time Complexity
07:47 Space Complexity
08:12 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/12-November/3105-Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/longest-strictly-increasing-or-strictly-decreasing-subarray
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Array Single Pass Traversal
Monotonic Streak Tracking
Subarray Boundary Handling
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
In LeetCode 3105, what should happen to both inc and dec streak counters when nums[i] equals nums[i - 1]?
1οΈβ£ Both inc and dec are reset to 1 because equal adjacent elements break strict monotonicity
2οΈβ£ Only inc is incremented while dec stays unchanged
3οΈβ£ Both inc and dec are incremented by 1 together
4οΈβ£ Both inc and dec are set to 0
#LeetCode3105 #LeetCode #LongestMonotonicSubarray #Java #JavaDSA #Array #SinglePass #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
