Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Simple Array Sum Problem Solution

HackerRank Simple Array Sum Problem Solution

Posted on April 9, 2023April 10, 2023 By Yashwant Parihar No Comments on HackerRank Simple Array Sum Problem Solution

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

Table of Contents

  • Simple Array Sum C Solution
  • Simple Array Sum C++ Solution
  • Simple Array Sum Java Solution
  • Simple Array Sum Javascript Solution
  • Simple Array Sum Python 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 Tags:C, c++, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Solve Me First Problem Solution
Next Post: HackerRank Compare the Triplets Problem Solution

Related Posts

HackerRank Find the Median Problem Solution HackerRank Find the Median Problem Solution c
HackerRank Max Min Problem Solution HackerRank Max Min Problem Solution c
HackerRank Airports Problem Solution HackerRank Airports Problem Solution c
HackerRank Solve Me First Problem Solution HackerRank Solve Me First Problem Solution c
HackerRank Beautiful Triplets Problem Solution HackerRank Beautiful Triplets Problem Solution c
HackerRank Beautiful Days at the Movies Solution HackerRank Beautiful Days at the Movies Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme