Algorithm
Problem Name: Algorithms -
In this HackerRank Functions in Algorithms - Java programming problem solution,
In this challenge, you are required to calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.
Function Description
Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements.
aVeryBigSum has the following parameter(s):
- int ar[n]: an array of integers .
Return
- long: the sum of all array elements
Input Format
The first line of the input consists of an integer n .
The next line contains n space-separated integers contained in the array.
Output Format
Return the integer sum of the elements in the array.
Constraints
1 <= n <= 100
0 <= ar[i] <= 10 power 10
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
Code Examples
#6 Code Example with C Programming
Code -
C Programming
#include<stdio.h>
#define max 1000
int main(){
int ar[max];
int n;
scanf("%d",&n);
int i;
for(i = 0; i < n; i++){
scanf("%d",&ar[i]);
}
long long sum=0;
for(i = 0; i < n; i++){
sum += ar[i];
}
printf("%lli",sum);
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
vector < string> split_string(string);
// Complete the aVeryBigSum function below.
long aVeryBigSum(vector<long> ar) {
long long int total = accumulate(ar.begin(), ar.end(), 0ll);
return total;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int ar_count;
cin >> ar_count;
cin.ignore(numeric_limits < streamsize>::max(), '\n');
string ar_temp_temp;
getline(cin, ar_temp_temp);
vector < string> ar_temp = split_string(ar_temp_temp);
vector<long> ar(ar_count);
for (int i = 0; i < ar_count; i++) {
long ar_item = stol(ar_temp[i]);
ar[i] = ar_item;
}
long result = aVeryBigSum(ar);
fout << result << "\n";
fout.close();
return 0;
}
vector < string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector < string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
static long aVeryBigSum(long[] ar) {
long sum = 0;
for(int i= 0; i < ar.length; i++)
{
sum = sum+ ar[i];
}
return sum;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int arCount = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
long[] ar = new long[arCount];
String[] arItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < arCount; i++) {
long arItem = Long.parseLong(arItems[i]);
ar[i] = arItem;
}
long result = aVeryBigSum(ar);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Javascript Programming
Code -
Javascript Programming
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(readLine());
var sum = 0;
arr = readLine().split(' ');
arr = arr.map(Number);
sum = arr.reduce( (prev, curr) => prev + curr );
console.log(sum);
}
Copy The Code &
Try With Live Editor
#5 Code Example with C# Programming
Code -
C# Programming
using System;
class Solution
{
static void Main(String[] args)
{
var finalSum = 0L;
var n = int.Parse(Console.ReadLine());
var ar_temp = Console.ReadLine().Split(' ');
var ar = Array.ConvertAll(ar_temp, long.Parse);
for (int i = 0; i < n; i++)
finalSum += ar[i];
Console.WriteLine(finalSum);
}
}
Copy The Code &
Try With Live Editor
#6 Code Example with Python Programming
Code -
Python Programming
import math
import os
import random
import re
import sys
# Complete the aVeryBigSum function below.
def aVeryBigSum(ar):
sum = 0
for i in range(len(ar)):
sum+=ar[i]
return sum
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
ar_count = int(input())
ar = list(map(int, input().rstrip().split()))
result = aVeryBigSum(ar)
fptr.write(str(result) + '\n')
fptr.close()
Copy The Code &
Try With Live Editor