Simple Array Sum - Problem Solving | HackerRank
Tags: python, algorithms, coding, newbie
Published: 2023-10-10 05:48:40

Simple Array Sum is easy-level Python problem that requires basic knowledge of Array. In this post, we will provide a Python solution for Simple Array Sum.

## Problem Statement and Explanation

Given an array of integers, find the sum of its elements. For example, if the array `ar = [1, 4, 5]`, `1 + 4 + 5 = 10`, so return `10`.

Where n is the size of the array, the function `simpleArraySum` takes an array of integers as input and returns the sum of all the elements in the array. And arr[i] is the ith element of the array. The function works as follows:

- It initializes a variable `sum` to 0.
- It uses the `accumulate()` function to iterate over the elements of the array. The `accumulate()` function takes three arguments: the beginning iterator of the range, the end iterator of the range, and an initial value.
- In this case, the initial value is 0. For each element in the array, the `accumulate()` function adds the element to the variable `sum`.

### Input Format

- The first line contains an integer, `n`, denoting the size of the array.

### Output Format

- Sum of the array’s elements as a single integer.

## Simple Array Sum Solution in Python

{% gist https://gist.github.com/codeperfectplus/934ae56572a9be30deeb86393c052b9b.js %}

## Simple Array Sum Solution in C++

{% gist https://gist.github.com/codeperfectplus/a0f18d284977ea80aec969928c667e39.js %}

## Explanation of Solution(C++)

The function takes an array of integers as input and returns the sum of all the elements in the array. The function works as follows:

- It initializes a variable `sum` to 0.
- It uses the `accumulate()` function to iterate over the elements of the array. The `accumulate()` function takes three arguments: the beginning iterator of the range, the end iterator of the range, and an initial value.
- In this case, the initial value is 0. For each element in the array, the `accumulate()` function adds the element to the variable `sum`.
- Finally, the function returns the value of `sum`.

Here is an explanation of the `accumulate()` function:

The `accumulate()` function is a standard library function in C++. It takes three arguments:

1. The beginning iterator of the range
2. The end iterator of the range
3. An initial value.

The function iterates over the range and adds each element to the initial value. The final value of the initial value is returned by the function.

In the case of the function `simpleArraySum`, the `accumulate()` function iterates over the elements of the array and adds each element to the initial value of 0. The final value of 0 is the sum of all the elements in the array.

I hope this explanation is clear. Let me know if you have any other questions.

## Explanation of Solution(Python)

The function takes a list of integers as input and returns the sum of all the elements in the list. The function works as follows:

- It initializes a variable `sum` to 0.
- It iterates over the elements of the list using a for loop.
- For each element in the list, the function adds the element to the variable `sum`.
- Finally, the function returns the value of `sum`.