WATER LOGGING BETWEEN THE BUILDINGS

question link

solution

question link

Andrew has recently moved to Wengaluru. He finds this city amazing as all the buildings of the city are of same shape. There are N buildings in a row with no space in between. Buildings may have different heights while width in other two dimensions is always 1 unit.

Rain occurs very frequently in Wengaluru so Andrew being the curious boy thought that if the entire city is flooded with water then How much water would be collected between the buildings? Water would be collected between the buildings if a small sized building occurs between 2 big sized building i.e. if a building with small height occurs between buildings of relatively larger height.

Input

The first line contains T, the number of test cases.

Each test case consist of 2 lines. First line of each test case Would contain N- total no. of buildings in the city. Second line contains N integers, the height of N buildings.

The first line contains T, the number of test cases.

Each test case consist of 2 lines. First line of each test case Would contain N- total no. of buildings in the city. Second line contains N integers, the height of N buildings.

Output

For each test case output the answer to the above query. As the total water collected between the buildings could be a huge number so output your answer by taking Modulus with 10^9+7(1000000007).

For each test case output the answer to the above query. As the total water collected between the buildings could be a huge number so output your answer by taking Modulus with 10^9+7(1000000007).

2 5 3 2 1 4 5 5 1 2 3 4 5

solution

`/*`

`I Will Win Not Immediately But Definitely.. -Aniruddha Sharma`

`*/`

`// Name:- Aniruddha Sharma`

`// Problem:- Andrew and Wengaluru City`

`// Site:- HackerEarth`

`#include<iostream>`

`#include<map>`

`#include<vector>`

`#include<iostream>`

`#include<algorithm>`

`#include<cstring>`

`#include<cstdio>`

`#include<cmath>`

`#include<functional>`

`#include<vector>`

`#include<stack>`

`#include<set>`

`#include<map>`

`#include<queue>`

`#include<deque>`

`using namespace std;`

`long long arr[100010],left_max[100010],right_max[100010];`

`int totalWaterFilled(int n)`

`{`

`long long i,ans=0;`

`left_max[0]=arr[0];`

`for(i=1;i<n;i++)`

`{`

`if(arr[i]>left_max[i-1])`

`left_max[i]=arr[i];`

`else`

`left_max[i]=left_max[i-1];`

`}`

`right_max[n-1]=arr[n-1];`

`for(i=n-2;i>=0;i--)`

`{// finding the rightmost and left most building whichever is bigger was the most important task`

`if(arr[i]>right_max[i+1])`

`right_max[i]=arr[i];`

`else`

`right_max[i]=right_max[i+1];`

`}`

`// now the only tast left is too see when a buling get a water logging or not and how much it gets ( that is equal to the smaller of the big buildings (right or left) to it)`

`for(i=1;i<=n-2;i++)`

`{`

`if((min(left_max[i-1],right_max[i+1])-arr[i])>0)`

`ans=(ans+(min(left_max[i-1],right_max[i+1])-arr[i]))%1000000007;`

`}`

`return ans;`

`}`

`int main()`

`{`

`int t;`

`cin>>t;`

`while(t--)`

`{`

`int n;`

`cin>>n;`

`for(int i=0;i<n;i++)`

`cin>>arr[i];`

`cout<<totalWaterFilled(n)<<endl;`

`}`

`return(0);`

`}`