Funcia
Tutorialarray

Sum of an Array

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Given an integer array nums, return the sum of all its elements.

Examples

numsresult
[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

  • 1≤1 \le nums.length ≤1,000\le 1{,}000
  • −106≤-10^6 \le nums[i] ≤106\le 10^6

Expected complexity

Time
O(N)
Space
O(1)

Examples

numsExpected
[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

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1200 msestimated
C# 14 (.NET 10)1200 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness10012verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.