Count Strictly Increasing Subarrays - Problem

You are given an array nums consisting of positive integers. Return the number of subarrays of nums that are in strictly increasing order.

A subarray is a contiguous part of an array. A sequence is strictly increasing if each element is greater than the previous element.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,3,2,4,5]
Output: 8
💡 Note: Strictly increasing subarrays: [1], [3], [2], [4], [5], [1,3], [2,4], [4,5], [2,4,5] = 8 total
Example 2 — All Increasing
$ Input: nums = [1,2,3,4]
Output: 10
💡 Note: All subarrays are increasing: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], [1,2,3,4] = 10 total
Example 3 — Single Element
$ Input: nums = [5]
Output: 1
💡 Note: Only one subarray [5], which is trivially increasing

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Count Strictly Increasing Subarrays13245Input: [1,3,2,4,5]IncreasingIncreasingValid subarrays:[1], [3], [1,3] from first sequence (3 subarrays)[2], [4], [5], [2,4], [4,5], [2,4,5] from second sequence (6 subarrays)Total: 8 strictly increasing subarraysEach element must be greater than the previous one
Understanding the Visualization
1
Input Array
Given array [1,3,2,4,5]
2
Find Sequences
Identify increasing sequences: [1,3] and [2,4,5]
3
Count Subarrays
Each sequence contributes multiple valid subarrays
Key Takeaway
🎯 Key Insight: Use the formula length×(length+1)/2 to count subarrays from each increasing sequence
Asked in
Google 25 Microsoft 18 Amazon 15
23.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen