Tutorialarray
Sum of an Array
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer array nums, return the sum of all its elements.
Examples
| nums | result |
|---|---|
[1, 2, 3, 4] | 10 |
[5] | 5 |
[-3, 3, -7] | -7 |
Example 1 — 1 + 2 + 3 + 4 = 10.
Example 2 — A single element is its own sum.
Example 3 — -3 + 3 + (-7) = -7.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(nums: int[]) → int
Constraints
-
nums.length -
nums[i]
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| nums | Expected |
|---|---|
[1,2,3,4] | 10 |
[5] | 5 |
[-3,3,-7] | -7 |
Hint
Hint
Start total = 0 and add each element in a loop: for x in nums: total += x.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1200 ms | estimated |
| C# 14 (.NET 10) | 1200 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 100 | 12 | verdict only |