Arrays and Hashing Array basics

49. Sum of Array Elements

Easy

You are given an array of integers nums. Return the sum of all its elements.

The answer is the exact sum, which can be negative. It is not taken modulo anything.

Example 1

Input: nums = [3, -7, 12, 5, -1]

Output: 12

Explanation: 3 − 7 + 12 + 5 − 1 = 12. The negative elements lower the sum like any others.

Example 2

Input: nums = [1000000000, 1000000000, 1000000000]

Output: 3000000000

Explanation: Three times 109 is 3,000,000,000. That is more than 2,147,483,647, the largest value a 32-bit integer can hold.

Example 3

Input: nums = [-6]

Output: -6

Check your understanding

Work out the answer for this input before you write any code.

nums = [1000000000, 1000000000, 1000000000, 1000000000, 294967301]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.