
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of Subarray Minimums in C++
Suppose we have an array of integers A. We have to find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be very large, then return the answer in modulo 10^9 + 7. So if the input is like [3,1,2,4], then the output will be 17, because the subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4], so minimums are [3,1,2,4,1,1,2,1,1,1], and the sum is 17.
To solve this, we will follow these steps −
m := 1 x 10^9 + 7
Define two methods, add() will take a, b and returns the (a mod m + b mod m) mod m, mul() will take a, b and returns the (a mod m * b mod m) mod m
The main method will take the array A, define a stack st, and set n := size of array A
Define two arrays left of size n and fill with -1, and another is right of size n, fill with n
set ans := 0
for i in range 0 to n – 1
while st is not empty and A[stack top] >= A[i], delete from st
if st is not empty, then set left[i] := top of st
insert i into st
while st is not empty, then delete st
for i in range n – 1 down to 0
while st is not empty and A[stack top] >= A[i], delete from st
if st is not empty, then set right[i] := top of st
insert i into st
for i in range 0 to n – 1
leftBound := i – left[i] + 1, rightBound := right[i] – 1 – i
contri := 1 + leftBound + rightBound + (leftBound * rightBound)
ans := add(ans and mul(contri, A[i]))
return ans
Example(C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; typedef long long int lli; const lli MOD = 1e9 + 7; class Solution { public: lli add(lli a, lli b){ return (a % MOD + b % MOD) % MOD; } lli mul(lli a, lli b){ return (a % MOD * b % MOD) % MOD; } int sumSubarrayMins(vector<int>& A) { stack <int> st; int n = A.size(); vector <int> left(n, -1); vector <int> right(n, n); int ans = 0; for(int i = 0; i < n; i++){ while(!st.empty() && A[st.top()] >= A[i]){ st.pop(); } if(!st.empty())left[i] = st.top(); st.push(i); } while(!st.empty())st.pop(); for(int i = n - 1; i >= 0; i--){ while(!st.empty() && A[st.top()] > A[i]){ st.pop(); } if(!st.empty())right[i] = st.top(); st.push(i); } for(int i = 0; i < n; i++){ int leftBound = i - (left[i] + 1); int rightBound = (right[i] - 1) - i; int contri = 1 + leftBound + rightBound + (leftBound * rightBound); ans = add(ans, mul(contri, A[i])); } return ans; } }; main(){ vector<int> v = {3,1,2,4}; Solution ob; cout << (ob.sumSubarrayMins(v)); }
Input
[3,1,2,4]
Output
17
- Related Articles
- Continuous Subarray Sum in C++
- Subarray Sum Equals K in C++
- Minimum Size Subarray Sum in C++
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Maximum subarray sum modulo m in C++
- Maximum circular subarray sum in C++\n
- Maximum subarray sum in O(n) using prefix sum in C++
- Maximize the subarray sum after multiplying all elements of any subarray with X in C++
- Find Sum of all unique subarray sum for a given array in C++
- Find Maximum Sum Strictly Increasing Subarray in C++
- Maximum Subarray Sum with One Deletion in C++
- Maximum Subarray Sum Excluding Certain Elements in C++
- Maximum Size Subarray Sum Equals k in C++
