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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay