Problem
arr[]. Count the number of subarrays where the first element of the subarray is the minimum element of that subarray.Input | Output | Explanation |
[3,1,2] | 4 | Valid subarrays: (3), (1), (1,2), (2) |
[1,2,3] | 6 | All subarrays are valid because the first element is always the minimum |
Prerequisites
Minimum Stack / Minimum Queue - Algorithms for Competitive ProgrammingMinimum Stack / Minimum Queue - Algorithms for Competitive Programming
The goal of this project is to translate the wonderful resource http://e-maxx.ru/algo which provides descriptions of many algorithms and data structures especially popular in field of competitive programming. Moreover we want to improve the collected knowledge by extending the articles and adding new articles to the collection.
Next Smaller Element | Practice | GeeksforGeeks
Next Smaller Element | Practice | GeeksforGeeks
You are given an integer array arr[ ]. For every element in the array, your task is to determine its Next Smaller Element (NSE). The Next Smaller Element (NSE) of an element x is the first element that appears to the right of x in the array and
LeetCodeSum of Subarray Minimums - LeetCode

Sum of Subarray Minimums - LeetCode
Can you solve this real interview question? Sum of Subarray Minimums - Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7. Example 1: Input: arr = [3,1,2,4] Output: 17 Explanation: Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Sum is 17. Example 2: Input: arr = [11,81,94,43,3] Output: 444 Constraints: * 1 <= arr.length <= 3 * 104 * 1 <= arr[i] <= 3 * 104
LeetCodeLargest Rectangle in Histogram - LeetCode

Largest Rectangle in Histogram - LeetCode
Can you solve this real interview question? Largest Rectangle in Histogram - Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. Example 1: [https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg] Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. Example 2: [https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg] Input: heights = [2,4] Output: 4 Constraints: * 1 <= heights.length <= 105 * 0 <= heights[i] <= 104
Brute Force
Idea
- Generate every subarray
(i → j)
- Track the minimum value in that subarray
- If
arr[i] == minimum, count that subarray
Code
C++
Java
JavaScript
Python
Complexity
- Total subarrays =
O(N^2)
- Checking minimum =
O(N)
Metric | Value |
Time Complexity | O(N³) (or O(N²) with running minimum) |
Space Complexity | O(1) |
n ≤ 10^5.Optimal Approach
Hint
i:arr[i] must remain the minimum element.arr[i].Where is the next element smaller thanarr[i]?
Idea
Approach
- Traverse the array using a stack.
- Maintain indices of elements.
- When
arr[i] < arr[stack.top()], update the next smaller index.
- Sum all the contributions:
count = nextSmaller[i] - i
Code
C++
Java
JavaScript
Python
Complexity
Metric | Value |
Time Complexity | O(N) |
Space Complexity | O(N) |
Conclusion
- Focus on each index as the starting point
- Extend the subarray until a smaller element appears
- Count how many such extensions are possible

