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 n, long long a[]){long long sum = 0;for (long long i = 0; i < n; i++){sum += a[i];}return sum;}int main(){long long n = 5;long long a[n] = {1000000001, 1000000002,1000000003, 1000000004, 1000000005};cout << arraySum(n, a) << "\n";return 0;}
No comments:
Post a Comment