Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 3105 | Longest Strictly Increasing/Decreasing Subarray | Java | Easy | Array | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 3105 | Longest Strictly Increasing/Decreasing Subarray | Java | Easy | Array | CodeByTushu"

Related videos are temporarily unavailable.

Try again, or come back later.