21/03/2023

A Very Big Sum Hackerrank Solution C++ | Algorithms 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. 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.

A Very Big Sum Hackerrank Solution C++

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;

A Very Big Sum Hackerrank Solution Output


A Very Big Sum Hackerrank Solution Output

Similar to A Very Big Sum


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: