Back to Practice Dashboard
Python BasicsEasy

Calculate the sum of elements in an array

Learn how to solve the 'Calculate the sum of elements in an array' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function array_sum(arr) that takes a list of integers arr and returns the sum of all elements in the array. If the array is empty, return 0.

Constraints
  • 0 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9

Examples

Example 1
Input
arr = [1, 2, 3, 4, 5]
Output
15
Explanation

1 + 2 + 3 + 4 + 5 = 15.

Example 2
Input
arr = [-1, -2, -3]
Output
-6
Explanation

-1 + (-2) + (-3) = -6.

Example 3
Input
arr = []
Output
0
Explanation

Sum of an empty array is 0.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor