Problem · Array
Subarray Sum
Learn this problemProblem statement
A subarray is a contiguous segment of an array. Given an array of n integers, determine the sum of all elements across all subarrays of that array.
Function
getSubarraySum(n: int, arr: int[]) → longExamples
Example 1
n = 3arr = [4, 5, 6]return = 50The subarrays are [4], [5], [6], [4, 5], [5, 6], and [4, 5, 6].
Their total sum is 4 + 5 + 6 + (4 + 5) + (5 + 6) + (4 + 5 + 6) = 50.
Example 2
n = 3arr = [1, 1, 1]return = 10The subarrays are [1], [1], [1], [1, 1], [1, 1], and [1, 1, 1].
Their total sum is 1 + 1 + 1 + (1 + 1) + (1 + 1) + (1 + 1 + 1) = 10.
Constraints
1 <= n <= 2 * 10^51 <= arr[i] <= 10^3, where0 <= i < n
More Visa problems
- Minimum Cost to Attend Required CoursesOA · Seen Jul 2026
- Minimum Score of a Path Between CitiesOA · Seen Jul 2026
- Planning ProductionOA · Seen Jul 2026
- Maximum Even Tag SumOA · Seen Jun 2026
- Transform Binary MatrixOA · Seen Jun 2026
- Minimum Cost to Select People for Skill QuotasOA · Seen May 2026
- Longest Selectable Non-Decreasing SubarrayOA · Seen Apr 2026
- Maximize Capped Contribution SumOA · Seen Apr 2026