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: 9
💡 Note: Strictly increasing subarrays: [1], [3], [2], [4], [5], [1,3], [2,4], [4,5], [2,4,5] = 9 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 Subarrays INPUT nums = [1, 3, 2, 4, 5] 1 i=0 3 i=1 2 i=2 4 i=3 5 i=4 Increasing Break (reset) Continue streak All Increasing Subarrays: [1],[3],[2],[4],[5],[1,3],[2,4],[4,5] [2,4,5] ALGORITHM STEPS 1 Initialize count=0, len=1 2 Iterate array For each element i 3 Check condition If nums[i] > nums[i-1] 4 Update counters len++ else len=1 count += len Tracking Progress: i val len count 0 1 1 1 1 3>1 2 3 2 2<3 1 4 3 4>2 2 6 4 5>4 3 8 FINAL RESULT Output: 8 OK Subarray Breakdown: Single elements: 5 [1],[3],[2],[4],[5] Length 2: 2 [1,3],[2,4],[4,5] Length 3: 1 [2,4,5] Total = 5 + 2 + 1 = 8 Key Insight: When we have a strictly increasing sequence of length L, it contributes L new subarrays (ending at current position). By tracking the current streak length and adding it to count at each step, we achieve O(n) time complexity with O(1) space. Reset length to 1 on breaks. TutorialsPoint - Count Strictly Increasing Subarrays | Single Pass with Length Tracking
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