Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Simple Array Sum Problem Solution

Yashwant Parihar, April 9, 2023April 10, 2023

In this post, We are going to solve HackerRank Simple Array Sum Problem. Given an array of integers, find the sum of its elements.

For example, if the array ar=[1,2,3], 1 + 2 + 3 = 6, so return 6 .

Function Description

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 space-separated integers representing the array’s elements.

Constraints

0 < n, ar [I] < 1000

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.

HackerRank Simple Array Sum Problem Solution
HackerRank Simple Array Sum Problem Solution

Simple Array Sum C Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() 
{
    int *a,n,i,ans=0;
    scanf("%d",&n);
    a=(int *)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
        ans=ans+a[i];
    printf("%d",ans);
    return 0;
}

Simple Array Sum C++ Solution

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


int main() {
    unsigned nb;
    unsigned tot = 0;
    
    cin >> nb;
    for (int i = 0; i < nb; i++) {
        int num;
        cin>>num;
        tot += num;
    }
    cout << tot;
    return 0;
}

Simple Array Sum Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'simpleArraySum' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY ar as parameter.
     */

    public static int simpleArraySum(List<Integer> ar) {
        return ar.stream().mapToInt(i -> i).sum();

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int arCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> ar = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.simpleArraySum(ar);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Simple Array Sum Javascript Solution

function processData(input) {
    //Enter your code here
    var result = 0,
    lines = input.split('\n'),
    size = parseInt(lines[0]),
    digits = lines[1].trim().split(" ").slice(0, size);
    while (digits.length){
        result += parseInt(digits.pop(), 10);
    }
    console.log(result);
    return result;
} 

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

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

Simple Array Sum Python Solution

def counting(number, split):
    total = 0
    for i in range(0, number):
        total += int(split[i])
    return total

num = int(input())
array = input().split()
print(counting(num, array))

Other Solutions

  • HackerRank Compare the Triplets Problem Solution
  • HackerRank A Very Big Sum Problem Solution
c C++ HackerRank Solutions java javascript python Cc++Hackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes