Advertisement
nikunjsoni

1712

Apr 20th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int count_bet(vector<int> &a, int ind, int low, int high) {
  4.         if(low > high) return 0;
  5.    
  6.         auto l_itr = lower_bound(a.begin(), a.begin() + ind, low);
  7.         auto r_itr = upper_bound(a.begin(), a.begin() + ind, high);
  8.         return (r_itr - l_itr);
  9.     }
  10.    
  11.     int waysToSplit(vector<int>& a) {
  12.         int n = a.size(), mod = int(1e9)+7;
  13.         for(int i = 1; i < n; i++)
  14.             a[i] += a[i - 1];
  15.        
  16.         int sum = a[n-1];
  17.         long long cnt = 0;
  18.         int suf = 0;
  19.        
  20.         for(int i = n - 1; i >= 2; i--){
  21.             suf = a[n-1]-a[i-1];
  22.             auto in_bet = count_bet(a, i - 1, sum - 2*suf, (sum - suf)/2);
  23.             cnt = (cnt + in_bet) % mod;
  24.         }
  25.         return cnt;
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement