A Very Big Sum

Sort by

recency

|

1951 Discussions

|

  • + 0 comments
    # Python version
    def aVeryBigSum(ar):
        return sum(ar)
    
  • + 0 comments

    its can be simply done with return sum(ar) in python 3

  • + 0 comments
    sumOfArray = 0
    # Write your code here
    for i in range(len(ar)):
        sumOfArray += ar[i]
    return sumOfArray
    
  • + 0 comments

    Here is my c++ solution, you can watch the video explanation here : https://youtu.be/k6H_RljnY8g

    long aVeryBigSum(vector<long> ar) {
        long result = 0;
        for(int i = 0; i < ar.size(); i++) result += ar[i];
        return result;
    }
    
  • + 0 comments
    #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 sum = 0;  // Initialize sum to 0
        for (long num : ar) {
            sum += num;  // Add each element to the sum
        }
        return sum;  // Return the sum
    }
    
    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)));  // Read the number of elements
    
        string ar_temp_temp;
        getline(cin, ar_temp_temp);  // Read the space-separated list of numbers
    
        vector<string> ar_temp = split(rtrim(ar_temp_temp));  // Split input string into components
    
        vector<long> ar(ar_count);  // Create a vector to store the numbers
    
        for (int i = 0; i < ar_count; i++) {
            long ar_item = stol(ar_temp[i]);  // Convert string to long
            ar[i] = ar_item;  // Store the number in the array
        }
    
        long result = aVeryBigSum(ar);  // Call the function to compute the sum
    
        fout << result << "\n";  // Output the result
    
        fout.close();  // Close the output file
    
        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));  // Add the last token
        return tokens;
    }