A Very Big Sum - - Hacker Rank Problem C++ Calculate and print the sum of the el
ID: 3713092 • Letter: A
Question
A Very Big Sum - - Hacker Rank Problem C++
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 function aVeryBigSum described below to return the sum of all elements of the array.
aVeryBigSum(integer: n, integer: arr_size, integer array: arr)
Parameters: n: array size
arr_size: array size
ar: array of integers to sum
Returns: integer sum of all array elements
Constraints
1 < = N< = 10
0< = A[i]<= 1010
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
Print a single value equal to the sum of the elements in the array. In the above sample, you would print 5000000015.
The solution to this problem can be found on the web, but my point is that usually helpful comments and insightful comments are missing from the source code. Please include your reasoning and logic on your solution.
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
/*
* Complete the aVeryBigSum function below.
*/
long aVeryBigSum(int n, vector<long> ar) {
/*
* Write your code here.
*/
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), ' ');
string ar_temp_temp;
getline(cin, ar_temp_temp);
vector<string> ar_temp = split_string(ar_temp_temp);
vector<long> ar(n);
for (int ar_itr = 0; ar_itr < n; ar_itr++) {
long ar_item = stol(ar_temp[ar_itr]);
ar[ar_itr] = ar_item;
}
long result = aVeryBigSum(n, ar);
fout << result << " ";
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;
}
Expert Answer
Explanation / Answer
The code is as follows :
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string); // initializing the vector
/*
* Complete the aVeryBigSum function below.
*/
long long int aVeryBigSum(int n, vector<long> ar) { // I changed the return type to long long int as it can handle values till 264 so there will be no overflow even with large numbers
/*
* Write your code here.
*/
long long int sum = 0; // initializing the sum as 0
for(int i = 0; i < n; i++) // traversing the vector
{
sum += ar[i]; // adding each element
}
return sum; returning the total sum
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n; // taking the input
cin.ignore(numeric_limits<streamsize>::max(), ' ');
string ar_temp_temp;
getline(cin, ar_temp_temp); // taking the input of the whole line with spaces
vector<string> ar_temp = split_string(ar_temp_temp); // splitting the string with reference to the whitespaces
vector<long> ar(n); // decalring a vector to store all the elements
for (int ar_itr = 0; ar_itr < n; ar_itr++) {
long ar_item = stol(ar_temp[ar_itr]); // storing all the elements in the vector
ar[ar_itr] = ar_item;
}
long long int result = aVeryBigSum(n, ar); // changed the datatype to suit the return value of the function
fout << result << " "; // giving the output to the file stream
fout.close(); // closing the file stream
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;
}
Output :
5
1000000001 1000000002 1000000003 1000000004 1000000005
5000000015
As a tip always when coding on competitive coding websites, try to use long long int instead of just int so that your data diesn't overflow while dealing with huge constraints
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.