Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Compute array sumE02. Find the maximum and minimum in an arrayE03. Count occurrences of a valueE

Problem No 1

Compute array sum

Easy

6 minute session

Summary

Calculate the sum of all numeric elements in an array.

Problem statement

Given a list of numbers (integers), compute and return the sum of all elements. If the list is empty, return 0. Do not use built-in sum() — implement the accumulation manually to practice iteration and accumulation patterns.

Task

Write a function that returns the sum of elements in a list of numbers. Handle empty lists and negative values.

Examples

Basic example

Input

[1, 2, 3, 4]

Output

10

Explanation

1 + 2 + 3 + 4 = 10

Input format

A single argument: a list of integers, e.g. [1, 2, 3]

Output format

An integer representing the sum of the list. For an empty list, return 0.

Constraints

The list length can be 0 to 10^6. Elements fit into Python int. Time complexity O(n).

Samples

Sample input 0

[5, -2, 7]

Sample output 0

10

Explanation 0

5 + (-2) + 7 = 10

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:42 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.