Problem Description
Given an array of positive integers, count the number of contiguous subarrays that are strictly increasing. A subarray is a contiguous portion of the array, and every single element subarray is considered strictly increasing.
Key Insights
- Every single element subarray is strictly increasing by default.
- A subarray of length > 1 is strictly increasing if each element is greater than its previous element.
- Instead of enumerating every subarray, we can count the number of strictly increasing subarrays ending at each index.
- By maintaining the length of the current increasing sequence, we can add the current sequence length (which represents the number of new subarrays ending at the current index) to a total counter.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the array, since we do a single pass. Space Complexity: O(1) as we only use a few extra variables.
Solution
We iterate over the array while tracking the length of the current strictly increasing sequence. For each index, if the current element is greater than the previous one, we increment our counter for the sequence; otherwise, we reset it to 1. The current sequence length indicates how many new strictly increasing subarrays end at the current index. Summing these counts gives the final answer.