In this HackerRank Simple Array Sum problem, you have given an array of integers, find the sum of its elements.

HackerRank Simple Array Sum problem solution


Foundation course by prepbytes

Problem solution in Python programming.

#!/bin/python3

import os
import sys

#
# Complete the simpleArraySum function below.
#
def simpleArraySum(ar):
    x=0
    for i in range(0,ar_count):
        x = x + ar[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()




Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

	public static void main(String[] args) throws Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		br.readLine();
		int output=0;
		String[] input = br.readLine().split(" ");
		for(String value:input)
		{
			output += Integer.parseInt(value);
		}
		System.out.println(output);
	}
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int numLines;
    int currNumber, total = 0;
    
    cin >> numLines;
    
    for (int i=0; i<numLines;i++) {
        cin >> currNumber;
        total += currNumber;
    }
    
    cout << total;
    
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int N;
int map[1000];
int main() {
  long int sum =0;
    scanf("%d",&N);
    for(int i = 0; i<N; i++)
        {
      scanf("%d",&map[i]);
    }
    for(int i=0; i<N;i++)
        {
        sum += map[i];
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    printf("%ld",sum);
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    console.log(input.split(/\n/)[1].split(/\s/).map(Number).reduce(function(a, b) {
        return a + b;
    }))
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});