Array Sum

You are given an array of integers of size. You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.

Example:

Input:  n = 5, a = [1000000001, 1000000002, 1000000003, 1000000004, 1000000005]
Output: 5000000015

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long arraySum(long long nlong long a[])
{
    long long sum = 0;
    for (long long i = 0i < ni++)
    {
        sum += a[i];
    }
    return sum;
}
int main()
{
    long long n = 5;

    long long a[n] = {10000000011000000002,
                      100000000310000000041000000005};

    cout << arraySum(na<< "\n";

    return 0;
}


No comments:

Post a Comment