today Question
Count increasing Subarrays
Difficulty: Easy
Given an array arr[] of integers, count the number of subarrays in arr[] which are strictly increasing with size greater or equal to 2. A subarray is a contiguous part of array. A subarray is strictly increasing if each element is greater then it's previous element if it exists.
Intuition:
Think of this problem like a combo counter in a video game:
- As long as the numbers keep going up (strictly increasing), your "combo" grows.
- Every time you add a new number to an existing increasing streak, you aren't just forming one new subarray; you are forming several.
- Example: If you have [1, 4] and you add 5 to get [1, 4, 5]:
- The new element 5 creates two new valid subarrays ending at its position: [4, 5] and [1, 4, 5].
- The number of new subarrays created is exactly equal to the number of elements that came before it in that specific streak.
Approach:
- Initialize:
- count = 0: To store the total number of subarrays found.
- streak = 0: To keep track of how many elements are currently in an increasing sequence before the current index.
- Loop: Start from the second element (index 1) and go to the end of the array.
- Check Condition: Compare the current element arr[i] with the previous element arr[i-1].
- If arr[i] > arr[i-1] (Increasing):
- Increment the streak.
- Add the streak value to your count.
- If arr[i] <= arr[i-1] (Streak Broken):
- Reset the streak to 0 because the next subarray must start fresh.
- Return: The final count.
Solve :

Flow Trace
Input: arr = [1, 4, 5, 3, 7]