Tuesday, May 26, 2020
Home programming Solutions Simple Array Sum HackerRank Solution in Python

Simple Array Sum HackerRank Solution in Python

First, try to solve this problem by yourself – Click Here to solve

The Simple Array Sum problem is the basic problem of arrays that comes into the warmup section in Hackerrank.
We just need to add all integer values inside the array given and return them as the output.

Simple Array Sum HackerRank Solution - Python

Simple Array Sum HackerRank Function prototype:

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

  • ar: an array of integers

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.

Output Format

Print the sum of the array’s elements as a single integer.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

Explanation

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

Simple Array Sum HackerRank Solution in Python:

Solution Type 1
#!/bin/python3

import os
import sys

#
# Complete the simpleArraySum function below.
#
def simpleArraySum(ar):
    #
    # Write your code here.
    #
    x=0
    for i in ar:
      x+=i
    return x

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ar_count = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = simpleArraySum(ar)

    fptr.write(str(result) + '\n')

    fptr.close()
Solution Type 2:-
#!/bin/python3

import os
import sys

#
# Complete the simpleArraySum function below.
#
def simpleArraySum(ar):
    #
    # Write your code here.
    #
    return sum(ar)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ar_count = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = simpleArraySum(ar)

    fptr.write(str(result) + '\n')

    fptr.close()

If you found this solution helpful then please share it with your friends.

For any query just contact us- Click here

Join us on Telegram for the latest solutions and placement related help: Click Here to join us

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Grading Students HackerRank Solution in Python

First, try to solve the Grading Students problem by yourself - Click Here to solve The Grading students problem...

Simple Array Sum HackerRank Solution in Python

First, try to solve this problem by yourself - Click Here to solve The Simple Array Sum problem is...

SolveMeFirst HackerRank Solution in Python

First, try to solve this problem by yourself - Click Here to solve The SolveMeFirst problem is the basic...

Recent Comments