Random Sum - Expected Sum
Difficulty: Hard

Given an a list D of N integers, and an integer T, two numbers L and R are randomly chosen such that 0 ≤ L ≤ R < N.

What is the expected sum of the subarray {d[L]...d[R]} given that (sum{d[L],...d[R]} <= T)?

**Input Format:**

The first line will contain two numbers: N and T. N lines follow, each containing one number from list D.

**Output Format:**

Output one number P, the expected sum of D[L]...D[R] upto 9 decimal places. 

**Constraints:**

1 <= N < 100000

0 <= D[i] < 10^7

0 < T <= 10^9

**Sample Input:**
<pre>
5 10
4 
5 
3 
7
1
</pre>

**Sample Output:**
<pre>6.111111111</pre>

**Explaination**

[4],[5],[3],[7],[1],[4,5],[5,3],[3,7],[7,1] subarrays satisfy the property. 

4 + 5 + 3 + 7 + 1 + 9 + 8 + 10 + 8 = 55

55/9 = 6.11111111



Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:

Editorial: Random Sum - Expected Sum Solution by <a href ="https://www.hackerrank.com/sidsgr88">Siddharth Bora</a>
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><h3>Random Sum - Expected Sum</h3>

<p><strong>Idea:</strong> Two-Pointer</p>

<p><strong>Short Explanation:</strong>
You are expected to have the read the solution to Random Sum problem as the two-pointer idea is carried forward from that.
The problem asks us to give the expected sum of the arrays that have been found in the previous problem. This can be done maintaing a cumulative sum and a cumulative of cumulative sum and keeping a total sum that is updated as we calculate the count moving the two pointers as in the previous problem.</p>

<p><strong>Long Explanation:</strong>
We pre-calculate two arrays. Firstly a cumulative sum and then a cumulative of cumulative sum. The first of these gives the sum of number from the starting to a particular index. The second gives us the sum of all the sequences that start at index 0 and end at indices from 0 to i basically the ith value will be ele0 + (ele0 + ele 1) +...+ (ele0+..+elei) where elex is the xth element in the array. 
Now since we need the sum of all the sequences that sum to &lt;= T, we augment the previous question (Random Sum) solution to find the total sum too. Basically whenever we have an l,r we can calculate the sum of all sequences starting at l and ending at indices between l and r(left end inclusive) by 
cumcumsum[r-1]-((l&gt;0)?cumcumsum[l-1]:0)-(r-l)*((l&gt;0)?cumsum[l-1]:0);
where cumcumsum is the cumulative of cumulative sum array and cumsum is the cumulative sum array. Here we are subtracting all cumulative sums less than l and also subtracting the portion of cumcumsum[r-1] which extends to the left of l namely the third term. This gives us the cumulative sum and finally we simply have to divide this by the previous answer to get the expected value.</p>

<p><strong>Language : C++</strong></p>

