java.lang.Object
g1501_1600.s1588_sum_of_all_odd_length_subarrays.Solution

public class Solution extends Object
1588 - Sum of All Odd Length Subarrays.<p>Easy</p> <p>Given an array of positive integers <code>arr</code>, calculate the sum of all possible odd-length subarrays.</p> <p>A subarray is a contiguous subsequence of the array.</p> <p>Return _the sum of all odd-length subarrays of _<code>arr</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> arr = [1,4,2,5,3]</p> <p><strong>Output:</strong> 58</p> <p><strong>Explanation:</strong> The odd-length subarrays of arr and their sums are:</p> <p>[1] = 1</p> <p>[4] = 4</p> <p>[2] = 2</p> <p>[5] = 5</p> <p>[3] = 3</p> <p>[1,4,2] = 7</p> <p>[4,2,5] = 11</p> <p>[2,5,3] = 10</p> <p>[1,4,2,5,3] = 15</p> <p>If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> arr = [1,2]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> arr = [10,11,12]</p> <p><strong>Output:</strong> 66</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 100</code></li> <li><code>1 <= arr[i] <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sumOddLengthSubarrays

      public int sumOddLengthSubarrays(int[] arr)