#1480EasyPrefix Sum

Running Sum of 1D Array

Build a prefix sum array — foundation of all range query problems.

AmazonGoogle

Problem Statement

Given an array nums, define the running sum of an array as runningSum[i] = sum(nums[0]...nums[i]). Return the running sum of nums.

Examples

Example 1

Input:nums = [1,2,3,4]
Output:[1,3,6,10]

Explanation: Running sum: [1, 1+2, 1+2+3, 1+2+3+4]

Example 2

Input:nums = [1,1,1,1,1]
Output:[1,2,3,4,5]

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -10⁶ ≤ nums[i] ≤ 10⁶

Solutions

1
Extra Array
TimeO(n²)SpaceO(n)

Create a result array. For each index, sum all elements from 0 to i. Straightforward but O(n²).

Visual Animation
def runningSum(nums: list[int]) -> list[int]:
    return [sum(nums[:i+1]) for i in range(len(nums))]

Related Concepts

Deepen your understanding with these related topics from our AI Glossary:

Deepen your understanding

Want to master the core concepts?

Our free AI Glossary covers 190+ topics — from Prefix Sum to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.

Browse AI Glossary All Problems
39+
AI Models
₹69
Per day used
4
Languages

Stuck? Ask AI to explain it step by step.

Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.

Free to start · No credit card required to explore

Get Started Free
Back to all problems