From 7c504f1eba2f406864a2335b74c632a71da510d1 Mon Sep 17 00:00:00 2001 From: sunil Date: Fri, 2 Oct 2020 18:17:01 +0530 Subject: [PATCH] added solution --- .../1588.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1501-1600/1588. Sum of All Odd Length Subarrays/1588.cpp 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