diff --git a/1501-1600/1588. Sum of All Odd Length Subarrays/1588.cpp b/1501-1600/1588. Sum of All Odd Length Subarrays/1588.cpp new file mode 100644 index 00000000..0198ad3e --- /dev/null +++ b/1501-1600/1588. Sum of All Odd Length Subarrays/1588.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int sumOddLengthSubarrays(vector& arr) { + // contribution of each element in subarrrays = (i+1) * (n-i) + // i.e, hoe many left indexes for start (including a[i]) * how many right indexes to end ( including a[i]) + // for odd length, just divide by 2 + // now, we have element and it's contribution number, add number * contribution to your answer + int ans =0,n=arr.size(); + for(int i =0;i