Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-10-15_22-58-01.png
40.5 KB
#N1588 Sum of All Odd Length Subarrays
problem link=>https://leetcode.com/problems/sum-of-all-odd-length-subarrays/

#solution
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int res=0;
for(int i=0; i<arr.length; i++){
int start=arr.length-i;
int end=i+1;
if(start*end%2==1)
res+=(start*end+1)/2 * arr[i];
else
res+=(start*end)/2 * arr[i];
}
return res;
}
}