DEV Community

Cover image for A Very Big Sum - Problem-Solving | HackerRank
Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

A Very Big Sum - Problem-Solving | HackerRank

A Very Big Sum is an easy-level Python problem that requires basic knowledge of Python. In this post, we will provide a Python solution for A Very Big Sum.

Problem Statement and Explanation

In this challenge, you need to compute and print the sum of the elements in an array, taking into account that some of those integers may be exceedingly large.

Input Format

  • The array ‘arr’ includes integers ‘a[i]’ ranging from 1 to 10^10.

Output Format

  • The sum of the elements in the array, as a single integer.

Solve Me First Solution in Python

def aVeryBigSum(ar):
return sum(ar)
view raw very_big_sum.py hosted with ❤ by GitHub

Solve Me First Solution in C++

// Complete the aVeryBigSum function below.
long aVeryBigSum(vector<long> ar) {
long long int total = accumulate(ar.begin(), ar.end(), 0ll);
return total;
}

Explanation of Solution

  • aVeryBigSum is a function in C++ that takes a vector of integers as input and returns the sum of all the elements in the vector. The function uses the accumulate() function to calculate the sum.

  • The accumulate() function is a standard library function in C++. It takes three arguments: the beginning iterator of the range, the end iterator of the range, and an initial value. In this case, the initial value is 0.

  • The accumulate() function works by iterating over the range and adding each element to the initial value. The final value of the accumulator is the sum of all the elements in the range.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more