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. A very big sum is similar to an array sum or we can say that both problems are the same the only title is different. The same logic applies first take input from the user and store the input in an array after that take a long long int variable(Cause our array is integer type) and add the array elements and store it in a long-size variable, in the end, print the sum. See the below explanation part.
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 <= 10
0 <= at[i] <= 10^10
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
Submit your solution here: Click here
A Very Big Sum Hackerrank Solution C++
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
*Complete the 'aVeryBigSum' function below.
*
*The function is expected to return a LONG_INTEGER.
*The function accepts LONG_INTEGER_ARRAY ar as parameter.
*/
long aVeryBigSum(vector<long> ar)
{
long result = 0;
for (int i = 0; i < ar.size(); i++) result += ar[i];
return result;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string ar_count_temp;
getline(cin, ar_count_temp);
int ar_count = stoi(ltrim(rtrim(ar_count_temp)));
string ar_temp_temp;
getline(cin, ar_temp_temp);
vector<string> ar_temp = split(rtrim(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;
}
string ltrim(const string &str)
{
string s(str);
s.erase( s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int> (isspace)))
);
return s;
}
string rtrim(const string &str)
{
string s(str);
s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int> (isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str)
{
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos)
{
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
A Very Sum Explanation
The size of the sum variable should be big for the big size we are taking a long long int sum. by using a For Loop add all elements of an array and print the sum. We can do this by using one Loop and By using separate Loops for Both storing elements and the Sum of elements of an array.
for(int arr_i = 0;arr_i < n;arr_i++)
{
cin >> arr[arr_i];
sum+=arr[arr_i];
}
cout<<sum;
for(int arr_i = 0;arr_i < n;arr_i++)
{
cin >> arr[arr_i];
sum+=arr[arr_i];
}
cout<<sum;
A Very Big Sum Hackerrank Solution Output
Similar to A Very Big Sum
- Deque STL Hackerrank Solution in C++
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
- Plus Minus Hackerrank Solution C++
- Hello World in C Hackerrank Solution
- Divisible Sum Pairs Hackerrank Solution in C++
- Apple And Orange Hackerrank Solution
- Simple Array Sum Hackerrank Solution C++
- Compare The Triplets Hackerrank Solution C++
0 Comments: