We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>usingnamespacestd;stringltrim(conststring&);stringrtrim(conststring&);vector<string>split(conststring&);/* * Complete the 'aVeryBigSum' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts LONG_INTEGER_ARRAY ar as parameter. */longaVeryBigSum(vector<long>ar){longsum=0;// Initialize sum to 0for(longnum:ar){sum+=num;// Add each element to the sum}returnsum;// Return the sum}intmain(){ofstreamfout(getenv("OUTPUT_PATH"));stringar_count_temp;getline(cin,ar_count_temp);intar_count=stoi(ltrim(rtrim(ar_count_temp)));// Read the number of elementsstringar_temp_temp;getline(cin,ar_temp_temp);// Read the space-separated list of numbersvector<string>ar_temp=split(rtrim(ar_temp_temp));// Split input string into componentsvector<long>ar(ar_count);// Create a vector to store the numbersfor(inti=0;i<ar_count;i++){longar_item=stol(ar_temp[i]);// Convert string to longar[i]=ar_item;// Store the number in the array}longresult=aVeryBigSum(ar);// Call the function to compute the sumfout<<result<<"\n";// Output the resultfout.close();// Close the output filereturn0;}stringltrim(conststring&str){strings(str);s.erase(s.begin(),find_if(s.begin(),s.end(),not1(ptr_fun<int,int>(isspace))));returns;}stringrtrim(conststring&str){strings(str);s.erase(find_if(s.rbegin(),s.rend(),not1(ptr_fun<int,int>(isspace))).base(),s.end());returns;}vector<string>split(conststring&str){vector<string>tokens;string::size_typestart=0;string::size_typeend=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));// Add the last tokenreturntokens;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Very Big Sum
You are viewing a single comment's thread. Return to all comments →