Problem · Array

Subarray Sum

Learn this problem
EasyVisaOA

Problem 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[]) → long

Examples

Example 1

n = 3arr = [4, 5, 6]return = 50

The 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 = 10

The 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^5
  • 1 <= arr[i] <= 10^3, where 0 <= i < n

More Visa problems

drafts saved locally
public long getSubarraySum(int n, int[] arr) {
  // write your code here
}
n3
arr[4, 5, 6]
expected50
checking account