jayati

odd length subarray sum

Sep 5th, 2022
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.29 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int sumOddLengthSubarrays(vector<int>& arr) {
  4.  
  5. int sum=0;
  6. int n=arr.size();
  7. for(int i=0;i<n;i++)
  8. {
  9. int count= ((i+1)*(n-i) +1)/2;
  10. sum+= count* arr[i];
  11. }
  12. return sum;
  13. }
  14. };
Advertisement
Add Comment
Please, Sign In to add comment