<pre><code>using namespace std;
#include &lt;cmath&gt;
#include &lt;cstdio&gt;
#include &lt;list&gt;
#include &lt;map&gt;
#include &lt;set&gt;
#include &lt;queue&gt;
#include &lt;deque&gt;
#include &lt;stack&gt;
#include &lt;bitset&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
typedef long long ll; 
typedef pair&lt;int,int&gt; pii; 
#define FOR(i,n) for (int i = 0; i &lt; n; i++)
#define SZ(x) ((int)x.size())
#define PB push_back
#define MP make_pair
#define sf(x) scanf("%d",&amp;x)
#define pf(x) printf("%d\n",x)
#define split(str) {vs.clear();istringstream ss(str);while(ss&gt;&gt;(str))vs.push_back(str);}
#define PI 3.141592653589793
int main()
{
  ll count=0;
  ll count2=0;
  int n,t;
  sf(n);sf(t);
  ll sum=0;
  int l=0;
  int r=-1;
  vector&lt;int&gt; v;
  while(n--)
  {
    int a;
    sf(a);
    v.PB(a);
  }
  vector&lt;ll&gt; cumsum;
  FOR(i,v.size())
    cumsum.PB(v[i]);
  for(int i=1;i&lt;v.size();i++)
    cumsum[i]+=cumsum[i-1];

  vector&lt;ll&gt; cumcumsum;
  FOR(i,v.size())
    cumcumsum.PB(cumsum[i]);
  for(int i=1;i&lt;v.size();i++)
    cumcumsum[i]+=cumcumsum[i-1];
 // FOR(i,v.size())
   // cout&lt;&lt;cumcumsum[i]&lt;&lt;endl;
  while(l&lt;v.size())
  {
    //cout&lt;&lt;r&lt;&lt;" "&lt;&lt;(r&lt;v.size())&lt;&lt;" "&lt;&lt;(sum&lt;=t)&lt;&lt;endl;
    while(r&lt;(int)v.size() &amp;&amp; sum&lt;=t)
    {
      //cout&lt;&lt;"hello"&lt;&lt;endl;
      //count2+=cumsum[r];
      r++;
      if(r&lt;v.size())
      {
    sum+=v[r];
      }
    }
    if(r-l&gt;0)
    {  count+=r-l;
    //cout&lt;&lt;r&lt;&lt;" "&lt;&lt;l&lt;&lt;endl;
    count2+=cumcumsum[r-1]-((l&gt;0)?cumcumsum[l-1]:0)-(r-l)*((l&gt;0)?cumsum[l-1]:0);
    }
    sum-=v[l];
    l++;
  }
  //cout&lt;&lt;count2&lt;&lt;endl;
  //cout&lt;&lt;count&lt;&lt;endl;
  double ans=count2*1.0/count;
  printf("%.9lf\n",ans);
}
</code></pre>

Reference solution (C++):
   	using namespace std;
    #include <cmath>
    #include <cstdio>
    #include <list>
    #include <map>
    #include <set>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #define all(c) (c).begin(),(c).end()
    #define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
    typedef long long ll; 
    typedef pair<int,int> pii; 
    #define FOR(i,n) for (int i = 0; i < n; i++)
    #define SZ(x) ((int)x.size())
    #define PB push_back
    #define MP make_pair
    #define sf(x) scanf("%d",&x)
    #define pf(x) printf("%d\n",x)
    #define split(str) {vs.clear();istringstream ss(str);while(ss>>(str))vs.push_back(str);}
    #define PI 3.141592653589793
    int main()
    {
      ll count=0;
      ll count2=0;
      int n,t;
      sf(n);sf(t);
      ll sum=0;
      int l=0;
      int r=-1;
      vector<int> v;
      while(n--)
      {
        int a;
        sf(a);
        v.PB(a);
      }
      vector<ll> cumsum;
      FOR(i,v.size())
        cumsum.PB(v[i]);
      for(int i=1;i<v.size();i++)
        cumsum[i]+=cumsum[i-1];
      
      vector<ll> cumcumsum;
      FOR(i,v.size())
        cumcumsum.PB(cumsum[i]);
      for(int i=1;i<v.size();i++)
        cumcumsum[i]+=cumcumsum[i-1];
     // FOR(i,v.size())
       // cout<<cumcumsum[i]<<endl;
      while(l<v.size())
      {
        //cout<<r<<" "<<(r<v.size())<<" "<<(sum<=t)<<endl;
        while(r<(int)v.size() && sum<=t)
        {
          //cout<<"hello"<<endl;
          //count2+=cumsum[r];
          r++;
          if(r<v.size())
          {
    	sum+=v[r];
          }
        }
        if(r-l>0)
        {  count+=r-l;
        //cout<<r<<" "<<l<<endl;
        count2+=cumcumsum[r-1]-((l>0)?cumcumsum[l-1]:0)-(r-l)*((l>0)?cumsum[l-1]:0);
        }
        sum-=v[l];
        l++;
      }
      //cout<<count2<<endl;
      //cout<<count<<endl;
      double ans=count2*1.0/count;
      printf("%.9lf\n",ans);
    }