Leetcode: Running Sum of a 1D Array
Kotlin

Leetcode: Running Sum of a 1D Array

2024-03-20
10 min read

Leetcode — Running Sum of a 1D array

The Leetcode problem Running Sum of 1D array is tagged as an easy question. Running Sum of 1d Array - LeetCode

The problem statement is as follows:

Given an array *nums*. We define a running sum of an array as runningSum[i] = sum(*nums*[0]...*nums*[i]). 

Return the running sum of *nums*

Examples

Constraints

Brainstorm

This problem is pretty straightforward. One way to solve this is to have a variable that adds the current value to it as we loop through the array. We will then add the sum’s current value to the current number in the loop.

Below are examples of this solution in Java and Kotlin.

Java: {% gist https://gist.github.com/cmcoffeedev/b2d178d270a1b740968288c9874d815e.js %}

Kotlin: {% gist https://gist.github.com/cmcoffeedev/f4ed6540300d37ddd98e03abb4d2ca97.js %} Thank you for taking the time to read this!