Maximum length of Strictly Increasing or Strictly Decreasing Subarray — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Maximum length of Strictly Increasing or Strictly Decreasing Subarray
Detailed explanation and reference materials
Problem Overview

Problem: Maximum length of Strictly Increasing or Strictly Decreasing Subarray

Problem Statement

You are given an array of integers nums. Your task is to find the length of the longest strictly increasing or strictly decreasing contiguous subarray.

Constraints

  • A subarray is a contiguous part of the array.
  • A strictly increasing subarray means each element is greater than the previous one.
  • A strictly decreasing subarray means each element is smaller than the previous one.
  • The array may contain duplicate values, which break the strict order.

Example 1

Input:

plaintext
nums = [2, 5, 7, 6, 4]

Output:

plaintext
3

Explanation:

  • Strictly increasing subarrays: [2], [5], [7], [6], [4], [2,5], [5,7], [2,5,7]
  • Strictly decreasing subarrays: [2], [5], [7], [6], [4], [7,6], [6,4], [7,6,4]
  • Longest valid subarray: [7,6,4] (length = 3)

Example 2

Input:

plaintext
nums = [8, 8, 8, 8]

Output:

plaintext
1

Explanation:

  • No valid increasing or decreasing sequence of length > 1.
  • The longest valid subarray is just any single element (length = 1).

Example 3

Input:

plaintext
nums = [9, 7, 5, 3, 1]

Output:

plaintext
5

Explanation:

  • Strictly decreasing subarrays: [9], [7], [5], [3], [1], [9,7], [7,5], [5,3], [3,1], [9,7,5], [7,5,3], [5,3,1], [9,7,5,3], [7,5,3,1], [9,7,5,3,1]
  • Longest valid subarray: [9,7,5,3,1] (length = 5)

Additional Test Cases

Test Case 4

Input:
plaintext
nums = [12, 14, 16, 13, 11, 9]
Output:
plaintext
4
Explanation:
  • Strictly increasing subarrays: [12], [14], [16], [13], [11], [9], [12,14], [14,16], [12,14,16]
  • Strictly decreasing subarrays: [12], [14], [16], [13], [11], [9], [16,13], [13,11], [11,9], [16,13,11], [13,11,9], [16,13,11,9]
  • Longest valid subarray: [16,13,11,9] (length = 4)

Test Case 5

Input:
plaintext
nums = [1, 3, 5, 7, 9]
Output:
plaintext
5
Explanation:
  • Strictly increasing subarrays: [1], [3], [5], [7], [9], [1,3], [3,5], [5,7], [7,9], [1,3,5], [3,5,7], [5,7,9], [1,3,5,7], [3,5,7,9], [1,3,5,7,9]
  • No decreasing subarrays exist.
  • Longest valid subarray: [1,3,5,7,9] (length = 5)
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/description/?envType=daily-question&envId=2025-02-03

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.