Simple Array Sum — Hackerrank

Simple Array Sum — Hackerrank

Aadam
Competitive Programming
1 min readApr 1, 2017

--

Given an array of N integers, can you find the sum of its elements?

Input Format

The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers representing the array’s elements.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

Explanation

We print the sum of the array’s elements, which is: 1+2+3+4+10+11 = 31.

My Solution:

Here’s my Python solution. It’s pretty straightforward. We just use the built-in function sum() on the list of integers that we get from the input()

https://gist.github.com/aadimator/e5b79858b3b95f2826e579b97b5aa0be

I also did the same problem in C++. Here, we loop over all the input integers and just store their sum in our output variable.

https://gist.github.com/aadimator/fc47a8a42d0acc02f094bbb39cccf00b

I would welcome any queries or suggestions to improve this algorithm. Feel free to reach out to me.

If you want me to solve some specific problem, you can ask me here.

--

--

Aadam
Competitive Programming

I am a passionate individual with a zest for knowledge which drives me to learn about new concepts and technologies.